sizeof运营商 [英] sizeof operator

查看:74
本文介绍了sizeof运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在K& R页204中读到,char上使用的sizeof返回1.但是当我写下

以下我得到4!


int main(void){

printf("%d \ n",sizeof(''g''));


}


它不应该返回1吗?

解决方案

" JS" < DSA @ asdf.com>。写道:

我在K& R页面204中读到,在char上使用sizeof返回1.但是当我写下
时,我得到4!

printf("%d \ n",sizeof(''g''));




在C中,字符常量的类型为int ',所以sizeof''g''相等

到sizeof(int)。

-

Ben Pfaff

电子邮件: bl*@cs.stanford.edu

web: http://benpfaff.org




Ben Pfaff < bl*@cs.stanford.edu> skrev i en meddelelse

news:87 ************ @ benpfaff.org ...

" JS" < DSA @ asdf.com>。写道:

我在K& R页面204中读到char上使用sizeof返回1.但是当我
写下以后我得到4!

printf("%d \ n",sizeof(''g''));



在C中,字符常量的类型为int,所以sizeof ''g''与sizeof(int)相等。



如果他们的意思是int,他们为什么写char?

" JS" < DSA @ asdf.com>。在消息新闻中写道:d1 ********** @ news.net.uni-c.dk ...

我在K& R页面204中读到了大小用于char返回1.但是当我写下面的内容时,我得到4个!

int main(void){
printf("%d \ n",sizeof(') 'g''));

}

它不应该返回1吗?




不,单引号中的字符,如''g'',是C中类型的常量

int(我认为这在C ++中是不同的)。


尝试:


int main(无效){

char c;

printf("%d \ n",(int)sizeof c);

返回0;

}


Alex


I read in K&R page 204 that sizeof use on a char returns 1. But when I write
the following I get 4!

int main(void){
printf("%d\n",sizeof(''g''));

}

Was it not supposed to return 1?

解决方案

"JS" <dsa.@asdf.com> writes:

I read in K&R page 204 that sizeof use on a char returns 1. But when I write
the following I get 4!

printf("%d\n",sizeof(''g''));



In C, character constants have type `int'', so sizeof ''g'' is equal
to sizeof(int).
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org



"Ben Pfaff" <bl*@cs.stanford.edu> skrev i en meddelelse
news:87************@benpfaff.org...

"JS" <dsa.@asdf.com> writes:

I read in K&R page 204 that sizeof use on a char returns 1. But when I write the following I get 4!

printf("%d\n",sizeof(''g''));



In C, character constants have type `int'', so sizeof ''g'' is equal
to sizeof(int).


Why do they write char if they mean int?


"JS" <dsa.@asdf.com> wrote in message news:d1**********@news.net.uni-c.dk...

I read in K&R page 204 that sizeof use on a char returns 1. But when I
write the following I get 4!

int main(void){
printf("%d\n",sizeof(''g''));

}

Was it not supposed to return 1?



No, a character in single quotes, as in ''g'', is a constant of type signed
int in C (I think this is different in C++).

Try:

int main(void) {
char c;
printf("%d\n", (int)sizeof c);
return 0;
}

Alex


这篇关于sizeof运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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