[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Please explain the output

bhawna great

3/27/2011 1:25:00 AM

#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?
12 Answers

Ian Collins

3/27/2011 1:29:00 AM

0

On 03/27/11 02:25 PM, bhawna wrote:
> #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.

--
Ian Collins

China Blue Veins

3/27/2011 1:33:00 AM

0

In article <6e699fb3-b633-4445-b31b-a4ac97dc1506@22g2000prx.googlegroups.com>,
bhawna <bhawnagreat@gmail.com> wrote:

> #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?

Depends on how many monkeys you hire to pound on keyboards.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. Alameda County Sheriff.

Ben Bacarisse

3/27/2011 1:42:00 AM

0

bhawna <bhawnagreat@gmail.com> writes:

> #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.

--
Ben.

Andrew Smallshaw

3/27/2011 2:06:00 AM

0

On 2011-03-27, bhawna <bhawnagreat@gmail.com> wrote:
> #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.

--
Andrew Smallshaw
andrews@sdf.lonestar.org

bhawna great

3/27/2011 2:18:00 AM

0

Thanks

Joe Wright

3/27/2011 4:57:00 AM

0

On 3/26/2011 21:25, bhawna wrote:
> #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?
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.

--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."

Keith Thompson

3/27/2011 8:03:00 AM

0

Joe Wright <joewwright@comcast.net> writes:
> On 3/26/2011 21:25, bhawna wrote:
>> #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?
> 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)".)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

zooZodoy

3/27/2011 4:57:00 PM

0

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)
.
.
.
1 0x804962C 99 0x63
2 0x8049630 3 0x3
3 0x8049634 44 0x2c
4 0x8049638 134518324 0x8049634
5 0x804963C pointer 0xpointer

1 - 3) the int values
4) SP (StackPointer)
stores the address before the printf is called
printf is a funcion!
5) any nummber which is an adress
that points to an register
where the "%d %d %d" string is stored

if printf is called it pharses the string "%d %d %d" which tells printf
to print out 3 int's, it expect the 3 int's at 4,3,2 in the stack,
every time, one int is printed the SP is decrremented about 4bytes


here the stack for right call of printf

StackPointer Value (Value in hex)
.
.
.
1 0x804962C 99 0x63
2 0x8049630 3 0x3
3 0x8049634 44 0x2c
4 0x8049638 134518324 0x8049634
5 0x804963C 99 0x63
6 0x8049640 3 0x3
7 0x8049644 44 0x2c
8 0x8049644 pointer 0xpointer

Joe Wright

3/27/2011 6:46:00 PM

0

On 3/27/2011 04:02, Keith Thompson wrote:
> Joe Wright<joewwright@comcast.net> writes:
>> On 3/26/2011 21:25, bhawna wrote:
>>> #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?
>> 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)".)
>
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.

--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."

Keith Thompson

3/27/2011 6:53:00 PM

0

Joe Wright <joewwright@comcast.net> writes:
> On 3/27/2011 04:02, Keith Thompson wrote:
>> Joe Wright<joewwright@comcast.net> writes:
[...]
>> 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)".)
>>
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"