Hey i am getting a wrong output for the below code which is written in C language

#include <stdio.h>

int main()
    printf("Hello World");
    int a = 10;
    printf("%d %d %d", ++a, a--, --a);
    return 0;

output i am getting on replit is 11 11 9
while on other complier it is 9 9 9

The order of evaluation of expressions is unspecified in the C standard. Don’t modify a variable in the same statement multiple times.
I would recommend making the compiler fail when it sees such mistakes: paste in Shell:

sed -i '/override CFLAGS/s/-Wno-everything/-Wmost -Werror/' Makefile

Alternatively, just ensure that code intelligence is enabled (in the “Settings” tool in the workspace) and look out for yellow underlines in your code which should’ve complained about “multiple unsequenced modifications”.

1 Like