Garbage Value in C

Garbage value  :

Garbage value refers to the value stored in the memory of the variable without being initialized or declared a proper value. It can lead to unpredictable output of the program.

Garbage value in c

About Garbage Value :

The C compiler reserves a specific amount of memory for each variable when it is declared. The compiler does not specify the value that will be put in that memory, thus it might be anything that was previously stored there. This implies that a variable that has been defined but not initialized will have a “garbage value”—basically, a random value that has been placed in that memory address.

Example :

Run
#include<stdio.h> 

int main() {
    int val;
    printf("The value of val : %d ", val);
    return 0;
}

Output :

The value of val : 0 

In the above program, we initialized the variable val without declared any value but during execution it print the value 0 for val which is the garbage value stored already in the memory.

Types of Garbage Value :

There are four types of garbage value in C :

  • Uninitialized variables
  • Out of bound array access.
  • Unused Memory
  • Global Variables

Clear Garbage Value in C :

To clear garbage value, we can initialized it with the value during declaration or later in the code.

Disadvantages of Garbage value :

Using Garbage value in the program can lead to unpredictable and unwanted output of the program. For proper functioning of the code, garbage value should be managed properly.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription