why in this possible??

#include <stdio.h>

int main()
{
int num1,a=1,b;
printf("%d %d %d ",a++,a++,a++);printf("%d %d ",a++,a++);




}

i think the out put should be 1 2 3 4 5.
Why is it 3 2 1 5 4??
https://en.cppreference.com/w/cpp/language/eval_order
the order of evaluation is unspecified.
In other words: Please don't do this. It used to be undefined behavior, but I guess it's now just unspecified, but either way, avoid incrementing a variable while also separately referencing/incrementing it again on the same statement.
Last edited on
tlgh1113 wrote:
i think the out put should be 1 2 3 4 5.

Why? What makes you think that? There is nothing in C or in C++ language specification that suggests any particular outcome of evaluating this expression. If you learned this somewhere, consider better teaching materials.

tlgh1113 wrote:
Why is it 3 2 1 5 4??

It isn't. You saw garbage output produces on the specific combination of compiler/version/options/target that you happened to use. I just ran this through a few compilers and got "3 2 1 5 4 " on some gcc, "1 2 3 4 5 " on some clang, "1 1 1 4 4 " on some visual studio. All three compilers were correct.

Ganado wrote:
It used to be undefined behavior, but I guess it's now just unspecified

unspecified in C++ as of C++17, remains totally undefined in C (that source code could be either, but looks more like C)
Last edited on
Topic archived. No new replies allowed.