当前位置: 代码迷 >> C语言 >> 高手请进
  详细解决方案

高手请进

热度:244   发布时间:2008-04-04 14:38:33.0
高手请进
#include<stdio.h>
int main()
{
    int i=3,j;
    j=sizeof(++i+ ++i);
    printf("%d%d",i,j);
}
为什么i的值会是3 ?
搜索更多相关的解决方案: int  main  include  sizeof  

----------------解决方案--------------------------------------------------------
sizeof 运算符
----------------解决方案--------------------------------------------------------
sizeof不是一个函数,只是在预编译阶段就得到值了。
程序运行时候是不执行的
所以i是没有自加的
----------------解决方案--------------------------------------------------------
我觉得还是有点不对 有没有资料发来看看啊?
----------------解决方案--------------------------------------------------------
我认同3#的说法!!
----------------解决方案--------------------------------------------------------
我一会发个关于sizeof的帖子,比较长,耐心看看吧
----------------解决方案--------------------------------------------------------
http://bbs.bccn.net/viewthread.php?tid=207147&extra=page%3D1&frombbs=1
----------------解决方案--------------------------------------------------------
首先看一MSDN上如何对sizeof进行定义的:

sizeof Operator

sizeof expression

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type
(including aggregate types). This keyword returns a value of type size_t.

The expression is either an identifier or a type-cast expression (a type specifier enclosed in
parentheses).

When applied to a structure type or variable, sizeof returns the actual size, which may include
padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof
returns the size of the entire array. The sizeof operator cannot return the size of dynamically
allocated arrays or external arrays.

sizeof()是运算符而不是函数,它只认括号里面变量,结构体等的类型的整体大小(具体用法可参看7#).而不在里面进行运算.所以你sizeof(i);sizeof(++i);sizeof(--i)的结果都是一样的.
----------------解决方案--------------------------------------------------------
我又看了一遍我发的帖子,好像也没提到这里需要的知识点
我在这里解释下吧
预编译器在遇到下面这句:
j=sizeof(++i+ ++i);
时是这么处理的,先把sizeof((++i+ ++i)替换成一个数,现在的32位系统一般是4
也就是替换为j=4;
之后才会把处理权交给编译器,所以编译器看到的就是及j=4;
所以i不会自加
----------------解决方案--------------------------------------------------------
sizeof是测试变量占的空间,++I并不是说I的值就变了。如果是I=++I的话,那么打印出来I的值就是4了。
----------------解决方案--------------------------------------------------------
  相关解决方案