Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Please explain the output (http://www.velocityreviews.com/forums/t745776-please-explain-the-output.html)

bhawna 03-27-2011 01:25 AM

Please explain the output
 
#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?

Ian Collins 03-27-2011 01:28 AM

Re: Please explain the output
 
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

Ben Bacarisse 03-27-2011 01:42 AM

Re: Please explain the output
 
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 03-27-2011 02:05 AM

Re: Please explain the output
 
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 03-27-2011 02:17 AM

Re: Please explain the output
 
Thanks

Keith Thompson 03-27-2011 08:02 AM

Re: Please explain the output
 
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.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

zooZodoy 03-27-2011 04:56 PM

Re: Please explain the output
 
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)

Keith Thompson 03-27-2011 06:52 PM

Re: Please explain the output
 
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.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ian Collins 03-27-2011 07:46 PM

Re: Please explain the output
 
On 03/28/11 05:56 AM, zooZodoy wrote:
> 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


Says who?

--
Ian Collins

Hans Vlems 03-28-2011 06:46 AM

Re: Please explain the output
 
On 27 mrt, 03:25, bhawna <bhawnagr...@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?


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


All times are GMT. The time now is 07:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.