奇怪的printf的行为? [英] Strange printf behaviour?

查看:129
本文介绍了奇怪的printf的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  未定义行为和顺序点


  [CPRG] $猫test.c的
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
        INT I = 10;
        输出(I =%d个\\妮++ =%d个\\ n + i =%d个\\ N,我,我++ ++ I)
        返回0;
}
[CPRG] $使
GCC -g -Wall -o测​​试test.c的
test.c的:在函数'主':
test.c的:7:警告:操作上的'我'可能是不确定的
test.c的:7:警告:操作上的'我'可能是不确定的
[CPRG] $ ./test
我= 12
我++ = 11
++我= 12

我不知道为什么这件事情正在发生。请任何人都可以
解释我详细为这里发生了什么?


解决方案

C没有定义的顺序函数调用的参数得到评估。你是在自找麻烦那里;)

更新:结果
为了澄清是指什么,什么不是:


  

功能指示符,实际的论点评估的秩序,
  实际的参数内SUBEX pressions是不确定的,但有一个序列点
   前实际调用。


从ISO / IEC 9899:1999,第6.5.2.2节,函数调用

Possible Duplicate:
Undefined Behavior and Sequence Points

[cprg]$ cat test.c
#include  <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
        int i=10;
        printf("i=%d\ni++=%d\n++i=%d\n",i,i++,++i);
        return 0;
}
[cprg]$ make
gcc -g -Wall -o test test.c
test.c: In function ‘main’:
test.c:7: warning: operation on ‘i’ may be undefined
test.c:7: warning: operation on ‘i’ may be undefined
[cprg]$ ./test
i=12
i++=11
++i=12

I have no idea why this thing is happening. Please can anyone explain me in detail as to what is happening here ?

解决方案

C does not define in which order function call arguments get evaluated. You are in for trouble there ;).

Update:
To clarify what is defined and what not:

The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

From ISO/IEC 9899:1999, Section 6.5.2.2, Function calls

这篇关于奇怪的printf的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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