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. Cidr计算

    项目上有个需求,要求计算cidr,网上perl和php,python很多,java的很少,呵呵,虽说懂点那个,毕竟还是会这个. 所以查了一下: https://foo.cs.ucsb.edu/cs56 ...

  2. DELPHI 任务栏无EXE显示

    需要用到的一个函数: LONG SetWindowLong( HWND hWnd, int nIndex, LONG dwNewLong ); program Project; usesForms,  ...

  3. 如何用ATL创建ActiveX控件

    演示截图: 代码简介或代码解析: 如何用ATL创建ActiveX控件 实现了一个ActiveX控件,它在一个圆内部有个正多边形,当用户在多变形内部单击将会使多边形的边数在当前的基础上+1,在多变形外部 ...

  4. Javascript 排序数组或对象

    分享一个用于数组或者对象的排序的函数.该函数可以以任意深度的数组或者对象的值作为排序基数对数组或的元素进行排序. 代码如下: /** * 排序数组或者对象 * by Jinko * date 2015 ...

  5. AndroidUI 视图动画-缩放动画效果 (ScaleAnimation)

    放动画效果,可以使用ScaleAnimation: <Button android:id="@+id/btnScale2" android:layout_width=&quo ...

  6. ostream类的公有成员函数

    1 flush 2 operator<< 3 put 4 seekp 5 tellp 6 write 1 flush 刷新输出流 2 operator<< 插入运算符 3 pu ...

  7. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  8. Nginx基础教程PPT

    Nginx基础教程PPT By 马冬亮(凝霜  Loki) 一个人的战争(http://blog.csdn.net/MDL13412) pdf版本号下载 watermark/2/text/aHR0cD ...

  9. Python学习笔记(十五):类基础

    以Mark Lutz著的<Python学习手册>为教程,每天花1个小时左右时间学习,争取两周完成. --- 写在前面的话 2013-7-24 23:59 学习笔记 1,Python中的大多 ...

  10. AngularJs(八) 过滤器filter创建

    大纲 示例 过滤器的使用 创建过滤器 demo 这是整个示例demo 1.filter.js文件 angular.module("exampleApp", []) .constan ...