C strange..

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
kenken
New poster
Posts: 4
Joined: Thu Nov 15, 2001 2:00 am
Contact:

C strange..

Post by kenken »

hey there.. anyone know why this strange thing happens? c programming.
i wonder why....


int a = 9, b = 3;
a = a++ + b++;
printf("%d", a);

output: 13

but,

int a =9, b = 3;
printf("%d", a = a++ + b++);

output: 12

why.??

horape
New poster
Posts: 49
Joined: Sat Sep 13, 2003 3:18 pm
Location: Buenos Aires

Re: C strange..

Post by horape »

kenken wrote:hey there.. anyone know why this strange thing happens? c programming.
i wonder why....


int a = 9, b = 3;
a = a++ + b++;
printf("%d", a);

output: 13

but,

int a =9, b = 3;
printf("%d", a = a++ + b++);

output: 12

why.??
Your code is undefined by the standard. It can give any result, just avoid coding like that.

HoraPe

kenken
New poster
Posts: 4
Joined: Thu Nov 15, 2001 2:00 am
Contact:

Post by kenken »

what mean not standard?? is that the two codes are the same?? then why it gives diff output? according to NASI, i think the ouput should be 13, but after printf() fucntion, it changes... strange~! it is bcos of printf()??

horape
New poster
Posts: 49
Joined: Sat Sep 13, 2003 3:18 pm
Location: Buenos Aires

Post by horape »

kenken wrote:what mean not standard?? is that the two codes are the same?? then why it gives diff output? according to NASI, i think the ouput should be 13, but after printf() fucntion, it changes... strange~! it is bcos of printf()??
Your code is invalid C.See http://www.eskimo.com/~scs/C-faq/q3.2.html

More on http://online-judge.uva.es/board/viewto ... ght=#20617

Saludos,
HoraPe

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post by CDiMa »

kenken wrote:what mean not standard?? is that the two codes are the same?? then why it gives diff output? according to NASI, i think the ouput should be 13, but after printf() fucntion, it changes... strange~! it is bcos of printf()??
According to ANSI the output is undefined.
[c]a = a++ + b++;[/c]
If a variable is written to in an expression, all accesses to it within the same expression (i.e. before the next sequence point) are allowed only to compute the value to be written.
The post-increment operator writes to the variable a. a may be read to compute the value of the expression. a cannot be written to until the next sequence point which in this case is the semicolon. By writing to a the value of a becomes unspecified by the ANSI standard.

Hope this explanation is clear enough!

Ciao!!!

Claudio

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

The code is invalid, but the output is correct! (correct means that it behaves like it is expected to)
The reason for this is that postincrementation is made after the whole instruction it is a part of. So in case 1, variable a is incremented before printf, but in case 2 after it.
Last edited by Krzysztof Duleba on Fri Mar 12, 2004 2:12 pm, edited 1 time in total.

horape
New poster
Posts: 49
Joined: Sat Sep 13, 2003 3:18 pm
Location: Buenos Aires

Post by horape »

Krzysztof Duleba wrote:The code is invalid, but the output is correct! (correct means that it behaves like it is expected to)
The reason for this is that postincrementation is made after the whole instruction it is a part of. So in case 1, variable a is incremented before printf, but in case 1 after it.
There is no correct or expected behaviour of invalid code. It could well
give an answer some times and a different answer on others.

Saludos,
HoraPe

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

If a code is incorrect and it's behaviour is undefined,
still one can make some predictions.
It's not necessary that a program gets segmentation
fault while trying to access unreserved memory,
but it's the most common case and that's what
one should expect to happen. Same here, except that
instead of bailing out, the program still works and the
variables have certain (quite predictable) values.

kenken
New poster
Posts: 4
Joined: Thu Nov 15, 2001 2:00 am
Contact:

Post by kenken »

ok, i have read the links above...so, now i avoid using the undefined ANSI statement or assignment... thanks everyone...bless you

Post Reply

Return to “C”