为SUM +++我用C不确定的行为? [英] Is sum+++i undefined behavior in C?

查看:171
本文介绍了为SUM +++我用C不确定的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了不同的机器上,并在不同的编译器,但我给出了相同的输出:

I tested this on different machine and on different compiler, but I gave out the same output:

int sum = 10, i = 5;
printf("%d", sum+++i);

在C这明确或不确定的行为?

Is this well-defined or undefined behavior in C?

推荐答案

这是很好的界定。 总和+++我被解析为总和++ + I ,结果为 15 (含递增的副作用)。

It's well defined. sum+++i is parsed as sum++ + i, which results as 15 (with the side effect of incrementing sum).

如果输入流被解析为preprocessing令牌到一个给定的字符,所述
  接下来的preprocessing令牌是在最长字符序列可能构成一个
  preprocessing令牌。有一个例外:头名preprocessing令牌中的<$ C $只在的#include preprocessing指令和执行定义的位置识别C>的#pragma 指令。在这样的背景下,这可以是一个头名称或文字字符串的字符序列被认为是前者。

C11 §6.4 Lexical elements

If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token. There is one exception to this rule: header name preprocessing tokens are recognized only within #include preprocessing directives and in implementation-defined locations within #pragma directives. In such contexts, a sequence of characters that could be either a header name or a string literal is recognized as the former.

和一个类似的例子是如下:

And a similar example is followed:

例2的程序片段 X +++++是被解析为 X ++ ++ + Y ,这违反了增量运营商的约束,即使解析 X ++ + ++是可能产生正确的前pression。

EXAMPLE 2 The program fragment x+++++y is parsed as x ++ ++ + y, which violates a constraint on increment operators, even though the parse x ++ + ++ y might yield a correct expression.

这篇关于为SUM +++我用C不确定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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