zhu qun ying writes:
> I am in the progress of porting a program to StrongARM, and has encounter the
> following error. This simple error give us a big headache to trace it down.
>
> consider this simple program:
> int
> main(void)
> {
> char i = -1;
> printf("%d\n", i);
> return 0;
> }
>
> The print out is 255 in stead of -1, unless I define i as
> signed char i;
> then I get the "-1" print out.
Your code is buggy. I am trying to get a warning put into GCC for this type of
thing. However, the GCC mailing address seems to be rather non-responsive.
GCC will complain about things like:
{
char foo;
foo = bar();
if (foo == -1) {
...
}
}
Things to generally look out for in programs (in the following fragments, c is
declared as "char"):
Code Correction
---- ----------
1. c = getopt() c should be declared as "int"
2. c = getc() c should be declared as "int"
3. c == EOF c should be declared as "int"
4. c < 0 c should be declared as "signed char" or "int"
1) getopt() is defined as returning an "int" not a "char"
2) getc() is defined as returning an "int" not a "char"
3) EOF is a "negative integral constant"
4) this is ambiguous and examination of the surrounding code should indicate which
case is more correct. However, "int" is the perferred, unless the code is
relying on some characteristic of "signed char".
Nos 1, 2 and 3 can cause problems even in a signed char environment. Take the
instance of a file containing a byte value 0xff.
I'll put this up as a FAQ...
> I know the compiler is free to implement char as signed or unsigned or "pseudo
> unsigned" according to the standard.
Replace "the compiler" with "the compiler for a particular platform".
> But as gcc is treating char as signed char for other platforms, I wonder is
> this a special treatment for StronARM?
It generates faster, more efficient code.
> Is it a bug in GCC?
No.
> Or my assumsion of gcc will treat char as signed for all platform is
> simply wrong.
Yes.
> Should it behaves consistently across platforms?
No.
_____
|_____| ------------------------------------------------- ---+---+-
| | Russell King [EMAIL PROTECTED] --- ---
| | | | http://www.arm.linux.org.uk/ / / |
| +-+-+ --- -+-
/ | THE developer of ARM Linux |+| /|\
/ | | | --- |
+-+-+ ------------------------------------------------- /\\\ |
unsubscribe: body of `unsubscribe linux-arm' to [EMAIL PROTECTED]
++ Please use [EMAIL PROTECTED] for ++
++ kernel-related discussions. ++
Re: GCC bug? feature? char is unsigned
Russell King - ARM Linux Admin Fri, 12 May 2000 06:16:32 -0700
- GCC bug? feature? char is unsigned zhu qun ying
- Re: GCC bug? feature? char is unsigned Erik Mouw
- Re: GCC bug? feature? char is unsi... zhu qun ying
- Re: GCC bug? feature? char is unsigned Russell King - ARM Linux Admin
- Re: GCC bug? feature? char is unsigned Paul Koning
- Re: GCC bug? feature? char is unsigned Philip Blundell
- Re: GCC bug? feature? char is unsi... Kira Brown
- Re: GCC bug? feature? char is ... Philip Blundell
