关于sizeof运营商 [英] Regarding sizeof Operator

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

问题描述

#include< stdio.h>

main()

{

int x = 10,y;

y = sizeof(++ x);


printf(" x =%d\\\
y =%d \ n",x,y);

}


输出量


x = 10

y = 4


为什么不是x = 11

#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11

推荐答案

sonu说:
sonu said:
#include< stdio.h>
main()
{x /> int x = 10,y;
y = sizeof(++ x);

printf(" x =%d\\\
y =%d \ n",x,y);
}
输出

x = 10
为什么不是x = 11
#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11




因为sizeof不评估它的操作数。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



Because sizeof does not evaluate its operand.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


2006年3月14日星期二07:10, sonu认为(

< 11 ********************** @ e56g2000cwe.googlegroups .com>):
On Tuesday 14 March 2006 07:10, sonu opined (in
<11**********************@e56g2000cwe.googlegroups .com>):
#include< stdio.h>
main()
{x / 10,y;
y = sizeof(++ x) ;

printf(" x =%d\\\
y =%d \ n",x,y);
}

输出放置/>
x = 10
y = 4

为什么不是x = 11
#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11




不要发布相同的每隔6分钟提出一次问题,特别是不与

不同的主题行(这使得很难忽略)。假设至少24小时后没有看到你的帖子,这只是

合理。

这不是聊天室,这是Usenet。查看它是什么,它是历史上的b
。 GIYF。


无论如何,你很快得到了两份回复。


-

BR,弗拉基米尔


拉丁语是一种语言,

尽可能死。

首先它杀死了罗马人,

现在它正在扼杀我。



Do not post the same question every 6 minutes, especially not with the
different subject line (it makes it difficult to ignore). It is only
reasonable to assume your post is not seen after at least 24 hours.
This is not a chat room, this is Usenet. Look up what it is and it''s
history. GIYF.

In any case, you did get two replies very quickly.

--
BR, Vladimir

Latin is a language,
As dead as can be.
First it killed the Romans,
And now it''s killing me.


2006-03-14,sonu< sa ******* *********@gmail.com>写道:
On 2006-03-14, sonu <sa****************@gmail.com> wrote:
#include< stdio.h>
main()
{x> 10,y;
y = sizeof(++ x);

printf(" x =%d\\\
y =%d \ n",x,y);
}



x = 10
y = 4

为什么不是x = 11
#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11




" sizeof" ;运算符不会评估其中包含的表达式

似乎。我不知道这一点:但纯粹是因为我从未在其中插入

表达式,原因不明 - 我只是假设它是一个

预处理器。打字的东西,从来没有做过。我可以宣称没有天才。


如果你把它放在一边


sizeof(x = 3);


我同意这有点令人困惑,因为

编译器(gcc)很乐意让你写一些像


sizeof( x = funcCall(y))也好!


在n1124pdf中查看:


6.5.3.4 sizeof运算符

约束


2 sizeof运算符产生其操作数的大小(以字节为单位),其中
可能是一个表达式或带括号的a的名称类型。根据操作数的类型确定大小

。结果是整数。如果

操作数的类型是可变长度数组类型,则计算操作数

;否则,操作数不会被评估,结果

是....


有一个相当无用的脚注说明

" sizeof运算符的操作数通常不会被评估


希望有人会启发我们两个标准的含义

:我只是假设它是一个预处理器类型的东西,并且永远不会认为编译器会在那里允许这样的声明。



"sizeof" operator does not evaluate the expression contained therein
it seems. I didnt know that : but purely because I never stuck an
expression in it for reasons unknown - I just assumed it as a
"preprocessor" type thing and just never did it. I can claim no genius.

It would be the same had you put

sizeof(x=3);

I would agree that it is a little confusing since the
compiler (gcc) happily lets you write something like

sizeof(x=funcCall(y)) too!

Looking at this in n1124pdf:

6.5.3.4 The sizeof operator
Constraints

2 The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is ....

There is a rather unhelpful footnote which states

"the operand of a sizeof operator is usually not evaluated"

Hopefully someone will enlighten us both as to what the standard means
: I just assumed it was a pre-processor type thing and would never
have thought the compiler would allow such a statement there.


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

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