#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=11;
clrscr();
printf("%d");
getch();
}
Output=11 How the output is 11 even I am not mentioned the variable name in the printf function.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=11;
clrscr();
printf("%d");
getch();
}
Output=11 How the output is 11 even I am not mentioned the variable name in the printf function.
The 11 is on the stack because of the b
variable, and your printf()
function is looking on the stack for a value on the stack because that's where variables get passed.
If you add a c=47
, you'll probably get 47. But this is undefined behavior.
This is called "undefined behavior", which means that program can do just about anything.
What is actually happening in this case is that both variables and function parameters are put on the stack. Since you aren't passing the parameter that printf is expecting, it ends up pulling something else off the stack, which is your b
variable.
But because it is undefined behavior, if you had a different compiler, a different CPU, or even different compile options, such as a higher optimization level, you could get very different results.