Re: Hellp with type promotion



"manochavishal@xxxxxxxxx" <manochavishal@xxxxxxxxx> writes:
I read that every char in expression is promoted to int.

So as function arguments are expressions so type promotion should take
place for arguments passed to function.

so

*****
int main(void)
{
char c = 'C';
test(c);

return 0;
}

void test(char c)
{
printf("Size of char is %d",sizeof(c));

}
*******


should print -Size of char is 4 - as the char c is promoted to type
int.

But it prints 1

There are several problems with your program:
Calling test() with no visible declaration.
Missing "#include <stdio.h>".
No newline at the end of your output.
Incompatible type for "%d" format (it expects an int; you gave it
a size_t).

Leaving that aside, the parameter "c" within your "test" function is
effectively a local variable of type char. sizeof(c) yields the size
of that variable, which is 1 byte by definition.

There are a number of rules about when integer promotions and default
argument promotions are applied (and I actually don't know all the
rules myself). But applying sizeof to a variable doesn't involve any
promotions; it simply yields the size of the variable.

The fact that the variable happens to be a parameter is irrelevant.
On the function call, the value of the argument (the variable "c" in
main) is used to initialize the parameter object (the parameter "c" in
test(); this would be easier if they had different names). But the
value assigned to it doesn't affect its size.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Strange behaviour
    ... pass it a char. ... the integer promotions are performed on each argument, ... a char is a type of int. ... char is an integer type; ...
    (comp.lang.c)
  • Re: Strange behaviour
    ... pass it a char. ... If any argument is not the correct type for the corresponding ... the integer promotions are performed on each argument, ... a char is a type of int. ...
    (comp.lang.c)
  • Re: Strange behaviour
    ... Undefined behaviour -- %X expects an unsigned int, ... pass it a char. ... An int is passed. ... the integer promotions are performed on each argument, ...
    (comp.lang.c)
  • Re: Function returning a char
    ... a char operand is subject to the "integer ... rarely to unsigned int values). ... "The integer promotions are applied only: as part of the usual arithmetic ... conversions, to certain argument expressions, to the operands of the unary +, -, ...
    (comp.lang.c)
  • Re: Strange behaviour
    ... conversion specification, the behaviour is undefined. ... specifier applies to a signed char or unsigned char ... to the integer promotions, but its value shall be converted to ... the argument is always an int (assuming we are talking ...
    (comp.lang.c)