printf()的用C pre和递减后的功能评价 [英] printf() function evaluation of pre and post decrements in C

查看:117
本文介绍了printf()的用C pre和递减后的功能评价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main()
{
    int i = 2;
    printf("\n %d %d %d \n",--i,i--,i);    //   0 2 2
    return 0;
}

在输出打印 0 2 0 ,而不是 0 2 2 。我不明白,因为我承担该的printf()从右到左评估。

The output prints 0 2 0 and not 0 2 2.I couldn't understand, as I assumed that the printf() evaluates from right to left.

推荐答案

您code展品未指定的行为。按 C99 标准的文档,章节6.5.2.2,第10段:

Your code exhibits Unspecified behaviour. As per c99 standard document, chapter 6.5.2.2, paragraph 10:

功能指示符,实际的参数,实际的参数内SUBEX pressions评价的顺序是不确定的,但有一个序列点的的实际调用。

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.

此外,这表明未定义行为,因为 I 是越来越修改的不止一次。根据第6.5章第2款规定:

Again, this shows undefined behaviour, because, i is getting modified more than once between two sequence points. As per chapter 6.5 paragraph 2:

在previous和下一个序列点之间的对象应具有其存储的值由前pression评价修改最多一次。此外,前一个值是只读,以确定该值将被存储

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.

这篇关于printf()的用C pre和递减后的功能评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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