C的怪异的行为GCC编译器和Turbo [英] A weird behavior of C compilers GCC and Turbo

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

问题描述

我已经通过其他类似的问题了,而是试图了解我面临的形势。

I've gone through other similar questions, but trying to understand the situation I'm facing.

所以,这里是我的两个线C code。

So, here's my two line C code.

int i=0;
printf("%d %d %d %d %d",i++,i--,++i,--i,i);

这是我从GCC获取和输出的Turbo C编译器。

And here are the outputs I get from GCC and Turbo C Compiler.

输出:

-1 0 0 0 0

的Turbo C

输出:

-1 0 0 -1 0

我试过各种有独立pre-递增运算符实验,两种编译器的工作相似,但是当我使用上述的printf 语句,输出不同。

我知道的Turbo C是古老的编译器和现在已经过时和不标准,但仍然不能得到一个想法有什么不对上述code。

I know that Turbo C is age-old compiler and now obsolete and non-standard but still can't get an idea what's wrong with above code.

推荐答案

这是不确定的行为,你们都读取和修改 I 多次没有顺序点。 (该在函数参数列表不是一个顺序点,和函数参数评价的顺序没有任何限定。)

It's undefined behavior, you're both reading and modifying i multiple times without a sequence point. (The , in the function parameter list is not a sequence point, and the order of evaluation of function arguments is not defined either.)

编译器可以为所欲为在这种情况下输出。不这样做。

The compiler can output whatever it wants in this situation. Don't do that.

查找其他类似问题一大堆被搜索这个网站的 [ J]未定义行为 。这是相当有启发。

Find a whole host of other similar issues by search this site for [C] undefined behavior. It's quite enlightening.

这篇关于C的怪异的行为GCC编译器和Turbo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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