Explain the output for the below question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Explain the output for the below question

int main() { int i=5; int a= ++i + ++i + ++i; printf("%d",a); return 0; } O/P :22

24th May 2019, 2:34 AM
Komali Pinnaka
4 Answers
+ 3
The answer is compiler dependent. It could be 24 , 22 or anything else. you are modifying i multiple times before a defined sequence point. So the behavior will be undefined. https://en.m.wikipedia.org/wiki/Undefined_behavior https://en.m.wikipedia.org/wiki/Sequence_point
24th May 2019, 6:48 AM
kiRA
kiRA - avatar
+ 2
Second ++ operators result will be overwritten to the first operand.. even if we have n number of operands, that effect will take place only between the first and second operand. For example if we have I=5 A=++I + ++I + ++I + ++I + ++I i.e.. 6+7+8+9+10 and 6 will be replaced by 7.. so 7+7+8+9+10=41. Will result in 41
24th May 2019, 4:37 AM
Srija Padmanabhuni
+ 2
according to the definition of the pre increment operator 🤔 taking two terms at a time /*O/P :22 */ /* a = 6+7 //2 terms at a time a = 14 (13+1)+ 8 (7+1)= 22 https://code.sololearn.com/cexnFWwRkI1h/?ref=app
25th May 2019, 5:05 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
I think that it will be 18 because if i = 5 and i is incresing ++I will be a = 6 + 6 + 6
23rd Jun 2019, 10:41 AM
Alexito
Alexito - avatar