0

Does 0 = '0' in C++?

int x = 0; if (x == '0') cout << x; cout << x+1; // is this line essentially using an else statement? // outputs 1 I might be overthinking, but do the single quotes make the 'if' condition false? Also, under what circumstances are curly brackets not needed?

28th Jul 2020, 5:23 AM
Solus
Solus - avatar
1 ответ
+ 1
No 0 != '0'. Moreover, in this case an int is being compared with a char, so it's okay becuase char is internally stored as integer. But if you compared 0 with for example, string or a custom type, it would give error. The curly braces are not needed in a if, else if, else , while or a for block, if the block has only 1 statement. For example, in the above code, the if block has only 1 statement "cout << x"
28th Jul 2020, 5:35 AM
XXX
XXX - avatar