C语言gcc编译器中预增和后增的意外行为 [英] unexpected behaviour of pre and post increment in c language gcc compiler

查看:173
本文介绍了C语言gcc编译器中预增和后增的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果n的值为5,则输出:

If n has the value 5 then output:

printf("%d %d", n++, ++n); //should be 5 and 7

但是我得到的是输出6和7.

But I get as output 6 and 7.

推荐答案

对此类未定义行为进行了多次未排序的修改.如果您搜索大量结果,例如此问题.

Multiple unsequenced modifications result to such kind of Undefined Behavior. There are tons of results if you search for it, e.g. this question.

下次在启用所有警告的情况下进行编译,如下所示:

Next time compile with all warnings enabled, like this:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c 
main.c:6:22: warning: multiple unsequenced modifications to 'n' [-Wunsequenced]
    printf("%d %d", n++, ++n); 
                     ^   ~~

这篇关于C语言gcc编译器中预增和后增的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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