puzzle in C

S

sreelal

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
 
M

Martin Ambuhl

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?

Random luck. You gave too few arguments to printf which, upon seeing
the specifier "%d" looks for a value. It happens to find 10; it might
have found anything, or gotten violently ill.

More dumb luck allowed you to get away with the required prototype for
the variadic printf, which including <stdio.h> would give you. It also
allowed you to get away with either the implicit int for the return type
for main (required to be explicit in C99) or leaving off 'return 0;' or
equivalent (needed for C89), and leaving off the end-of-line character
on the last line of output (needed for predictable portable behavior).

The program

#include <stdio.h>

int main(void)
{
int a = 12, b = 10;
printf("%d , a+b\n");
return 0;
}

gives for output my system (this time):

826102610 , a+b

Where it got that number, it only knows. At least it didn't segfault.
 
S

Sam Jervis

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
 
E

Eric

Sam said:
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
Guessing:
probably a and b are stack variables for his compiler and since 10 is the
last one on the stack it got scoffed up by printf looking for an arg
The other guy who answered below and got a total different answer - well,
maybe his compiler used register vars for a and b and that would leave the
stack in a different state from the first guy. Who knows, as you said,
its undefined.
Eric
 
D

Dan Pop

In said:
The other guy who answered below and got a total different answer - well,
maybe his compiler used register vars for a and b and that would leave the
stack in a different state from the first guy. Who knows, as you said,
its undefined.

And, therefore, there is little point in speculating about why printf
picked one value or another.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top