Re: Hellp with type promotion
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 28 Feb 2006 21:04:47 GMT
"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.
.
- References:
- Hellp with type promotion
- From: manochavishal@xxxxxxxxx
- Hellp with type promotion
- Prev by Date: Re: Help in c pointers
- Next by Date: Re: Typedef or not
- Previous by thread: Re: Hellp with type promotion
- Next by thread: please explain clearly what actually happens after i click on COMPILE
- Index(es):
Relevant Pages
|