Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Example
3.
Frequently Asked Questions
3.1.
Can operators be used in printf() functions?
3.2.
What is the compiling order of the printf function?
3.3.
What is the difference between i++ and ++i?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Execution of printf with ++ operators

Author Aditi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The printf() function in the C programming language is used to print ("character, string, float, integer, octal, and hexadecimal values") to the output screen. To show the value of an integer variable, we use the printf() function with the % d format specifier.

Consider the C statement below and guess the result.

printf("%d %d %d", i, ++i, i++);

By addressing both ‘i’ and 'i++' in the argument list, this statement causes undefined behavior. The sequence in which the arguments are evaluated is not specified. Orders may alter depending on the compiler. At different times, a single compiler can pick multiple orders.

Various printf() statements, including lines using the ++ operator, can be found in some situations. These questions can be seen in multiple competitive test questions to determine the code's output.

In this blog, we will learn about the behavior ++operator with printf function.

Also see: C Static Function and  Short int in C Programming

Example

In this part, we'll look at an example of that question and try to figure out the solution.

Three printf() commands, in the example below, may result in ambiguous behavior:

#include <stdio.h>

int main()
{
    volatile int x = 1;
    printf("%d %d\n", x, x++);
 
    x = 1;
    printf("%d %d\n", x++, x);
 
    x = 1;
    printf("%d %d %d\n", x, x++, ++x);
    return 0;
}

Output:

2 1
1 1
3 2 2

Compilers usually read printf() parameters from right to left. As the last parameter of the first printf() expression, 'x++' will be run first. It will print 1. However, because the value has now been increased by one, the second last argument, i.e., will now print 2. The other statements will be performed in the same way.

Note that in pre-increment, ++x, the value increases by one before printing, but in post-increment, x++, the value is printed first and then incremented by 1.

As a result, using two or more pre or post-increment operators in the same statement is not advised. This indicates that there is no chronological sequence in this procedure. The arguments can be assessed in any sequence, and the evaluation process can be entwined.

Now we'll try to estimate what the result will be. The majority of compilers order printf() parameters from right to left. So, because the last parameter in the first printf() expression is x++, it will be run first, printing 1 and then increasing the number from 1 to 2. Show 2 after printing the second final argument. Similarly, other lines are computed in the same way. It will raise the value before printing for ++x, and it will print the value first, then increase the value for x++.

Also see, Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

Can operators be used in printf() functions?

Yes, we can use operators inside the printf() functions. 

What is the compiling order of the printf function?

Majority of compilers order printf() parameters from right to left.

What is the difference between i++ and ++i?

There are two types of ++ operators: pre and post-increment. i++ is post increment where the value of i will be used first and then increment. ++i is pre-increment, and it will first increment the value of i and then use it.

Conclusion

In this article, we have extensively discussed the execution of printf with the ++ operator in C.

We hope this blog has helped you enhance your knowledge regarding the ++ operator execution with the printf function. Some official documentation on C programming language that can help you improve your understanding is C documentation.

If you would like to learn more, check out our articles on Introduction to operatorsIntroduction to c, and operators

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Comma Operator in C
Next article
Anything Written in sizeof() is Never Executed
Live masterclass