views:

45

answers:

1

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 there'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?

+1  A: 

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.

David Gelhar
Which means it wasn't promoted at all in the above example?Could you please tell me why does a lib function such as 'ungetc' declared like this: int ungetc(int c,FILE *stream)?why not char ungetc(char c,FILE *stream)? And almost every func tend to use INT instead of CHAR in their prototype...
HaoCheng
David Gelhar