About printing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

About printing

Why does the below code print 2 ? https://code.sololearn.com/cKO3S0e9aj57/?ref=app

19th Jul 2020, 5:17 AM
Gauri Shirkande
Gauri Shirkande - avatar
6 Answers
+ 4
As assignment operation is of higher precedence than that of "," thus first *x* is assigned the value 2 then rest of them were read. If you use parentheses"()" to enclose the stuff in brackets like this:- x = (2,3,4,5); Then everything inside the parentheses will execute first(because of higher precedence of parentheses), so the program will go through all the valus and will assign last value(5 in this case) to *x*
19th Jul 2020, 5:25 AM
Arsenic
Arsenic - avatar
+ 2
Thanks...absolutely you cleared everything🙏
19th Jul 2020, 5:36 AM
Gauri Shirkande
Gauri Shirkande - avatar
+ 2
One more question I have in this case is when you declare and define simultaneously i.e int x = 2,3,4,5; It throws error while printing x. What's the reason?
19th Jul 2020, 5:39 AM
Gauri Shirkande
Gauri Shirkande - avatar
+ 2
Gauri Shirkande , this is an interesting one. Here your program is assuming that you are trying to declare another variable of int type but using a constant("3" in this case) as the name thus it is giving out this error.
19th Jul 2020, 5:55 AM
Arsenic
Arsenic - avatar
+ 2
Ohkay got it👍...thanks😁
19th Jul 2020, 6:01 AM
Gauri Shirkande
Gauri Shirkande - avatar
+ 1
Gauri Shirkande your welcome 🙃🙂.
19th Jul 2020, 6:02 AM
Arsenic
Arsenic - avatar