C traps and defects-lexical and syntactic "traps"

Source: Internet
Author: User

Symbol token: A Basic Information Unit in a program. Lexical Analysis: divides programs into symbols.

 

1. Greedy rules in lexical analysis

 

If the (compiler) input stream has been decomposed into symbols before a character, the next symbol will include the longest string that may constitute a symbol after the input character. (K & R)

 

Example:

Y = x/* P;/* P points to the divisor */

 

According to the annotation, the preceding statement seems to divide X by the value pointed to by P and assign the operator to X. In fact,/* is understood by the compiler as the beginning of a comment, it will continue to read characters until */appears. The preceding statement directly assigns X to Y regardless of P.

 

2. Integer constants

 

The first character is the integer constant of the number 0, which will be considered as the octal number. 10 is different from 010. Sometimes, for the sake of format alignment, we may accidentally write the 10th hexadecimal format into the 8th format.

 

3. Character and string

 

A character enclosed in single quotes ('') represents an integer, and its value corresponds to the Sequence Value of the character in the character set used by the compiler. For example, 'A' is strictly consistent with 0141 (octal) or 97 (decimal). For compilers that use the ASCII character set.

 

A string enclosed in double quotation marks ("") represents a pointer pointing to an unknown array of Knight characters, the array is initialized by characters between double quotation marks and a character '/0' with an extra binary value of zero.

 

Because the two types are different, if they are mixed, the type check function of the compiler will detect such errors. For example, char * slash = '/';. In some compilers that do not perform type checks on function parameters, unexpected errors are generated while the program is running, without providing compiler diagnostic information.

 

4. function declaration

 

The Declaration of the C variable contains two parts: 1) type; 2) a set of declarations similar to expressions (declarator ).

 

Brackets can be used arbitrarily in the Declaration (because the declaration Operator is similar to the expression): Float (f ))

 

Float * g (), (* H )();

* G () and (* H) () are both floating point expressions. () The combination priority is higher than *, * g () is * (G (): G is a function, and its return value type is a pointer to a floating point. Similarly, it can be seen that H is a function pointer, And the return value of the function pointed to by H is of the floating point type.

 

According to a variable declaration of a given type, the type conversion operator of this type is obtained: you only need to remove the variable name and semicolon at the end of the Declaration in the Declaration, enclose the remaining parts with a bracket.

For example, float (* H) (); --->(Float (*)())
: Indicates a "pointer to a function with a return value of the floating point type.

 

Based on the function pointer FP, how to call the function pointed to by FP: (* FP )();

FP is a function pointer, and * FP is the function pointed to by the pointer, so (* FP) () is the method to call the function. Ansi c can be abbreviated as FP ().

 

5. Pay attention to the semicolon of the statement end flag and the break in the switch statement.

 

6. function call

 

The parameter list should be included even if no parameter is included during function calling. For example, F is a function, F () is a function call, while F; is a statement that does nothing: Calculate the address of F and does not call this function.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.