关于运营商规模的疑问 [英] doubt regarding sizeof operator
问题描述
大家好,
我不能了解波纹管提到的行为
程序。
#include< stdio.h>
int main(void)
{
printf(" \ n Size =%d \ n",sizeof 1< 0);
返回0;
}
程序的输出是
尺寸= 0
但我的理解是sizeof返回类型的大小。在这个
的情况下,它不应该返回sizeof(int)吗?
但是如果我重写代码如下所述输出是4
#include< stdio.h>
int main(无效)
{
printf(" \ n Size =%d \ n",sizeof(1< 0));
返回0;
}
尺寸= 4
你能帮我解释一下这个问题吗?
问候
Somenath
somenath写道:
>
大家好,
我无法理解提到的波纹管的行为
程序。
#include< stdio.h>
int main(void)
{
printf(" \ n Size =%d \ n",sizeof 1< 0);
返回0;
}
该程序的输出是
尺寸= 0
>
但我的理解是sizeof返回类型的大小。在这个
的情况下,它是否应该返回sizeof(int)?
表达式sizeof 1< 0"是假的,因此是零。 sizeof binds
比''<''更紧,所以你写了(sizeof 1)< 0.
-
Ian Collins。
10月30日,4 44,somenath < somenath ... @ gmail.comwrote:
大家好,
我无法理解这种行为提到的轰鸣声
程序。
#include< stdio.h>
int main(无效)
{
printf(" \ n Size =%d \ n",sizeof 1< 0);
返回0;}
程序的输出是
大小= 0
但我的理解是sizeof返回大小在这个
的情况下,它不应该返回sizeof(int)吗?
但是如果我按照提到的那样重写代码,则输出为4
#include< stdio.h>
int main(无效)
{
printf( " \ n Size =%d \ n",sizeof(1< 0));
返回0;
}
Siz e = 4
能帮助我理解这个问题吗?
问候
Somenath
printf(" \ n Size =%d \ n",sizeof(1< 0)); // sizeof(bool)
printf(" \ n Size =%d \ n",sizeof 1< 0); //等于((sizeof(int))< 0)
当然为零 - -false--
somenath< so ********* @ gmail.comwrites:
我无法理解提到的波纹管的行为
程序。
#include< stdio.h>
int main(无效)
{
printf(" \ n Size =%d \ n",sizeof 1< 0);
返回0;
}
程序的输出是
大小= 0
Ian Collins解释说,这正是应该是什么。
[...]
但如果我按照提到的那样重写代码,则输出为4
#include< stdio.h>
int main(void)
{
printf(" \ n Size =%d \ n",sizeof(1< 0));
返回0;
}
大小= 4
(1< 0)产生int类型的值(带值0),因此sizeof(1 <0)
产生sizeof(int)。显然你的系统上有4个。但是你
使用%d打印该值。格式,对于int类型是正确的,
但不适用于类型size_t。
对于像这样的小值,打印它的合理方法是
将其转换为int:
printf(" \ n Size =%d \ n",(int)sizeof(1< 0) );
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *< http://users.sdsc.edu/ ~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
- Antony Jay和Jonathan Lynn,是部长
Hi All,
I am not able to understand the behavior of the bellow mentioned
program.
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
The output of the program is
Size = 0
But my understanding is sizeof returns the size of the type .In this
case should it not returns sizeof (int) ?
But if I rewrite the code as mentioned bellow the output is 4
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof (1<0));
return 0;
}
Size = 4
Could you please help me to understand the problem?
Regards
Somenath
somenath wrote:>
Hi All,
I am not able to understand the behavior of the bellow mentioned
program.
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
The output of the program is
Size = 0
But my understanding is sizeof returns the size of the type .In this
case should it not returns sizeof (int) ?
The expression "sizeof 1<0" is false, hence the zero. sizeof binds
tighter than ''<'', so you have written (sizeof 1) < 0.
--
Ian Collins.
On 10 30 , 4 44 , somenath <somenath...@gmail.comwrote:Hi All,
I am not able to understand the behavior of the bellow mentioned
program.
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;}
The output of the program is
Size = 0
But my understanding is sizeof returns the size of the type .In this
case should it not returns sizeof (int) ?
But if I rewrite the code as mentioned bellow the output is 4
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof (1<0));
return 0;
}
Size = 4
Could you please help me to understand the problem?
Regards
Somenathprintf("\n Size = %d \n",sizeof (1<0));//sizeof(bool)
printf("\n Size = %d \n",sizeof 1<0);// equal to ((sizeof (int))<0)
of course zero --false--
somenath <so*********@gmail.comwrites:I am not able to understand the behavior of the bellow mentioned
program.
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof 1<0);
return 0;
}
The output of the program is
Size = 0Which is exactly what it should be, as Ian Collins explained.
[...]
But if I rewrite the code as mentioned bellow the output is 4
#include<stdio.h>
int main(void)
{
printf("\n Size = %d \n",sizeof (1<0));
return 0;
}
Size = 4(1<0) yields a value of type int (with the value 0), so sizeof (1<0)
yields sizeof(int). Apparently that''s 4 on your system. But you
print the value using a "%d" format, which is correct for type int,
but not for type size_t.
For a small value like this, a reasonable way to print it is to
convert it to int:
printf("\n Size = %d \n",(int)sizeof (1<0));
--
Keith Thompson (The_Other_Keith) ks***@mib.org <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
这篇关于关于运营商规模的疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!