New course! Every coder should learn Generative AI!
Try a free lesson0
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.
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.
+ 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