[Learning notes] [C Language] macro definition, learning notes

Source: Internet
Author: User

[Learning notes] [C Language] macro definition, learning notes

1. macro definition can be divided into two types:
Macro definition without Parameters
Macro definition with Parameters

2. Definition

General Form
# Define macro name string
For example, # define ABC 10
The string on the right can also be omitted, for example, # define ABC

3. Role

It is used to replace all "macro names" in the source program with "strings" on the right during compilation and preprocessing, and is often used to define constants.
Next, write a program to calculate the perimeter Based on the circle radius.

4. Example

A macro called PI is defined in row 4th. After compilation and preprocessing, the 2 * PI * radius in row 8th will become 2*3.14 * radius.

1 # include <stdio. h> 2 3 // All macro PI in the source program will be replaced by 3.14 during compilation and preprocessing. 4 # define PI 3.14 5 6 // calculate the perimeter 7 float according to the radius of the circle girth (float radius) {8 return 2 * PI * radius; 9} 10 11 int main () 12 {13 float g = girth (2); 14 15 printf ("perimeter: % f ", g); 16 return 0; 17}

5. Note

1> macro names generally use uppercase letters to distinguish them from variable names, but there is no syntax error in lowercase letters.

2> do not replace macros for characters in strings expanded with double quotes in the program. For example:

1 #define R 102  int main ()3  {4      char *s = "Radio";5      return 0;6  }

A macro called R is defined in row 1st, but the 'R' in row 4th "Radio" is not replaced with 10.

3> when the macro name is replaced by a string during compilation and preprocessing, no syntax check is performed, but a simple string replacement is used. You must check the syntax of the source program that has expanded the macro name only during compilation.

1 #define I 1002  int main ()3  {4      int i[3] = I;5      return 0;6  }

4> the valid range of macro names is from the defined position to the end of the file. To terminate the macro-defined scope, use the # undef command.
# Define PI 3.14
/*
.
.
.
.
*/
# Undef PI
The PI macro is valid between 1st rows and 8th rows, and is invalid after 8th rows.

5> when defining a macro, You can reference the defined macro name.
# Define R 3.0
# Define PI 3.14
# Define L 2 * PI * R
# Define s pi * R

6. Code

1/* 2 1. all preprocessing commands start. pre-processing commands are divided into three types: 4 1> macro definition 5 2> Conditional compilation 6 3> file contains 7 3. run the pre-processing command 8 4 before the code is translated into 0 and 1. the pre-processing position is random write 9 5. scope of the pre-processing instruction: Starting from the line where the instruction is written to the end of the file, # undef can be used to cancel the macro definition. 6. macro names generally start with uppercase or k, and variable names generally Use lowercase 11 */12 # include <stdio. h> 13 14 15 // # define kCount 416 17 int main () 18 {19 char * name = "COUNT"; 20 21 printf ("% s \ n ", name); 22 23 # define COUNT 424 25 int ages [COUNT] = {1, 2, 67, 89}; 26 27 28 29 for (int I = 0; I <COUNT; I ++) {30 printf ("% d \ n", ages [I]); 31} 32 33 // starting from this line, the macro COUNT is invalid. 34 # undef COUNT35 36 int a = COUNT; 37 38 return 0; 39} 40 41 void test () 42 {43 44}
1/* 2 1. the macro definition efficiency with parameters is 3 4 */5 6/* 7 int sum (int a, int B) 8 {9 return a + B; 10} */11 # include <stdio. h> 12 13 # define sum (v1, v2) (v1) + (v2) 14 15 # define pingfang (a) * ()) 16 17 int main () 18 {19 // pingfang (5 + 5) (10*10) 20 // pingfang (5 + 5) 21 // pingfang (5 + 5) (35) 22 // pingfang (5 + 5)/pingfang (2) 23 int c = pingfang (5 + 5)/pingfang (2 ); 24 25 printf ("c is % d \ n", c); 26/* 27 int c = sum (2, 3) * sum (6, 4 ); 28 29 printf ("c is % d \ n", c); */30/* 31 int a = 10; 32 33 int B = 20; 34 35 36 int c = sum (a, B); 37 38 printf ("c is % d \ n", c); 39 // int c = sum (, b); */40 41 return 0; 42}

 

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.