Page 1 of 1
C strange..
Posted: Thu Mar 11, 2004 4:04 am
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.??
Re: C strange..
Posted: Thu Mar 11, 2004 5:07 am
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
Posted: Thu Mar 11, 2004 6:24 am
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()??
Posted: Thu Mar 11, 2004 6:50 am
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
Posted: Thu Mar 11, 2004 3:00 pm
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
Posted: Thu Mar 11, 2004 4:12 pm
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.
Posted: Thu Mar 11, 2004 4:53 pm
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
Posted: Fri Mar 12, 2004 3:55 am
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.
Posted: Mon Mar 15, 2004 2:11 am
by kenken
ok, i have read the links above...so, now i avoid using the undefined ANSI statement or assignment... thanks everyone...bless you