Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why this line works with parentheses around (t[i] = getche) != '\r', but doesn't work without?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading