Interview question 4: What are the C language preprocessing and interview preprocessing?

Source: Internet
Author: User

Interview question 4: What are the C language preprocessing and interview preprocessing?

Problem description: operations involved in C preprocessing

Three types of preprocessing in C language include macro definition (# define), file inclusion (# include), Conditional compilation (# if, # else, And # endif ).

Macro definition introduction:

The macro definition must be written outside the function, and its scope is the macro definition ending with the source program. In the C language source program, an identifier is allowed to represent a string, which is called a "macro ". The identifier defined as a macro is called the macro name ". During compilation and preprocessing, all the macro names in the program are replaced by the strings in the macro definition. This is called macro replacement or macro expansion.

The macro name without any macro parameter is not included. It is defined as # define identifier string. "#" Indicates that this is a preprocessing command (all commands starting with # Are preprocessing commands ). "Define" is a macro-defined command. "Identifier" is a symbolic constant, that is, a macro name. A string can be a constant, expression, or format string.

The C language allows macros with parameters. In macro definition, parameters are called formal parameters, and in macro calls, parameters are called actual parameters. For macros with parameters, not only macro expansion is required during the call, but also real parameters must be used to replace the form parameters. The general format of macro definition with parameters is: # define macro name (form parameter table) string example: # define INC (x) x + 1 // macro definition y = INC (5 ); // when macro calls are called, the real parameter 5 is used to replace the form parameter x. The statement after the pre-processing macro is expanded is y = 5 + 1. Note: (1) there must be no space between the macro name and the brackets of the form parameter table. (2) macro replacement is only used for replacement, without calculation or expression solving. (3) function calls are performed when the program is running after compilation and memory is allocated. Macro replacement is performed before compilation without memory allocation. (4) macro expansion does not occupy the running time, only the Compilation Time, and function calls occupy the running time (memory allocation, field reservation, value transfer, and return value ). (5) To prevent unrestricted recursive expansion, the expansion will not continue when the macro calls itself. For example, # define TEST (x) (x + TEST (x) is expanded to 1 + TEST (1 ). # Include <. h> and # include ". h "difference: Use angle brackets to search in the directory containing files (the include directory is the include directory set by the user when setting the environment), rather than the current source file directory; if double quotation marks are used, they are first searched in the current source file directory. If they are not found, they are found in the included directory. When programming, you can select a command form based on the directory where your files are located.

Related Article

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.