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
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
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
Increment (Decrement) operators require L-value Expression What will be the output of the following program? c #include<stdio.h> int main() { int i = 10; printf("%d", ++(-i)); return 0; } A) 11 B) 10 C) -9 D) None Answer: D, None - Compilation Error. Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) opera
1 min read
Unary Operators in C In C programming, unary operators are operators that operate on a single operand. These operators are used to perform operations such as negation, incrementing or decrementing a variable, or checking the size of a variable. They provide a way to modify or manipulate the value of a single variable in
5 min read
Output of C programs | Set 55 (Shift Operators) Prerequisite: Shift operators Q.1 What Is The Output Of this program? C #include <stdio.h> int main() { unsigned int i = 0x80; printf("%d ", i << 1); return 0; } Option a) 0 b) 256 c) 100 d) 80 ans :- b Explanation :- We know that 0x is hexa-decimal representation of number so
3 min read
Unary Operators In C++ In C++, unary operators are the type of operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address.C++ has a total of 9 unary operators:Table of ContentIncrement Operator (++)Decrement Operat
6 min read