LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: alvas

i=2,(++i)+(++i)+(++i),最后求结果,在fc4中得13

 关闭 [复制链接]
发表于 2005-11-28 23:46:46 | 显示全部楼层
From "the c programming language“

One unhappy situation is typified by the statement

   a = i++;

The question is whether the subscript is the old value of i or the new. Compilers can interpret this in different ways, and generate different answers depending on their interpretation. The standard intentionally leaves most such matters unspecified. When side effects (assignment to variables) take place within an expression is left to the discretion of the compiler, since the best order depends strongly on machine architecture.
回复 支持 反对

使用道具 举报

发表于 2005-11-28 23:48:06 | 显示全部楼层
我最上面那贴第二句英文来自
practical c programming
回复 支持 反对

使用道具 举报

发表于 2005-11-28 23:52:07 | 显示全部楼层
Post by rickxbx
这样的代码只会出现在考卷上

这就是问题所在
为什么考这些东西
sigh
回复 支持 反对

使用道具 举报

发表于 2005-11-29 00:01:45 | 显示全部楼层
Post by ssbeonly
YAHOO的题就一定没有错误吗?

没有错误
因为人家期待的答案是
undefined

这个题只在研究某个编译器的具体实现时有意义
如果出题人期待的答案不是undefined
那么他还应该提供的信息有

  • 什么CPU
  • 什么操作系统
  • 什么版本
  • 什么编译器
  • 什么编译器版本
  • 编译选项(有否优化,优化级别)
回复 支持 反对

使用道具 举报

发表于 2005-11-29 09:20:36 | 显示全部楼层
Post by zhllg
这就是问题所在
为什么考这些东西
sigh


诚如你所说,考官期待undefined这样的答案,如果按照一个方式慢慢去算,不仅得不到正确答案,也会浪费不少时间,要知道,考场时间可是一秒值千金啊.....
回复 支持 反对

使用道具 举报

发表于 2005-11-29 09:49:31 | 显示全部楼层
呵呵,也是
不过倒很少见到考题的选项里有这一条
回复 支持 反对

使用道具 举报

发表于 2005-12-21 17:21:49 | 显示全部楼层
这种题目就像有人问你:天上有多少星星?你说:1000或10000颗。他告诉你:不,我期待的答案是“数不清”。 一样白痴!!
面试的时候看到这种题目,你可以转身就走!!
回复 支持 反对

使用道具 举报

发表于 2005-12-23 15:22:51 | 显示全部楼层

asm

gcc -v
>gcc -v
Reading specs from C:/MinGW/bin/../lib/gcc-lib/mingw32/3.2.3/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable-languages=c++,f77,objc --disable-win32-registry --disable-shared --enable-sjlj-exceptions
Thread model: win32
gcc version 3.2.3 (mingw special 20030504-1)
>Exit code: 0
gcc -S x.c
>gcc -S x.c
>Exit code: 0
cat x.s
>cat x.s
        .file        "x.c"
.globl _i
        .data
        .align 4
_i:
        .long        2
        .text
.globl _x
        .def        _x;        .scl        2;        .type        32;        .endef
_x:
        pushl        %ebp
        movl        %esp, %ebp
        incl        _i
        incl        _i
        movl        _i, %eax
        addl        _i, %eax
        incl        _i
        addl        %eax, _i
        movl        _i, %eax
        popl        %ebp
        ret
>Exit code: 0
cat x.c
>cat x.c

/** x.c**/

int i =2;
int x(){
i = (++i) + (++i) + (++i);
return i;

}

>Exit code: 0

Please see below ASM code of the (++i) + (++i) + (++i);

                /**i =2 **/
        incl        _i                  ;i = i + 1 = 3
        incl        _i                  ;i = i + 1 = 4
        movl        _i, %eax            ;eax= i = 4
        addl        _i, %eax            ;eax = eax + i = 4 + 4 = 8
        incl        _i                  ;i = i + 1 = 5
        addl        %eax, _i            ;i = eax + i = 8 + 5 = 13
   

see attachment of the source code

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2005-12-23 15:34:33 | 显示全部楼层
考试专用题目...
不如去分析编译器...
回复 支持 反对

使用道具 举报

发表于 2005-12-23 17:49:44 | 显示全部楼层
Post by nicle
这种题目就像有人问你:天上有多少星星?你说:1000或10000颗。他告诉你:不,我期待的答案是“数不清”。 一样白痴!!
面试的时候看到这种题目,你可以转身就走!!



您说的很有趣...意思也很明确...

多多少少的,都会给中国的软件业发展带来浪费..
为什么这么说呢?
就好象你在学英语一样,没有一个好的方法..老师总是会出些选择题给你做,而他们常常告诉你用排除法..4个候选,其中只有一个是正确的,而老师却要把三个错误答案也讲给你听.....毫无疑问,这是在浪费.....为什么不用讲错误答案的时间,仔细讲解正确的答案呢???为什么要告诉你错误的说法???
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表