sizeof运营商 [英] sizeof operator
问题描述
我在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屋!