Home > front end >  why this line works with parentheses around (t[i] = getche) != '\r', but doesn't wor
why this line works with parentheses around (t[i] = getche) != '\r', but doesn't wor

Time:08-01

this line should get input from the user and stop when he presses enter. here is the line:

for (i = 0; (t[i] = getche()) != '\r'; i ); //works

for (i = 0; t[i] = getche() != '\r'; i ); //t is getting gibberish values

CodePudding user response:

Order of operations. != is higher precedence than =. Without parentheses, you get the character, compare it to '\r', and assign either 1 or 0 to t[i] depending on the result of the comparison, rather than assigning the character read as intended (and as you do correctly with the parenthesized version).

  • Related