
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Execution of printf with ++ Operators in C
In some problems we can find some printf() statements are containing some lines with ++ operator. In some questions of competitive examinations, we can find these kind of questions to find the output of that code. In this section we will see an example of that kind of question and try to figure out what will be the answer.
Example Code
#include<stdio.h> int main() { volatile int x = 20; printf("%d %d\n", x, x++); x = 20; printf("%d %d\n", x++, x); x = 20; printf("%d %d %d ", x, x++, ++x); return 0; }
Now we will try to guess what will be the output. Most of the compilers takes each parameter of printf() from right to left. So in the first printf() statement, the last parameter is x++, so this will be executed first, it will print 20, after that increase the value from 20 to 21. Now print the second last argument, and show 21. Like that other lines are also calculated in this manner. For ++x, it will increase the value before printing, and for x++, it prints the value at first, then increase the value.
Please check the output to get the better understanding.
Output
21 20 20 20 22 21 21