How pre-increment operator works in C language? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How pre-increment operator works in C language?

#include<stdio.h> int main(){ int b=10; printf("%d\n",(++b)+b+b);b=10; //prints 33 printf("%d\n",b+(++b)+b);b=10; //prints 33 printf("%d\n",b+b+(++b));b=10; //prints 31 printf("%d\n",b+b*(++b)); // prints 132 return 0; } In this program how the outputs/results are calculated? In line 4 and 5, the results are same which is 33; but in line 6 the result is 31. What is the difference between line 4/5 and line 6? And, in line 7 why the result is 132? Thanks in advance.

18th Apr 2020, 3:50 PM
Kishorè Shanto
2 Answers
+ 2
I think it makes sense when you go through the code and think about operator precedence etc. but honestly that doesn't have any practical value. You will never encounter such a code in real life (except someone writes unreadable garbage code). Don't waste your time with this. Rather think about how to write better code and improve your skills.
18th Apr 2020, 4:17 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 5
Look it works from right to left , 1st line , (++b)+b+b =11+11+11=33 2nd line, b+(++b)+b =10+11+11=32 3rd line, b+b+(++b) =10+10+11=31 4th line , b+b*(++b) =11+11*11=132 Most the answers are complier dependent.. So sometimes it is not easy to predict result
18th Apr 2020, 4:23 PM
Raj Kalash Tiwari
Raj Kalash Tiwari - avatar