Increment operator

D

deepak

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
 
I

Ian Collins

deepak said:
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.
 
B

Ben Pfaff

deepak said:

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.)
 
R

Richard Heathfield

Ian Collins said:
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".
 
R

Richard

Richard Heathfield said:
Ian Collins said:

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

Richard Heathfield

Richard said:
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.
 
R

Richard

Richard Heathfield said:
Richard said:


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

Richard Heathfield

Richard said:
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.
 
M

Manuel T

Ben said:
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,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
1:putchar(a[i&15]);break;}}}

Just another C hacker.

Great!
 
C

Chris Dollin

Manuel said:
Ben said:
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,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
1:putchar(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/.
 
K

Kenny McCormack

[QUOTE="Richard said:
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.[/QUOTE]

(Dicky is an) Alberto Gonzales in training.
 
K

Keith Thompson

Chris Dollin said:
Manuel said:
Ben said:
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,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
1:putchar(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/.

In the Perl newsgroups, there's a tradition of similarly short and
obuscated Perl programs that print the string "Just another Perl
hacker". Manuel T was probably referring to that. (And let's not
waste time arguing whether Manuel's comment, or mine, was worth the
bandwidth.)
 
C

Chris Dollin

Keith said:
Chris Dollin said:
Manuel said:
Ben Pfaff wrote:
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
b[]

={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,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
1:putchar(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/.

In the Perl newsgroups, there's a tradition of similarly short and
obuscated Perl programs that print the string "Just another Perl
hacker". Manuel T was probably referring to that. (And let's not
waste time arguing whether Manuel's comment, or mine, was worth the
bandwidth.)

Ah -- I see -- sorry, Manuel, I misread your comment as derogatory.
Apologies.
 
K

Kenneth Brody

Richard said:
Richard said:


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

Strange... I thought 3 or 4 is 1?

==========
#include <stdio.h>

int main(void)
{
int i;
i = ( 3 || 4 );
printf("3 or 4 is %d\n",i);
exit(0);
}
==========
Output:

3 or 4 is 1

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

Richard Heathfield wrote On 07/26/07 05:18,:
Ian Collins said:




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

I'd hazard a guess it was in the vicinity of the loins,
and that your dry-cleaning bill is outrageous.
 
R

Richard Heathfield

Eric Sosman said:
Richard Heathfield wrote On 07/26/07 05:18,:

I'd hazard a guess it was in the vicinity of the loins,
and that your dry-cleaning bill is outrageous.

The mind boggleth, doth it not? But no, the program really did write the
text "a suffusion of yellow" (and a newline character) to stdout. I
think my compiler must have been reading too much Douglas Adams
recently, but in any case that really really is the result that the
program produced.
 
K

Keith Thompson

Richard Heathfield said:
Eric Sosman said:
Richard Heathfield wrote On 07/26/07 05:18,:
Ian Collins said:
deepak wrote:
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. [...]
Undefined behaviour is not /required/ to be bizarre behaviour.
Incidentally, I ran the program at home and got the output: "a
suffusion of yellow".

I'd hazard a guess it was in the vicinity of the loins,
and that your dry-cleaning bill is outrageous.

The mind boggleth, doth it not? But no, the program really did write the
text "a suffusion of yellow" (and a newline character) to stdout. I
think my compiler must have been reading too much Douglas Adams
recently, but in any case that really really is the result that the
program produced.

Just out of curiosity, what output does your implementation produce
for the following?

#include <stdio.h>
int main(void)
{
int i = 3;
if (0) {
i = i++;
}
printf("%d\n", i);
return 0;
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top