Hellp与类型推广 [英] Hellp with type promotion

查看:56
本文介绍了Hellp与类型推广的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我读到表达式中的每个字符都被提升为int。


因为函数参数是表达式所以类型提升应该采用

传递给函数的参数的位置。


所以


*****

int main(void)

{

char c =''C'';

test(c);


返回0;

}


无效测试(char c)

{

printf(" char的大小是%d,sizeof(c));


}

*******

应打印 - char的大小为4 - 因为char c被提升为类型

int。


但它打印1


为什么?


干杯

Vishal


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

推荐答案



ma *** ********@gmail.com 写道:


ma***********@gmail.com wrote:
HI,
我读到表达式中的每个字符都被提升为int。

因为函数参数是表达式所以类型提升应该为传递给函数的参数设置


所以

**** *
int main(无效)
{/ c>'C'';
测试(c);

返回0;
}

void test(char c)
{/> printf(" char of size is%d" sizeof(c));


sizeof - sizeof运算符产生其操作数的大小(以字节为单位),

所以在你的情况下,操作数是c,类型为'' char''

和你的系统字符大小是1,所以当你打印时你得到1!

}
*******

应打印 - char的大小为4 - 因为char c被提升为类型
int。


不是没有


- ravi


但它打印1

为什么?

干杯
Vishal

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









它确实提升为int类型。


如果我们不明确声明函数原型,则函数

参数字符被提升为int。


void test();

int main(void)

{

int n;

char c =''C'';

test(c);


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


返回0;


}

无效test(c)

{

printf(func%d中的大小,sizeof(c));

}

这个打印:

$ f $ b尺寸func 4

尺寸主要1


所以如果我们不会在原型中明确告诉编译器它们被提升的类型

参数。


太棒了!!

干杯

Vishal。

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.


" ma *********** @ gmail.com" <毫安*********** @ gmail.com>写道:
"ma***********@gmail.com" <ma***********@gmail.com> wrote:
我读到表达式中的每个char都被提升为int。


是的。

因为函数参数是表达式所以类型提升应该为传递给函数的参数设置
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.




否。这仅适用于旧式声明,没有

声明的函数或可变参数函数的变量参数。


Richard



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

Richard


这篇关于Hellp与类型推广的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆