C language-07 others,-07 others

Source: Internet
Author: User
Tags processing instruction

C language-07 others,-07 others
Preprocessing commands

/* Macro definition without parameters 1. all preprocessing commands start with #. 2. preprocessing commands are divided into three types: 1. macro definition 2. Conditional compilation 3. File Inclusion 3. the pre-processing command is executed before the code is translated into 0 and 1. the pre-processing position is random. scope of the pre-processing instruction: Starting from the line where the instruction is written to the end of the file, you can use # undef to cancel the macro definition. 6. macro names generally start with uppercase or k, and variable names are generally different from variables in lower case */# include <stdio. h> // # define kCount 4int main () {char * name = "COUNT"; // printf ("% s \ n ", name); # define COUNT 4 int ages [COUNT] = {1, 2, 67, 89}; for (int I = 0; I <COUNT; I ++) {printf ("% d \ n", ages [I]);} // starting from this line, the COUNT macro becomes invalid # undef COUNT int a = COUNT; return 0 ;}

 

/* 1. macro definitions with parameters are more efficient than functions * // * int sum (int a, int B) {return a + B;} */# include <stdio. h> # define sum (v1, v2) (v1) + (v2) # define pingfang (a) * ()) // note that to calculate the correct parameters and result values, you must add parentheses. The macro is only responsible for replacing the result. The result is calculated at run time. If the priority problem occurs, it will be calculated incorrectly without parentheses. (*,/) int main () {// pingfang (5 + 5) (10*10) // pingfang (5 + 5) // pingfang (5 + 5) (35) // pingfang (5 + 5)/pingfang (2) int c = pingfang (5 + 5)/pingfang (2); printf ("c is % d \ n ", c);/* int c = sum (2, 3) * sum (6, 4 ); // note that if the value following the macro definition is incorrectly calculated without parentheses, 2 + 3*6 + 4 = 24 should be 50 macros with parentheses (2 + 3) * (6 + 4) printf ("c is % d \ n", c); * // * int a = 10; int B = 20; int c = sum (a, B); printf ("c is % d \ n", c); // int c = sum (a, B ); */return 0 ;}

 

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.