Velocity Reviews > Plz explain me the following code

# Plz explain me the following code

manish sahu
Guest
Posts: n/a

 09-19-2009
plz tell me how this code is working and what would be the the o/p
1)

{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %dn",x,y);
}

2)
{
int x=5;
printf("%d %d %dn",x,x<<2,x>>2);
}
Thankyou

Nick Keighley
Guest
Posts: n/a

 09-19-2009
On 19 Sep, 17:24, manish sahu <(E-Mail Removed)> wrote:

> plz tell me how this code is working and what would be the the o/p
> 1)
>
> {
> int x=20,y=35;
> x = y++ + x++;

the C standard does not define the behaviour of a program like
this. Anything like
i = i++;
exhibits undefined behaviour. Dig out the comp.lang.c FAQ for details

> y = ++y + ++x;
> printf("%d %dn",x,y);
>
> }
>
> 2)
> {
> int x=5;
> printf("%d %d %dn",x,x<<2,x>>2);}

why don't you just run the program? Look in your textbook
to find out what << and >> do

Seebs
Guest
Posts: n/a

 09-19-2009
On 2009-09-19, manish sahu <(E-Mail Removed)> wrote:
> plz tell me how this code is working and what would be the the o/p
> 1)
>
> {
> int x=20,y=35;
> x = y++ + x++;
> y = ++y + ++x;
> printf("%d %dn",x,y);
> }

This could do just about anything; it's undefined behavior and there is no
realistic way to determine what it "should" produce.

> 2)
> {
> int x=5;
> printf("%d %d %dn",x,x<<2,x>>2);
> }

This one is well-defined and requires no explanation.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

manish sahu
Guest
Posts: n/a

 09-21-2009
On Sep 19, 4:59*pm, Seebs <(E-Mail Removed)> wrote:
> On 2009-09-19, manish sahu <(E-Mail Removed)> wrote:
>
> > plz tell me how this code is working and what would be the the o/p
> > 1)

>
> > {
> > int x=20,y=35;
> > x = y++ + x++;
> > y = ++y + ++x;
> > printf("%d %dn",x,y);
> > }

>
> This could do just about anything; it's undefined behavior and there is no
> realistic way to determine what it "should" produce.
>
> > 2)
> > {
> > int x=5;
> > printf("%d %d %dn",x,x<<2,x>>2);
> > }

>
> This one is well-defined and requires no explanation.
>
> -s
> --
> Copyright 2009, all wrongs reversed. *Peter Seebach / (E-Mail Removed)://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Thankyou dear

Peter Nilsson
Guest
Posts: n/a

 09-21-2009
On Sep 20, 9:59*am, Seebs <(E-Mail Removed)> wrote:
> On 2009-09-19, manish sahu <(E-Mail Removed)> wrote:
> > int x=5;
> > printf("%d %d %dn",x,x<<2,x>>2);
> > }

>
> This one is well-defined and requires no explanation.

Whether a text line requires a terminating new-line is
implementation defined. There can be practical consequences
to leaving it off. [e.g. on old line printers.]

--
Peter

Ben Pfaff
Guest
Posts: n/a

 09-21-2009
Peter Nilsson <(E-Mail Removed)> writes:

> On Sep 20, 9:59Â*am, Seebs <(E-Mail Removed)> wrote:
>> On 2009-09-19, manish sahu <(E-Mail Removed)> wrote:
>> > int x=5;
>> > printf("%d %d %dn",x,x<<2,x>>2);
>> > }

>>
>> This one is well-defined and requires no explanation.

>
> Whether a text line requires a terminating new-line is
> implementation defined. There can be practical consequences
> to leaving it off. [e.g. on old line printers.]

The "n" is there, I think that manish just left off the \ as a
typo.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}

Keith Thompson
Guest
Posts: n/a

 09-21-2009
Peter Nilsson <(E-Mail Removed)> writes:
> On Sep 20, 9:59Â*am, Seebs <(E-Mail Removed)> wrote:
>> On 2009-09-19, manish sahu <(E-Mail Removed)> wrote:
>> > int x=5;
>> > printf("%d %d %dn",x,x<<2,x>>2);
>> > }

>>
>> This one is well-defined and requires no explanation.

>
> Whether a text line requires a terminating new-line is
> implementation defined. There can be practical consequences
> to leaving it off. [e.g. on old line printers.]

"n" was almost certainly a typo for "\n".

It's not clear that the output produced by the printf call is going to
be the last line of the program's output. The posted code was just a
single compound statement. It was probably intended to be the body of
main(), but as long as we're not making assumptions ...

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"

Processor-Dev1l
Guest
Posts: n/a

 09-22-2009
On Sep 19, 6:24*pm, manish sahu <(E-Mail Removed)> wrote:
> plz tell me how this code is working and what would be the the o/p
> 1)
>
> {
> int x=20,y=35;
> x = y++ + x++;
> y = ++y + ++x;
> printf("%d %dn",x,y);
>
> }

x is set to 20 and y is set to 35.
in the next line you make x a result of 20+35 and THEN you increase
both x and y. It means x should be 56 and y 35.
Then you increase x (57) and y (36) and y = y + x will make y to be
set to 93.
But as my colleagues here said, it is compiler dependent (this code
can be just junk)

>
> 2)
> {
> int x=5;
> printf("%d %d %dn",x,x<<2,x>>2);}
>
> Thankyou

Man, find out something about binary shifts by yourself .

Processor-Dev1l
Guest
Posts: n/a

 09-22-2009
On Sep 22, 8:10*am, Processor-Dev1l <(E-Mail Removed)> wrote:
> On Sep 19, 6:24*pm, manish sahu <(E-Mail Removed)> wrote:> plz tell me how this code is working and what would be the the o/p
> > 1)

>
> > {
> > int x=20,y=35;
> > x = y++ + x++;
> > y = ++y + ++x;
> > printf("%d %dn",x,y);

>
> > }

>
> x is set to 20 and y is set to 35.
> in the next line you make x a result of 20+35 and THEN you increase
> both x and y. It means x should be 56 and y 35.
> Then you increase x (57) and y (36) and y = y + x will make y to be
> set to 93.
> But as my colleagues here said, it is compiler dependent (this code
> can be just junk)

Correction here:
> x is set to 20 and y is set to 35.
> in the next line you make x a result of 20+35 and THEN you increase
> both x and y. It means x should be 56 and y 36.
> Then you increase x (57) and y (37) and y = y + x will make y to be
> set to 94.
> But as my colleagues here said, it is compiler dependent (this code
> can be just junk)

>
>
>
> > 2)
> > {
> > int x=5;
> > printf("%d %d %dn",x,x<<2,x>>2);}

>
> > Thankyou

>
> Man, find out something about binary shifts by yourself .

Sorry for typo...

Keith Thompson
Guest
Posts: n/a

 09-22-2009
Processor-Dev1l <(E-Mail Removed)> writes:
> On Sep 19, 6:24Â*pm, manish sahu <(E-Mail Removed)> wrote:
>> plz tell me how this code is working and what would be the the o/p
>> 1)
>>
>> {
>> int x=20,y=35;
>> x = y++ + x++;
>> y = ++y + ++x;
>> printf("%d %dn",x,y);
>>
>> }

> x is set to 20 and y is set to 35.
> in the next line you make x a result of 20+35 and THEN you increase
> both x and y. It means x should be 56 and y 35.
> Then you increase x (57) and y (36) and y = y + x will make y to be
> set to 93.
> But as my colleagues here said, it is compiler dependent (this code
> can be just junk)

The behavior is undefined, as several posters have already pointed
out.

[snip]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"