Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Hellp with type promotion

Reply
Thread Tools

Hellp with type promotion

 
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      02-28-2006
HI,
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

Why??

Cheers
Vishal

 
Reply With Quote
 
 
 
 
Ravi Uday
Guest
Posts: n/a
 
      02-28-2006


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> HI,
> 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));


sizeof - The sizeof operator yields the size (in bytes) of its operand,
so in your case the operand is c which is of type 'char'
and on your system size of char is 1 so you get 1 when you print that !

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


No it doesnt

- ravi


>
> But it prints 1
>
> Why??
>
> Cheers
> Vishal
>


 
Reply With Quote
 
 
 
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      02-28-2006
Hi,

it does promote to int type.

If we dont explicitly declare the function prototype, the function
argument character is promoted to int.



void test();
int main(void)
{


int n;
char c = 'C';
test(c);

printf("\nsize in main %d",sizeof(c));

return 0;

}
void test(c)
{
printf("size in func %d",sizeof(c));
}


This prints:

size in func 4
size in main 1

So if we dont tell compiler explicitly in the prototype the type of
arguments they are promoted.

Great!!

Cheers
Vishal.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      02-28-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

> I read that every char in expression is promoted to int.


Yes.

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


No. This is only true for old-style declarations, a function without a
declaration, or the variable arguments of a variadic function.

Richard
 
Reply With Quote
 
Robin Haigh
Guest
Posts: n/a
 
      02-28-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi,
>
> it does promote to int type.
>
> If we dont explicitly declare the function prototype, the function
> argument character is promoted to int.
>
>
>
> void test();
> int main(void)
> {
>
>
> int n;
> char c = 'C';
> test(c);
>
> printf("\nsize in main %d",sizeof(c));
>
> return 0;
>
> }
> void test(c)
> {
> printf("size in func %d",sizeof(c));
> }
>
>
> This prints:
>
> size in func 4
> size in main 1
>
> So if we dont tell compiler explicitly in the prototype the type of
> arguments they are promoted.



But here's the odd thing. If you change the call

test(c);

to

test();

so that you don't pass an argument at all, you still get the same output

--
RSH



 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-28-2006
(E-Mail Removed) wrote:
> HI,
> 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
>
> Why??
>


The real question is why you either
1) have your diagnostics turned off or
2) are ignoring them.
These are the diagnostics gcc produces for your code:

a.c: In function 'main':
a.c:4: warning: implicit declaration of function 'test'
a.c: At top level:
a.c:10: warning: conflicting types for 'test'
a.c:4: warning: previous implicit declaration of 'test' was here
a.c: In function 'test':
a.c:11: warning: implicit declaration of function 'printf'
a.c:11: warning: incompatible implicit declaration of built-in function
'printf'
a.c:11: warning: format '%d' expects type 'int', but argument 2 has type
'long unsigned int'
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-28-2006
"Robin Haigh" <(E-Mail Removed)> wrote in news:du1qg6$7jd$1
@newsg3.svr.pol.co.uk:

>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...


>> void test();

....

>> void test(c)
>> {
>> printf("size in func %d",sizeof(c));
>> }
>>

....
> But here's the odd thing. If you change the call
>
> test(c);
>
> to
>
> test();
>
> so that you don't pass an argument at all, you still get the same
> output


How is that odd? (Except that the code looks like it is from the 80s).

You have circumvented the prototype system. When func is called, it is
set up to receive one int argument. The value might be crap, but that
doesn't really matter because sizeof does not evaluate its operand.

Of course, IMHO, anyone who writes code like this in this day and age
deserves whatever they get.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-28-2006
(E-Mail Removed) wrote:

> int main(void)
> {
> char c = 'C';
> test(c);
>
> return 0;
> }
>
> void test(char c)
> {
> printf("Size of char is %d",sizeof(c));
>
> }


1. This code is not legal. You don't have a declaration for test, and
the definition is different than the default declaration you would get
with a pre-C99 compiler. Also, printf() is a variadic function a
requires a prototype. The best way is to include <stdio.h>

2. Even if there were a legal implicit declaration or an old-style
declaration, that wouldn't matter. Except for variable-length arrays,
sizeof is performed at compile time. What the arguments get promoted to
at run-time have no bearing on what's printed there. It will be
sizeof(char), which is guaranteed to be 1, always.



Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-28-2006
(E-Mail Removed) wrote:

> Hi,
>
> it does promote to int type.
>
> If we dont explicitly declare the function prototype, the function
> argument character is promoted to int.
>
>
>
> void test();
> int main(void)
> {
>
>
> int n;
> char c = 'C';
> test(c);
>
> printf("\nsize in main %d",sizeof(c));
>
> return 0;
>
> }
> void test(c)
> {
> printf("size in func %d",sizeof(c));
> }
>
>
> This prints:
>
> size in func 4
> size in main 1
>
> So if we dont tell compiler explicitly in the prototype the type of
> arguments they are promoted.


Has nothing to do with promotion. You didn't give c a type in the
definition of test(). Therefore the implementation provided one for
you. The equivalent definition was:

void test(c)
int c;
{
}




So you got sizeof(int).



Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-28-2006
"(E-Mail Removed)" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hellp on home nextworking j Wireless Networking 1 08-15-2006 09:07 PM
hellp about local timestamp Zhaohui C++ 3 12-07-2005 10:59 AM
Data Recovery Book and Online Business Promotion, Products Sales Promotion, Search Engine Optimization and Online Data Recovery Training services Author Tarun Tyagi Cisco 0 12-15-2004 01:52 AM
The promotion code "MSUU4C8E3475" is not a valid promotion code Sam-Hong Kong MCDST 2 03-04-2004 06:47 PM
Hellp Recover Outlook Express 6.0 Folder/Messages deleted with Shift-Del Arawak Computer Support 1 09-26-2003 05:43 PM



Advertisments