sreelal said:
Hi all,
main()
{
int a=12,b=10;
printf("%d , a+b");
}
the output is
is
10,a+b
why it is so?
I am using Turbo C compiler on Windows
plz give me reply soon
regs
sreelal
First of all, was that the program you intended to write? Did you
perhaps intend to write:
printf("%d", a+b);
Which would print:
22
To answer the question that you asked literally, in short, you gave
printf() the format string "%d , a+b" but you didn't give it a value to
print in place of %d; hence your use of printf is wrong and its
behaviour is undefined. That it printed 10 is coincidence.
Long answer. Since your format string includes %d the printf() function
assumes that it has been called with a second argument, a signed
integer. Arguments to functions are conventionally passed on the stack.
In the case of your program only one argument is placed on the stack -
the memory location of the string literal "%d , a+b". However the
printf function will try to read two values from the stack - the memory
location of the format string, and a signed integer to print in place of
%d. Since nothing was placed on the stack for the latter argument the
value printed could be anything, depending on whatever value happens to
be just above the stack in your computer's memory at the time. For you
it was the number 10, which may or may not be related to the previous
statement, the assignment of 10 to b. If so this was a coincidence.
Basically your program as printed is wrong and it could behave
differently depending on your compiler, operating system and platform.
Hope this helps,
Sam