On x86-64 Linux, at least it is defined by x86-64 System V psABI
Other platforms will have similar ABI standards documents that define rules that allow different C compilers to coordinate with each other when invoking conventions, structural layouts, and the like. (See x86 wiki tags for links to other x86 ABI documents or other places for other architectures. Most architectures other than x86 have only one or two standard ABIs.)
From x86-64 SysV ABI: Figure 3.1: Scalar Types
C sizeof Alignment AMD64 (bytes) Architecture _Bool* 1 1 boolean ----------------------------------------------------------- char 1 1 signed byte signed char --------------------------------------------------------- unsigned char 1 1 unsigned byte ---------------------------------------------------------- ... ----------------------------------------------------------- int 4 4 signed fourbyte signed int enum*** ----------------------------------------------------------- unsigned int 4 4 unsigned fourbyte -------------------------------------------------------------- ...
* This type is called bool in C ++.
*** C ++ and some implementations of C permissions allow more than internal. The base type is superimposed on unsigned int, long int or unsigned long int in that order.
Regardless of whether the char signed or not, it actually directly affects the calling convention in this case due to the currently undocumented requirement that clang relies on: narrow types are characters or zeros - expanded to 32 bits when transmitted as args functions , according to the prototype of the called party.
So for int foo(char c) { return c; } int foo(char c) { return c; } , clang will rely on the caller to have an extended arg sign. ( code + asm for this and calling on Godbolt ).
gcc: movsx eax, dil # sign-extend low byte of first arg reg into eax ret clang: mov eax, edi # copy whole 32-bit reg ret
Even despite the calling convention, C compilers must agree, so they compile inline functions in .h in the same way.
If (int)(char)x behaved differently in different compilers for the same platform, they would not be truly compatible.
Peter Cordes Sep 28 '17 at 14:09 on 2017-09-28 14:09
source share