C lexical trap: lexical of "greedy"

First, the C language lexical analysis of the "greedy"

Introduction : C language unary sign there are many, but they may be composed of unary operator lot, when the composition of these conflict operators should follow: from left to right to read a character, if these characters can form a symbol, you can read the next character, and so on, this is called the C language lexical analysis "greedy." `
Such as:

    int a = 3;
    int b = 0;
    b = a---1;
    printf("The result is b = %d\n",b);

The result is: b = 2.
Reason: Since left to right, the first combined into a A- symbols, substantially a-- -1, that is performed before the expression a-, as is a- (a self finally reduced), therefore, the expression for the b = 3 - 1 = 2, so the result is 2.

Another example:

    int a = 3;
    int b = 0;
    b = --a-1;
    printf("The result is b = %d\n",b);

The result is: b = 1. Reason: Since left to right, -a first combined into one symbol, so that the result is a first -a self reduced, so for a = a - 1 = 2, then to b = a - 1, so the result it's 1.
Another example:

    int a = 3;
    int b = 0;
    int *p = &a;
    b = a/*p;

Careful small partners will find that, in fact, where b = a / * p; is problematic, due to the combination of rules from left to right symbol, the first symbol combined into a coordinate annotation symbol, it will appear the following code into comment the problem, so it should be changed to:

b = a / *p;              

/ * The spacing between a comment symbols combined into a space to avoid, or change:

b = a /(*p);

* P plus a bracket that can first be engaged.

Published 24 original articles · won praise 27 · views 10000 +

Guess you like

Origin blog.csdn.net/gyyu32g/article/details/86410287
Recommended