开发者

3 plus symbols between two variables (like a+++b) in C [duplicate]

This question already has answers here: What does the operation c=a+++b mean? 开发者_运维问答 (9 answers) Closed 9 years ago.
#include <stdio.h>

int main()
{
    int a=8,b=9,c;
    c=a+++b;
    printf("%d%d%d\n",a,b,c);
    return 0;
}

The program above outputs a=9 b=9 and c=17. In a+++b why is the compiler takes a++ and then adds with b. Why is it not taking a + and ++b? Is there a specific name for this a+++b. Please help me to understand.


I like the explanation from Expert C Programming:

The ANSI standard specifies a convention that has come to be known as the maximal munch strategy. Maximal munch says that if there's more than one possibility for the next token, the compiler will prefer to bite off the one involving the longest sequence of characters. So the example will be parsed

c = a++ + b;


Read Maximum Munch Principle

"maximal munch" or "longest match" is the principle that when creating some construct, as much of the available input as possible should be consumed.


Every compiler has a tokenizer, which is a component that parses a source file into distinct tokens (keywords, operators, identifiers etc.). One of the tokenizer's rules is called "maximal munch", which says that the tokenizer should keep reading characters from the source file until adding one more character causes the current token to stop making sense


Order of operations in C dictate that unary operations have higher precedence than binary operations.

You could use a + (++b) if you wanted b to be incremented first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜