I am a novice programmer in C.
Here is a program:
#include <stdio.h>
void main()
{
int a,b, c ;
printf("%d %d %d",a,b,c);
}
output:
7811664 31 2130567168 (1st time)
4665936 31 2130567168 (2nd time)
I didn't get this output?
I am a novice programmer in C.
Here is a program:
#include <stdio.h>
void main()
{
int a,b, c ;
printf("%d %d %d",a,b,c);
}
output:
7811664 31 2130567168 (1st time)
4665936 31 2130567168 (2nd time)
I didn't get this output?
These are garbage values as you have not initialized the variables. As pointed in one of the below links Unassigned variables has so-called indeterminate state that can be implemented in whatever way ie, its an undefined behavior.
On a side note:-
The garbage values are not assigned they are allocated to the variable when they are declared ie, the value is already there. When you initialize the variable the garbage value is overwritten
For refernce you may like to check these Threads:-
Also worth mentioning that the main should have a int
return type rather than void
You may check this:-
In C and C++, the function prototype of the main function looks like one of the following:
int main(void);
int main();
int main(int argc, char **argv);
int main(int argc, char *argv[]);
You've declared the variables, which means the compiler has allocated memory to them, but you haven't initialized them, which means their memory contains whatever data happened to be present when the the program began.
Since the program may not always be loaded into the same area of memory, and since the contents of memory can change between runs, the uninitialized values appear to be random.
This is a perfect demonstration of the reason you should always initialize variables before using them.
Why not give them values? You got undefined behaviour