1. 问题描述

以下代码的输出结果是什么?

题目1:

int i=;
printf("%d, %d\n", i++, ++i);

题目2:

int i = ;
printf("%d, %d, %d, %d, %d\n", i++, ++i, i, i++, i);

2. 解题思路【错误】

  printf参数是从右至左入栈的,故:

  • 题目1的输出为:11,12
  • 题目2的输出为:

3. 反思

  • 注意:该类题目编译器不一样,结果就会不一样,即这种行为依赖编译器!!!不必纠结。
  • 原因分析:
    • C/C++语言没有规定具体压栈顺序,没有标准化时C语言支持没有固定参数的函数,所以为了实现这个当时多数编译器都采用从右往左压栈,但是标准化的要求至少有一个固定参数,这个限制就没有必要了。不过从右到左几乎已经成为了C编译器惯用的顺序。C++的_stdcall方式也是采用从右到左,不同的只是不需要调用者自己手动清栈。
    • 另外求值顺序和压栈顺序是两回事,C语言里几乎没有对求值顺序做规定,编译器完全可以先求出值再决定如何压栈。
    • 简单说,这种问题与编译器实现语言的规定有关。不同编译器下生成的代码可能不同,出现的结果就不同。对于这种可能造成错误的代码,不用纠结。

What's the value of i++ + i++?
It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:

v[i] = i++;

Related example:

f(v[i],i++);

Here, the result is undefined because the order of evaluation of function arguments are undefined.

Having the order of evaluation undefined is claimed to yield better performing code. Compilers could warn about such examples, which are typically subtle bugs (or potential subtle bugs). I'm disappointed that after decades, most compilers still don't warn, leaving that job to specialized, separate, and underused tools.

printf("%d, %d\n", i++, ++i)的输出结果是确定的吗???的更多相关文章

  1. linux下printf函数为什么不加\n就不能输出相关的内容 ?

    转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/53255970 原因:  输出缓冲区的问题. unix上标准输入输出都是带有缓存的, ...

  2. C语言中printf的规范输出

    1.调用格式为  printf("<格式化字符串>", <参量表>);   其中格式化字符串包括两部分内容: 一部分是正常字符, 这些字符将按原样输出; 另 ...

  3. printf格式输出总结

    #include<stdio.h>    #include<string.h>    int main()    {        ];        ;       floa ...

  4. win32程序调试OutputDebugString 类似printf格式化输出

    有没有win32编程因为打印变量调试程序而头疼呢.方法二的函数完全类似printf.非常完美.方法一:不带参数输出如printf("hello world"); OutputDeb ...

  5. 7.20.01 java格式化输出 printf 例子

    java格式化输出 printf 例子 importjava.util.Date; publicclassPrintf { publicstaticvoidmain(String[] args) { ...

  6. shell之 printf 输出语句

    总结: (1)printf 使用引用文本或空格分隔的参数,外面可以在printf中使用格式化字符串,还可以制定字符串的宽度.左右对齐方式等.默认printf不会像 echo 自动添加换行符,我们可以手 ...

  7. php输出语句 echo print printf print_r var_dump sprintf

    php的几种输出方式: echo 常用的输出语句,例如:echo 'helloworld!'; print() 输出语句,有返回值.例如:print('helloworld!'); 输出成功返回1,失 ...

  8. C编程中printf不加'\n'不输出

    有时,使用printf("a=%d",a);并不一定会输出显示,只有当在格式化输出时加上'\n'才能输出,如printf("a=%d\n",a); 由于unix ...

  9. Java面向对象 第2节 Scanner 类和格式化输出printf

    §Scanner 类 java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入. 1.创建 Scanner 对象的基本语法:Scanner s = ...

随机推荐

  1. 设置windows窗口半透明(使用SetLayeredWindowAttributes API函数)

    所需函数原型:BOOL WINAPI SetLayeredWindowAttributes(HWND hWnd,  COLORREFcrKey,  BYTE bAlpha,  DWORD flag); ...

  2. docker 容器管理常用命令

    Docker 容器管理: docker create -it centos //这样可以创建一个容器,但该容器并没有启动: create Create a new container 创建一个容器: ...

  3. 在JavaScript的数组中进行数组元素查找和替换(JS的indexOf等)

    <html> <head> <title> Extend JavaScript Array Method </title> <script lan ...

  4. mysql中多个字段共同确定唯一性

    create table tbl_table ( id integer not null auto_increment, fname varchar(255), lname varchar(255), ...

  5. MAX Average Problem(斜率优化dp)

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. 精通CSS+DIV基础总结(一)

    这段时间学习了玩了DIV+CSS的视频,感觉效率不高.前边的Javascript总结的不好,但是看了后边的JQuery,觉得学习的再多一点,再进行Javascript的总结.DIV+CSS总结,估计会 ...

  7. DevExpress]ChartControl 创建Drill-Down样式的Title

    关键代码: /// <summary> /// 创建Drill-Down样式的Title /// </summary> /// <param name="cha ...

  8. sql中在查询语句中加判断,控制输出的内容

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  9. new到底做了什么?

    下面是一个实例化自定义的对象,我们将要对他进行分析 //定义构造函数 function A(){ this.b = 1 //在这个对象里增加一个属性 //不可以拥有返回对象的return语句 } va ...

  10. setter设置器 gutter访问器

    set方法书写规范: 1.必须以set开头,set后跟去掉下划线的实例变量并且首字母大写.ps: setAge:2.一定有参数3.不能有返回值4.一定是对象方法(-开头)5.形参一般是去掉下划线的实例 ...