为什么“a+++++b”是gcc 中无法编译,但 "a"++b"、"a"+ + “a”和“a” ++b"可以吗?

发布于 2024-11-02 20:04:28 字数 716 浏览 1 评论 0 原文

可能的重复:
请帮助我理解错误 a+++++b C

这是示例代码,为什么“a++++++b”不能编译,而其他的可以?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int a = 0;
    int b = 0;
    int c = 0;
    c = a+++b;
    printf("a+++b is: %d\n", c);

    c = a = b = 0;
    c = a++ + ++b;
    printf("a++ + ++b is: %d\n", c);

    c = b = a = 0;
    c = a+++ ++b;
    printf("a+++ ++b is: %d\n", c);

    c = b = a = 0;
    c = a+++++b;      // NOTE: Can not be compiled here.
    printf("a+++++b is: %d\n", c);

    return 0;
}

Possible Duplicate:
Please help me understanding the error a+++++b in C

Here is is the sample code, why "a+++++b" can not be compiled , but others can be?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int a = 0;
    int b = 0;
    int c = 0;
    c = a+++b;
    printf("a+++b is: %d\n", c);

    c = a = b = 0;
    c = a++ + ++b;
    printf("a++ + ++b is: %d\n", c);

    c = b = a = 0;
    c = a+++ ++b;
    printf("a+++ ++b is: %d\n", c);

    c = b = a = 0;
    c = a+++++b;      // NOTE: Can not be compiled here.
    printf("a+++++b is: %d\n", c);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情愿 2024-11-09 20:04:28

这是因为 a+++++b 被解析为 a ++ ++ + b 而不是 a ++ + ++ b [C 的分词器是贪婪的]。 a++ 返回一个右值,并且您不能将 ++ 应用于右值,因此您会收到该错误。

a+++b; // parsed as a ++ + b
a+++ ++b; // parsed as a ++ + ++ b

了解最大蒙克规则。

That's because a+++++b is parsed as a ++ ++ + b and not as a ++ + ++ b[C's tokenizer is greedy]. a++ returns an rvalue and you cannot apply ++ on an rvalue so you get that error.

a+++b; // parsed as a ++ + b
a+++ ++b; // parsed as a ++ + ++ b

Read about Maximal Munch Rule.

北城孤痞 2024-11-09 20:04:28

编译器是贪婪的,所以你的表达式

a+++++b

将被理解为

a++ ++ +b

The compiler is greedy so your expression

a+++++b

will be understood as

a++ ++ +b
[旋木] 2024-11-09 20:04:28

+ 运算符级联 ... 与 a+++++b,在级联加法运算后没有左值(内存可寻址值)可相加。

换句话说,a+++b(a++) + b 相同。这是一个有效的操作。 a+++ ++b 也是如此,它相当于 (a++) + (++b)。但是对于 a+++++b,您无法通过 C 解析器获得它。对于解析器来说,它看起来像 ((a++)++) + b,并且由于 (a++) 返回一个临时值,因此它不是可以通过 ++ 再次递增的左值 运算符。

The + operators cascade ... with a+++++b, there is no l-value (memory addressable value) to add against after the addition operations are cascaded.

Put another way, a+++b is the same as (a++) + b. That's a valid operation. The same is true with a+++ ++b which equates to (a++) + (++b). But with a+++++b, you don't get that via the C-parser. To the parser it looks like ((a++)++) + b, and since (a++) returns a temp, that's not an l-value that can be incremented again via the ++ operator.

何以畏孤独 2024-11-09 20:04:28
# include <stdio.h>
# include <stdlib.h>

int main(int argc, char **argv)
{
 int a = 0;
 int b = 0;
 int c = 0;
 c = a+++b;
 printf("a+++b is: %d\n", c);

 c = a = b = 0;
 c = (a++)+(++b);
 printf("a++ + ++b is: %d\n", c);

 c = b = a = 0;
 c = (a++)+(++b);
 printf("a+++ ++b is: %d\n", c);

 c = b = a = 0;
 c = (a++)+(++b);     
 printf("a+++++b is: %d\n", c);

 return 0;
}
# include <stdio.h>
# include <stdlib.h>

int main(int argc, char **argv)
{
 int a = 0;
 int b = 0;
 int c = 0;
 c = a+++b;
 printf("a+++b is: %d\n", c);

 c = a = b = 0;
 c = (a++)+(++b);
 printf("a++ + ++b is: %d\n", c);

 c = b = a = 0;
 c = (a++)+(++b);
 printf("a+++ ++b is: %d\n", c);

 c = b = a = 0;
 c = (a++)+(++b);     
 printf("a+++++b is: %d\n", c);

 return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文