(c = getchar())

Walter Bright bright at Data-IO.COM
Thu Dec 15 05:09:09 AEST 1988


In article <45472 at yale-celray.yale.UUCP> wald-david at CS.YALE.EDU (david wald) writes:
>Doesn't the expression (c = getchar()) have type (int) regardless of the
>type of c?  Or will c being a char really prevent this comparison?

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.



More information about the Comp.lang.c mailing list