简体   繁体   中英

Sequence points and I/O format Specifiers in C

The C standard (Annex C) states that there is a sequence point

After the actions associated with each formatted input/output function conversion specifier.

Given that, why am I getting an unsequenced modification and access to i (clang) warning for the below?

int i = 0;
printf("%d, %d\n", i, ++i);

Based on the standard, there is a sequence point after the first and the second %d . If so I should be getting a 0 1 ? But then there is no ordering guarantee in the evaluation of function arguments and I could be getting 1 1 instead?

So what does the text of the standard I quoted really mean?

Given that, why am I getting an unsequenced modification and access to i (clang) warning for the below?

 int i = 0; printf("%d, %d\n", i, ++i);

Because the problem happens when evaluating the function's argument list, before the function is actually called. The evaluations of multiple arguments to the same function call are not sequenced with respect to each other, and you both read and modify i via separate, unsequenced arguments.

The provision you cited is not relevant to this issue. It describes sequence points between the I/O operations performed by the function when it executes. Because function arguments are always passed by value, and because there is a sequence point between evaluating the arguments and executing the function body, I don't see any practical relevance of that provision to the printf -family functions.

For scanf & friends, however, the provision helps ensure that

int i;
scanf("%d, %d", &i, &i);

has well-defined behavior, because it specifies that the two resulting writes to i are sequenced with respect to each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM