为什么" A +++++ B"不能在GCC编译,但" A +++ B"," A ++ + ++ B"和" A +++ ++ B"可? [英] Why "a+++++b" can not be compiled in gcc, but "a+++b", "a++ + ++b", and "a+++ ++b" can be?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/5341202/please-help-me-understanding-the-error-ab-in-c\">Please帮我理解错误+++++ b在ç
块引用>下面是在样品code,为什么一个+++++ B不能编译,但其他人可以吗?
的#include&LT;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主(INT ARGC,字符** argv的)
{
INT A = 0;
INT B = 0;
INT C = 0;
C = A +++ B:
的printf(A +++ b为:%d个\\ N,C); C = A = B = 0;
C = A + + + + B:
的printf(A + + + + b为:%d个\\ N,C); C = B = A = 0;
C = A +++ ++ B:
的printf(A +++ ++ b为:%d个\\ N,C); C = B = A = 0;
C = A +++++ B: //注意:不能在此编译。
的printf(A +++++ b为:%d个\\ N,C); 返回0;
}
解决方案这是因为
A +++++ b
被解析为A + + + + + b
,而不是A + + + + b
[C的分词器是贪婪的。A ++
返回一个右值,你可以不适用++
上一个右值,所以你得到这个错误。A +++ B: //解析为++ + B
A +++ ++ B: //解析为+ + + + b阅读最大蒙克规则。
Possible Duplicate:
Please help me understanding the error a+++++b in CHere 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; }
解决方案That's because
a+++++b
is parsed asa ++ ++ + b
and not asa ++ + ++ 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.
这篇关于为什么&QUOT; A +++++ B&QUOT;不能在GCC编译,但&QUOT; A +++ B&QUOT;,&QUOT; A ++ + ++ B&QUOT;和&QUOT; A +++ ++ B&QUOT;可?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!