c - 当a=1时a,a++,++a的输出是多少;

标签 c

任何人都可以向我解释以下代码输出背后的正确逻辑吗?

 #include <stdio.h>
 int main()
    {
            int a=1;
            printf("%d\t%d\t%d\n",a,a++,++a);
            return 0;
   }

预期输出为 3 2 3

最佳答案

您的代码是undefined behavior ,所以它不存在“正确的逻辑”——它可以输出 0 0 0, 1 1 2, hello, world,或发出 nasal demons .

来自C99 standard :

  • 第 6.5 节第 2 段:

    Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

  • 第 6.5.2.2 节第 10 段:

    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.

关于c - 当a=1时a,a++,++a的输出是多少;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344634/

相关文章:

c - 如何将结构属性作为参数传递

c - 有没有办法转储 C 结构?

c - pjsip pj_timer_heap_schedule 崩溃

c++ - 在条件表达式中使用时,gcc 或其他编译器是否自动将按位或转换为 bool 值或?

cmake无法构建简单的项目

c - Xcode 上的 arc4random C 版本

c - 我的真实路径对文件返回 null

c - #define FOO 1u 2u 4u ... 1u 和 2u 是什么意思?

c - 如何创建一个程序,能够从用户处获取最多 1000 位的两个数字,然后打印结果?

c# - 使用 PInvoke char** (C -> C#) 更正返回值