"Learning note", "C language" macro definition

Source: Internet
Author: User

1. Macro definitions can be divided into 2 types:
Macro definition with no parameters
Macro definition with Parameters

2. Definition

General form
#define Macro Name string
Like #define ABC 10.
The string on the right can also be omitted, such as # define ABC

3. Role

It is used to define constants by replacing all "macro names" in the source program with the right "strings" when compiling preprocessing.
Then write a program that calculates the perimeter based on the radius of the circle

4. Example

A macro called PI is defined in line 4th, and after the pre-processing of the compilation, the 2 * PI *radius in line 8th becomes 2 * 3.14 * radius.

1#include <stdio.h>2   3   //all the macro names in the source program will be replaced by 3.14 when compiling the preprocessing.4   #definePI 3.145 6  //Radius calculation perimeter based on radius of Circle7  floatGirth (floatradius) {8     return 2* PI *radius;9 }Ten   One intMain () A  { - floatg = Girth (2); -      theprintf"perimeter is:%f", g); - return 0; -}

5. Note

1> macro names are generally capitalized in order to distinguish them from variable names, but there is no syntax error in lowercase

2> the characters within the string that are enclosed in double quotation marks for the program, and does not replace the macro. Like what:

1 #define R2  int  main ()3  {4      char" Radio " ; 5      return 0 ; 6  }

A macro called R is defined in line 1th, but ' R ' in line 4th is not replaced with 10. Radio

3> does not make a syntax check when it replaces a macro name with a string in the compilation preprocessing, but simply replaces the string. The source program that has expanded the macro name is checked for syntax only at compile time

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

The valid range of 4> macro names is from the defined position to the end of the file. If you need to terminate the scope of a macro definition, you can use the #undef command
#define PI 3.14
/*
.
.
.
.
*/
#undef PI
PI This macro is valid between lines 1th through 8th and is invalid after line 8th.

5> you can refer to a macro name that you have defined when defining a macro
#define R 3.0
#define PI 3.14
#define L 2*pi*r
#define S Pi*r*r

6. Code

1 /*2 1. All pre-processing instructions are preceded by #3 2. Pre-processing instructions are divided into 3 types4 1> Macro Definition5 2> Conditional Compilation6 The 3> file contains7 3. Pre-processing instructions are executed before the code is translated into 0 and 18 4. The location of the preprocessing is casually written9 5. Scope of preprocessing directives: starting from the line where the instruction was written, until the end of the file, you can use #undef to cancel the function of the macro definitionTen 6. Macro names usually start with uppercase or K, and variable names are usually lowercase One  */ A#include <stdio.h> -  -  the //#define KCOUNT 4 -  - intMain () - { +     Char*name ="COUNT"; -      +printf"%s\n", name); A      at     #defineCOUNT 4 -      -     intAges[count] = {1,2, the, the}; -      -      -      in      for(inti =0; i<count; i++) { -printf"%d\n", Ages[i]); to     } +      -     //from this line, count this macro will expire the #undefCOUNT *      $     intA =COUNT;Panax Notoginseng      -     return 0; the } +  A voidTest () the { +      -}
1 /*2 1. The macro with parameters is defined as a higher efficiency than the function3  4  */5 6 /*7 int sum (int a, int b)8 {9 return a + B;Ten }*/ One#include <stdio.h> A  - #defineSum (v1, v2) ((v1) + (v2)) -  the #definePingfang (a) ((a) * (a)) -  - intMain () - { +     //Pingfang (5+5) (10*10) -     //Pingfang (5+5) +     //Pingfang (5+5) (+) A     //Pingfang (5+5)/pingfang (2) at     intc = Pingfang (5+5)/pingfang (2); -      -printf"C is%d\n", c); -     /* - int c = SUM (2, 3) * SUM (6, 4); -      in printf ("C is%d\n", c);*/ -     /* to int a = ten; +      - int b =; the      *      $ int c = SUM (A, b);Panax Notoginseng      - printf ("C is%d\n", c); the //int c = SUM (A, b);*/ +      A     return 0; the}

"Learning note", "C language" macro definition

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.