Please explain the output

B

bhawna

#include<stdio.h>
int main()
{

int i=99,a=3,p=44;
printf("%d %d %d");
return 0;
}

/*Output*/
134518324 99 3

Is the output compiler dependent?
 
I

Ian Collins

#include<stdio.h>
int main()
{

int i=99,a=3,p=44;
printf("%d %d %d");
return 0;
}

/*Output*/
134518324 99 3

Is the output compiler dependent?

The output is undefined.
 
B

Ben Bacarisse

bhawna said:
#include<stdio.h>
int main()
{

int i=99,a=3,p=44;
printf("%d %d %d");
return 0;
}

/*Output*/
134518324 99 3

Is the output compiler dependent?

Not even that. I get different output every time I run the program.
The problem is that the behaviour is not defined by the specification of
the C programing language so there is no reasonable way to ascribe any
meaning to the code. There is not even any obligation on the compiler
to generate an executable from this source.
 
A

Andrew Smallshaw

#include<stdio.h>
int main()
{

int i=99,a=3,p=44;
printf("%d %d %d");
return 0;
}

/*Output*/
134518324 99 3

Is the output compiler dependent?

Yes, and system, and possibly even invocation dependent if the
address space is randomised each invocation to prevent shellcode
attacks. The implementation is free to segfault or anything else
it likes in this situation, but the most likely real-world outcome
is as here, where it seems that printf is looking on the stack for
the missing parameters. Since none have been placed there it is
finding things that are not really intended for it, at least not
in this context. It finding the storage allocated to i and a should
therefore come as no surprise. I imagine the 134518324 is the return
address for when printf returns - if you look it up in a debugger
you will probably find it is the address of the return statement.
 
K

Keith Thompson

Joe Wright said:
Try..

#include<stdio.h>
int main(void) {
int i=99,a=3,p=44;
printf("%d %d %d", i, a, p);
return 0;
}
output is..
99 3 44
..as you might expect.

bhawna, this is why it's helpful to be more explicit in your
question, particularly about why you're asking. You asked whether
the output is compiler dependent, but you didn't say why you thought
it might be. You could plausibly have just forgotten to pass i,
a, and p as arguments. You could equally plausibly have done so
deliberately, and been curious about the consequences.

Oh, and you really should have a "\n" at the end of the output;
some systems may not show the output correctly without it.

(And to nitpick even further, "int main()" should be
"int main(void)".)
 
Z

zooZodoy

Am 27.03.2011 03:25, schrieb bhawna:
#include<stdio.h>
int main()
{

int i=99,a=3,p=44;
printf("%d %d %d");
return 0;
}

/*Output*/
134518324 99 3

Is the output compiler dependent?


an int is 4 byte
an adress is byte

have a look at the Stack:


StackPointer Value (Value in hex)
 
K

Keith Thompson

Joe Wright said:
Hi Keith. I should have added "\n" in the format string. I did change "int
main()" to "int main(void)". You missed it. If I want authority on C here I
choose you, now that Heathfield and Pop have quit. I really miss Dan
Pop.

I comments were mostly directed at the OP, not at you. My main point
was really that the OP didn't provide enough information for you (or
anyone else) to understand just what he was asking, except in the most
literal sense.
 
H

Hans Vlems

#include<stdio.h>
int main()
{

        int i=99,a=3,p=44;
        printf("%d %d %d");
        return 0;
        }

/*Output*/
134518324 99 3

Is the output compiler dependent?

It sure helps if you tell the compiler what variables you want to
print with printf.
printf takes two kinds of input. The first is a format-string that
tells printf how to organize the lay-out of the output.
Count the number of %-signs and that tells you how many variables must
follow the format string.
In your example there are three %-signs, so printf expects three
variables while you gave it none at all.
Printf doesn't complain however, it just prints data that sits in
memory locations that weren't specified so it prints garbage.
Hans
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top