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.??
C strange..
Moderator: Board moderators
Re: C strange..
Your code is undefined by the standard. It can give any result, just avoid coding like that.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.??
HoraPe
Your code is invalid C.See http://www.eskimo.com/~scs/C-faq/q3.2.htmlkenken 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()??
More on http://online-judge.uva.es/board/viewto ... ght=#20617
Saludos,
HoraPe
According to ANSI the output is undefined.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()??
[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
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
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.
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.
There is no correct or expected behaviour of invalid code. It could wellKrzysztof 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.
give an answer some times and a different answer on others.
Saludos,
HoraPe
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
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.
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.