Why ?
-
:doh: int test = 1 + 2 + 3; +2 + 3 +4; after I compile with Visual studio 6 or .net the answer test is 6 Is there anyone know what is weird ? the compiler will not have any error message, is this kind of bug ? Thanks for advance.
coding is just the beginning
-
:doh: int test = 1 + 2 + 3; +2 + 3 +4; after I compile with Visual studio 6 or .net the answer test is 6 Is there anyone know what is weird ? the compiler will not have any error message, is this kind of bug ? Thanks for advance.
coding is just the beginning
Of course
int test = 1 + 2 + 3;
equals 6.+2 + 3 +4;
is a seperate expression. Remove the first semicolon if you want to connect the two expressions.int test = 1 + 2 + 3 +2 + 3 +4;
will result in test == 15. -
:doh: int test = 1 + 2 + 3; +2 + 3 +4; after I compile with Visual studio 6 or .net the answer test is 6 Is there anyone know what is weird ? the compiler will not have any error message, is this kind of bug ? Thanks for advance.
coding is just the beginning
Sofian Teja wrote:
int test = 1 + 2 + 3;
Evaluates to 6 (according to Adam Riese).
Sofian Teja wrote:
+2 + 3 +4;
Is an empty expression (it does nothing), always switch on your compiler warnings and you will be notified when such things happen accidentally. Related articles: Getting pragmatic with warnings, here and here. Hope it helps.
-
:doh: int test = 1 + 2 + 3; +2 + 3 +4; after I compile with Visual studio 6 or .net the answer test is 6 Is there anyone know what is weird ? the compiler will not have any error message, is this kind of bug ? Thanks for advance.
coding is just the beginning
Any expression can also be a statement. The statement
+2 + 3 +4;
is perfectly legal. It calculates the value and does nothing with it. (Of course, in optimized builds, the statement will be removed since it does nothing and has no side effects.)--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.