C language preprocessing two (macro definition--#define)

Source: Internet
Author: User

//#define macro definition (macro definition general capitalization)// Point -and-#define的作用域: Starting with # define, from top to bottom, if you encounter #undef, it ends at #undef, if not the current entire file#include<stdio.h>#include<stdlib.h>// Knowledge Point two ---Macro definition function//macro Definition Action 1: can be used to define constants #defineX 10//macro Definition Action 2: Replace any part of the code (usually for easy language, or encrypted code)#defineWater is the source of all things, main.//Note: The macro definition is not a normal C statement, there is no need to add a semicolon at the end of the line, such as a semicolon is also substituted#defineY 100;// Knowledge Point three --and macro definition with parameters (can partially override function calls)//① a macro with a parameter is simply a substitution, does not find the value of the actual parameter, the value of the parameter is computed when the function is called//when the ② function is called, the parameters are allocated memory, and the macros are simply replacements, not memory//The ③ function has the concept of a return value, the macro has no concept of return value, an expression is returned, and the result of the expression is equivalent to the return value of the function.//The ④ function is called when there is a type, and the macro substitution does not require a type//the code is longer when the ⑤ macro replaces the expansion, and the function call does not//Jump when function is called, return time consuming, resource occupied//multiple function calls increase program run time without affecting preprocessing time//multiple macro calls, which can increase preprocessing time and run short#defineFL (x) x*x//find the minimum number of two numbers#defineMymin (x, y) x>y?y:x#defineMytex (x, y) x*y#defineMYTEX2 (x, Y) (×) * (y)//#define MYTEX2 ((x), (y)) (x) * (y) Such a macro definition is wrong, the parameters of the macro with parameters can not have ()// Knowledge Point four ----the role of # in a macro definition statement is to change a parameter to a string#defineMYTEF (x) printf ("%s\n", #x)#defineMYTEF2 (x) printf ("%s---%d\n", #x, X)// Knowledge Point Five --in the C language macro, "# #" is called a connector//It is a preprocessing operator that uses two language symbols to form a single language symbol//the language symbol here is not necessarily a macro variable, and the "# #" operator can "glue" two tokens (for example, identifiers) together to become a marker//Example:#defineL (x) s# #x//for example L (1) is equivalent to S1#defineL1 (x) person # #x//for example L (1) is equivalent to people 1#definePN (x) etes# #x//for example L (1) is equivalent to people 1//Note: The "# #" operator cannot be present as the first or last element//#define PN2 (x) # #x Error//#define PN2 (x) x## error (direct execution is not wrong, should # #会把 "error" two characters as a symbol)//if the macro definition with parameters is longer, you can use the ' \ ' connection#defineMytre (x) if (x>1) {\printf ("this number is greater than 1, AH \ n");}Else{printf ("that's not really a big number."); }voidgo1 () {//NOTE: #define is not restricted by block statements    #defineZ 10.0//the following main function can still use the z}voidEtes1 () {printf ("I'm a function etes1\n .");}voidEtes2 () {printf ("I'm a function etes1\n .");}voidWater is the source of all things () {intA[x] = {0 }; //X = 5; BUG: Error 1 C2106: "=": the left operand must be an lvalue, indicating that x is a constant//printf ("%d", Y); Error: 2 err C2059: syntax error: ")"//because y is here;printf ("%d\n", X); printf ("%d\n", Z);//print 0, because here Z is a double type of//Note: Macro expansion is simply a substitution that can contain any character in a string,//It can be a constant, or it can be an expression, and the preprocessor does not perform a syntax check for macro expansion.     #undefX//macro definition for end x    //printf ("%d\n", X); Error//macro definition with Parametersprintf"%d\n", FL (3)); printf ("%d\n", Mymin (3,8));//Printing 3printf ("%d\n", Mytex (1+7,2+7));//Print//Note: Define is only replaced, so Mytex (1+7, 2+7)) is equivalent to 1+7*2+7printf"%d\n", MYTEX2 (1+7,2+7));//Print//application of # in macro definitionMytef (hello);//print "Hello"//in the macro definition # also applies to the name of the print variable    intA1 =9;    MYTEF2 (A1); Mytre (2)        //use of the "# #" operator        intL1), L1 (1); //the L (1) here is S1S1 =Ten; Person 1=3; printf ("the value of L (1) is%d\n"L1)); MYTEF2 (L1 (1)); //the "# #" operator is used to invoke various functionsPN (1)(); //PN (1) is equivalent to Etes1System ("Pause");}

C language preprocessing two (macro definition--#define)

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.