Execution of printf With ++ Operators in C
Last Updated :
30 Aug, 2024
Consider the following statement in C and predict its output.
printf("%d %d %d", i, ++i, i++);
This statement invokes undefined behavior by referencing both āiā and āi++ā in the argument list.
In C, the evaluation order of function arguments is not specified. It means the compiler is free to evaluate arguments in any order. That is why, Similar thing happens in statement printf("%d %d %d", i, ++i, i++); the order of evaluating i, ++i, and i++ is undefined, that leads to inconsistent and unpredictable results as different compilers or even the same compiler may choose different evaluation orders at different times.
For example, below three printf() statements may also cause undefined behaviour:
C
// C Program to demonstrate the three printf() statements
// that cause undefined behavior
#include <stdio.h>
// Driver Code
int main() {
volatile int a = 10;
printf("%d %d\n", a, a++);
a = 10;
printf("%d %d\n", a++, a);
a = 10;
printf("%d %d %d\n", a, a++, ++a);
return 0;
}
Output11 10
10 10
12 11 11
Explanation: Usually, the compilers read parameters of printf() from right to left. So, 'a++' will be executed first as it is the last parameter of the first printf() statement. It will print 10. Although now the value has been increased by 1, so the second last argument, i.e., will print 11. Similarly, the other statements will also get executed.
Therefore, it is not recommended to do two or more than two pre or post-increment operators in the same statement. This means that there's absolutely no temporal ordering in this process. The arguments can be evaluated in any order, and the process of their evaluation can be intertwined in any way.
Note: In pre-increment, i.e., ++a, it will increase the value by 1 before printing, and in post-increment, i.e., a++, it prints the value at first, and then the value is incremented by 1.
How to avoid the undefined behaviour in this case?
To ensure predictable behaviour and avoid undefined behaviour, it is recommended to separate operations into distinct statements. For example, instead of writing printf("%d %d %d", i, ++i, i++);, break it down into multiple steps like illustrated in the below example:
C
// C Program to demonstrate the three printf() statements
// without causing undefined behavior
#include <stdio.h>
int main(){
volatile int i = 10;
int temp1 = i;
int temp2 = ++i;
int temp3 = i++;
printf("%d %d %d", temp1, temp2, temp3);
return 0;
}
Similar Reads
%n in scanf() in C with Example In C, %n is a special format specifier. In the case of printf() function the %n assign the number of characters printed by printf(). When we use the %n specifier in scanf() it will assign the number of characters read by the scanf() function until it occurs. Key points: It is an edit conversion code
2 min read
How to Change the Output of printf() in main() in C? To change the output of printf() in main(), we can use Macro Arguments. #define macro can be used for this task. This macro is defined inside the function. Although, #define can be used without declaring it in the function, in that case always the printf() will be changed. The function needs to be c
2 min read
To find sum of two numbers without using any operator Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used.Solution It's a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in
9 min read
Nested printf (printf inside printf) in C Predict the output of the following C program with a printf inside printf. C #include<stdio.h> int main() { int x = 1987; printf("%d", printf("%d", printf("%d", x))); return(0); } Output : 198741 Explanation : 1. Firstly, the innermost printf is executed which res
1 min read
Relational Operators in C In C, relational operators are the symbols that are used for comparison between two values to understand the type of relationship a pair of numbers shares. The result that we get after the relational operation is a boolean value, that tells whether the comparison is true or false. Relational operato
4 min read