[net.lang.c] Char vs. Int

jl42@andrew.cmu.edu (Jay Mathew Libove) (09/13/86)

I have always been under the impression that a CHAR variable and an INT variable
were the same in 'C'- yet I now see:

Message-Id: <1219@drutx.UUCP>
Date: 8 Sep 86 02:52:47 GMT
To: info-c@brl-smoke.arpa

In the sequence:

	main()
	{
		char c = 'w';
		foo(c)
	}

	foo(ch)
	char ch;

char ch is the correct declaration.  The compiler/code generator should
handle pulling the character portion of the promoted variable off the
stack correctly.  Declaring it 'int' is asking for trouble.

Why is it asking for trouble to declare ch as INT when K&R says they are the
same?

karl@haddock (09/18/86)

jl42@andrew.cmu.edu writes:
>I have always been under the impression that a CHAR variable and an INT
>variable were the same in 'C'

No, they're not.  An expression of type "char" will be converted to "int"
at the drop of a hat, but they are not identical.  (Neither are arrays and
pointers the same thing, and for much the same reasons.)

Karl W. Z. Heuer (ima!haddock!karl; karl@haddock.isc.com), The Walking Lint

jsdy@hadron.UUCP (Joseph S. D. Yao) (09/23/86)

In article <3770@brl-smoke.ARPA> jl42@andrew.cmu.edu (Jay Mathew Libove) writes:
>I have always been under the impression that a CHAR variable and an INT variable
>were the same in 'C'- yet I now see:
>In the sequence:
> ...
>char ch is the correct declaration.  ...
>		.  Declaring it 'int' is asking for trouble.
>
>Why is it asking for trouble to declare ch as INT when K&R says they are the
>same?

Simple.  It doesn't and they aren't.  K&R 2.2: char is a byte, one
character; int is the machine's natural integer (and examples).
K&R 2.7 mentions chars' automatic conversion, in arithmetic and
arguments, into ints.  (The need for conversion, of course, implies
the difference between the two.)  This is repeated in CRef 2.6,
4., 6.1, 6.6.
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}
			jsdy@hadron.COM (not yet domainised)