C preparation instructions for learning notes

Source: Internet
Author: User
1. What are pre-processing commands?
Pre-processing commands tell the compiler some commands that are pre-processed before compilation, with macro definitions, files including Conditional compilation.
The pre-processing command generally starts with #. It can act on the entire file wherever it is generated. Ii. macro definition
Macro definition refers to the macro definition with no limit or no limit.
Limit 1? Macro definition without limit
# Import <stdio. h> # define age 10 // macro name should be capitalized int main () {int age = age; // age will be replaced with 10 printf ("% d ", age); Return 0 ;}
Before compilation by the compiler, all age values in the code will be replaced with 10. to change the age value, you only need to change it once in the definition macro, all age values in the file are replaced with new values.
Because macros are replaced before compilation, the resulting syntax errors will only be checked when the macro name is replaced by the syntax.
You can reference a Defined Macro when defining a macro, for example:

//// Main. M // pre-processing instructions //// created by funeral Bridge on 14-5-18. // copyright (c) 2014 itcast. all rights reserved. // # import <stdio. h> # define age 10 // use uppercase macro name # define age2 age + 10 // reference the defined macro int main () {int age = age; // age will be replaced with 10 int age2 = age2; printf ("age1 = % d \ nage2 = % d", age, age2); Return 0 ;}


2? Macro definition with limit
The macro definition with the number of arguments is similar to the function definition, but they are essentially different. The macro definition is only a replacement, no memory allocation, no transfer of the number of arguments, and no return value.


# Define sum (A, B) A + B // macro definition with limit number

The macro sum here carries the number of workers, which will be replaced with the following when used:



Iii. files include
Files including pre-compiled commands are all in use, # include <> # import, etc.
It will copy the content of the target file to the current file, which is very easy and there is nothing to say. Just pay attention to repetition, including errors caused by loops, repeated definition errors, or loops including errors.
However, if Conditional compilation is used, repeated definition errors can be avoided.
Iv. Conditional compilation
# If defined (SUM) printf ("sum has been defined! \ N "); # endif
This preprocessing command indicates that the printf statement is compiled if sum has been defined.

# If! Defined (SUM) printf ("sum is not defined! \ N "); # endif

If sum is not defined, compile the printf statement.

Which of the following functions

# Ifdef sum // same as # If defined (SUM) # endif # ifndef sum // and # If! Same as defined (SUM) # endif


When a file is included, Conditional compilation can prevent repeated inclusion of the same file.



C preparation instructions for learning notes

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.