What is Garbage value in C or C++ and Why It Produces if we are not initialized any variables?

Asked 10-Mar-2018
Updated 10-Mar-2018
Viewed 586 times

1 Answer


0

The Garbage Value in C & C++ :-
When we make a variable declaration in programming languages such as C and C++ but do not set any value to it. Then that variable at that time like a waste material or garbage. But memory allocation has done at the creation time.
Here some condition is behind the garbage value:
1). When we explicitly define a variable in C or C ++, but no value is passed to them, then that variables are becoming like a garbage.
2). When we reach on extra indexes of an array and that array is on the stack, C or C++ will easily allow you to do that but it could generate the garbage. etc.
An Example for Garbage Collection - 
#include<stdio.h>

void main()
{
int a,b,c;
clrscr();
printf(“\nValue of Variables Before Initialization a=%d,b=%d,c=%d”,a,b,c);//it printf the garbage value
a=10;
b=20;
c=a+b;
printf(“\nValue of Variables After Initialization a=%d,b=%d,c=%d”,a,b,c);
getch();