![]() | The ARM Linux FAQ Signed Character Frequently Asked Questions | ![]() |
| Disclaimer: All information given here is believed to be correct, but is subject to change at any time. However, I assume no responsibility for any errors, omissions, consequences or damages caused by the application or use of the information within this document. The following email fragment appeared on the linux-arm mailing list recently: The above code is actually buggy in that it assumes that the type "char" is equivalent to "signed char". The C standards do say that "char" may either be a "signed char" or "unsigned char" and it is up to the compilers implementation or the platform which is followed.As the poster points out, the above code does not work as expected if "char" is "unsigned". It is difficult to detect this code at compile time, since GCC does not issue any warnings. The only way to detect it is either by visual examination of the code, or by actually running it and finding a problem. This causes problems on ARM based machines since "char" is of the "unsigned" variety, which allows the compiler to generate faster, more efficient code. ARM is not alone in this - SGI Mips running IRIX also encounters this problem. However, dispite the lack of warning for the above case, GCC does warn with the following code: { char foo; foo = bar(); if (foo == -1) { ... } } Code like the above will generate a compiler warning, which will be one of the following depending on the actual test used: warning: comparison is always 0 due to limited range of data type warning: comparison is always 1 due to limited range of data typePlease note however that the above warnings are not issued if "char" is "signed" and therefore can be difficult to pick up when compiling in such an environment. The following table lists the four types of code which cause problems when "c" is declared as just "char", and the most likely correct method of fixing the code.
|
| (C) 2025 Russell King, All rights reserved. | Linux is a trademark of Linus Torvalds in the US and some other countries. ARM and StrongARM are trademarks of ARM Ltd. This site is hosted on a Macchiatobin VM running Linux. Connectivity for this site is provided by Deep Blue Solutions Ltd. |