Search code examples
csequence-points

Sequence Point Warning clarification


I am a beginner in C. When I try to run the following code :

#include <stdio.h>

    int main(void) {

    int a = 3, b;

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

    return 0;
}

It shows me :

error: operation on 'a' may be undefined [-Werror=sequence-point]
b = printf("%d %d", a, a++);
                        ^

But here I am changing the value of a only once. Why is there a sequence point error then ?

I am using -Wall -Werror flags.


Solution

  • It is true that you are changing the value of a only once but order of evaluation of arguments a and a++ is not guaranteed. Either a or a++ will be evaluated first resulting in undefined behavior.