(c = getchar())

Andrew Koenig ark at alice.UUCP
Thu Dec 15 13:16:40 AEST 1988


In article <1786 at dataio.Data-IO.COM>, bright at Data-IO.COM (Walter Bright) writes:

-> If c is of type char, then:
-> 	(c = getchar())
-> is equivalent to:
-> 	(int)(c = (char) getchar())
-> What's happening is that the rvalue (getchar()) is cast to be the same
-> type as the lvalue (c). The value of the assignment expression is the
-> value of (c) after the integral promotions are performed, i.e. after (c)
-> is converted to an int. Thus, the int result of getchar() was truncated to
-> be a char and then promoted back to an int, in the process losing anything
-> that was in the bits beyond the char.

Indeed.

At least that's what's supposed to happen.  Sometimes other things
happen, though.  See page 70 of ``C Traps and Pitfalls'' for details.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list