C | Operators | Question 23

Predict the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

# include <stdio.h>
int main()
{
    int x = 10;
    int y = 20;
    x += y += 10;
    printf (" %d %d", x, y);
    return 0;
}

chevron_right


(A) 40 20
(B) 40 30
(C) 30 30
(D) 30 40


Answer: (B)

Explanation: The main statement in question is “x += y += 10”. Since there are two += operators in the statement, associativity comes into the picture. Associativity of compound assignment operators is right to left, so the expression is evaluated as x += (y += 10).

Quiz of this Question

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.