argument promotions in C function calls
I learned from ----As to when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when ther开发者_高级运维e's no prototype or when the argument is variadic.
But an example confusing me is:
void func(char a, char b)
{
printf("a=%p,b=%p\n",&a,&b);
}
int main(void)
{
char a=0x11,b=0x22;
func(a,b);
return 0;
}
It is cleard in the above example: when calling func in main, there is no need to promote the arguments a and b, but the output shows &a = &b +4 not &a = &b+1. If no promotion occured, why 4 bytes between two CHAR argument?
Because the compiler feels like doing it that way :-)
You can't infer that an argument has or hasn't been promoted just by looking at its address. There's no requirement that arguments be passed continguously on the stack (or even that they are passed on a stack at all, for that matter).
The compiler (and calling conventions for your platform) might specify that the stack is always kept 4-byte aligned, but that's an implementation-specific detail, not part of the C language standard.
精彩评论