Why does the following code print 2 instead 5? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does the following code print 2 instead 5?

A part of Code: int x; x = 2, 3, 4, 5; printf("%d", x);

24th Jan 2020, 9:49 AM
Subha Basak
Subha Basak - avatar
1 Answer
+ 4
You can string up several operations with commas, but then you can't control in which order they're executed, so normally you'd use semicolons instead. Also, you can just write any literal for a value into your code, like 42, without anything else - it just won't do anything. So this is somewhat like: x = 2; 3; 4; 5; Lines 2 to 4 become meaningless statements. If you do it like this, you'll get a warning for ineffective statements though.
24th Jan 2020, 10:30 AM
HonFu
HonFu - avatar