Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Increment operator

Reply
Thread Tools

Increment operator

 
 
deepak
Guest
Posts: n/a
 
      07-26-2007

Hi,

For the following program the output I'm getting is not the one I
expected.

main()
{
int i = 3;

i = i++;

printf("%d\n", i);
}

Output is 3 and the output I expected was 4.
Is it an undefined behaviour?

Thanks,
Deepak

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-26-2007
deepak wrote:
> Hi,
>
> For the following program the output I'm getting is not the one I
> expected.
>
> main()
> {
> int i = 3;
>
> i = i++;
>
> printf("%d\n", i);
> }
>
> Output is 3 and the output I expected was 4.
>

Why would you expect 4? The result of i++ is the original value of i.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      07-26-2007
deepak <(E-Mail Removed)> writes:

> i = i++;


This is in the FAQ.

3.3: I've experimented with the code

int i = 3;
i = i++;

on several compilers. Some gave i the value 3, and some gave 4.
Which compiler is correct?

A: There is no correct answer; the expression is undefined. See
questions 3.1, 3.8, 3.9, and 11.33. (Also, note that neither
i++ nor ++i is the same as i+1. If you want to increment i,
use i=i+1, i+=1, i++, or ++i, not some combination. See also
question 3.12.)

--
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;}}}
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-26-2007
Ian Collins said:

> deepak wrote:
>> Hi,
>>
>> For the following program the output I'm getting is not the one I
>> expected.
>>
>> main()
>> {
>> int i = 3;
>>
>> i = i++;
>>
>> printf("%d\n", i);
>> }
>>
>> Output is 3 and the output I expected was 4.
>>

> Why would you expect 4? The result of i++ is the original value of i.


Yes, which is a good argument for expecting 3, but presumably the OP's
expectation was that the program's undefined behaviour would result in
a value that we might not expect - and boy, was he surprised!

Undefined behaviour is not /required/ to be bizarre behaviour.
Incidentally, I ran the program at home and got the output: "a
suffusion of yellow".

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-26-2007
Richard Heathfield <(E-Mail Removed)> writes:

> Ian Collins said:
>
>> deepak wrote:
>>> Hi,
>>>
>>> For the following program the output I'm getting is not the one I
>>> expected.
>>>
>>> main()
>>> {
>>> int i = 3;
>>>
>>> i = i++;
>>>
>>> printf("%d\n", i);
>>> }
>>>
>>> Output is 3 and the output I expected was 4.
>>>

>> Why would you expect 4? The result of i++ is the original value of
>> i.


Aha. But he didnt print the result of "i++". He printed "i" :-;

>
> Yes, which is a good argument for expecting 3, but presumably the OP's
> expectation was that the program's undefined behaviour would result in
> a value that we might not expect - and boy, was he surprised!
>
> Undefined behaviour is not /required/ to be bizarre behaviour.
> Incidentally, I ran the program at home and got the output: "a
> suffusion of yellow".


No you didn't. You got 3 or 4.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-26-2007
Richard said:

> Richard Heathfield <(E-Mail Removed)> writes:
>

<snip>

>> Incidentally, I ran the program at home and got the output: "a
>> suffusion of yellow".

>
> No you didn't. You got 3 or 4.


No I didn't. 3 or 4 is 7, and I certainly didn't get 7.

Anyway, since you accuse me of lying, perhaps I should demonstrate that
I am not.

me@here/ub> cat ub.c
main()
{
int i = 3;

i = i++;

printf("%d\n", i);
}

me@here/ub> compile ub.c
me@here/ub> ./ub
a suffusion of yellow
me@here/ub>

There are more things in heaven and earth, Horatio, than are dreamt of
in your philosophy.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-26-2007
Richard Heathfield <(E-Mail Removed)> writes:

> Richard said:
>
>> Richard Heathfield <(E-Mail Removed)> writes:
>>

> <snip>
>
>>> Incidentally, I ran the program at home and got the output: "a
>>> suffusion of yellow".

>>
>> No you didn't. You got 3 or 4.

>
> No I didn't. 3 or 4 is 7, and I certainly didn't get 7.
>
> Anyway, since you accuse me of lying, perhaps I should demonstrate that
> I am not.
>
> me@here/ub> cat ub.c
> main()
> {
> int i = 3;
>
> i = i++;
>
> printf("%d\n", i);
> }
>
> me@here/ub> compile ub.c
> me@here/ub> ./ub
> a suffusion of yellow
> me@here/ub>
>
> There are more things in heaven and earth, Horatio, than are dreamt of
> in your philosophy.


Wow. I am convinced. I apologise and take back all those things I said
about you :-;

Unless of course your "undefined" compile command got up to some
trickery and actually created another file called "ub".

Hmmm.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-26-2007
Richard said:

> Richard Heathfield <(E-Mail Removed)> writes:
>
>> Richard said:
>>
>>> Richard Heathfield <(E-Mail Removed)> writes:
>>>

>> <snip>
>>
>>>> Incidentally, I ran the program at home and got the output: "a
>>>> suffusion of yellow".
>>>
>>> No you didn't. You got 3 or 4.

>>
>> No I didn't. 3 or 4 is 7, and I certainly didn't get 7.
>>
>> Anyway, since you accuse me of lying, perhaps I should demonstrate
>> that I am not.
>>
>> me@here/ub> cat ub.c
>> main()
>> {
>> int i = 3;
>>
>> i = i++;
>>
>> printf("%d\n", i);
>> }
>>
>> me@here/ub> compile ub.c
>> me@here/ub> ./ub
>> a suffusion of yellow
>> me@here/ub>
>>
>> There are more things in heaven and earth, Horatio, than are dreamt
>> of in your philosophy.

>
> Wow. I am convinced. I apologise and take back all those things I said
> about you :-;


Yeah, right.

> Unless of course your "undefined" compile command got up to some
> trickery and actually created another file called "ub".
>
> Hmmm.


No, it was a shell script to invoke a C compiler, which compiled the
code as written. The resulting executable image was run, and it
produced the output cited above. I wrote the shell script only so that
I could avoid mentioning the (conforming) compiler I used, so that I
could in turn avoid accusations of plugging a particular compiler.

The output is genuine.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Manuel T
Guest
Posts: n/a
 
      07-26-2007
Ben Pfaff wrote:

> 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;}}}


Just another C hacker.

Great!
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      07-26-2007
Manuel T wrote:

> Ben Pfaff wrote:
>
>> 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;}}}

>
> Just another C hacker.


We're /all/ "just another C hacker": I don't see why you're making a fuss
about Ben's /signature/.

--
Chris "just another hacker" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM
c++ unary increment operator MSDousti C++ 1 11-01-2003 07:52 PM



Advertisments