C preprocessor

tags: C language  c++  algorithm  

1. Show constant: #define

#Define pre -processor instructions are the same as other pre -processors. ANSI and the later standards are allowed to have a space or component in front of#, and two thousand can also be allowed to have space in the rest of the# and instructions. The C -requirements instructions of the old version start from the far left of the line, and the#instructions cannot have space between the rest.
The instruction can appear anywhere for the source file, and its definition is valid from the place where the instruction appears to the end of the file. You can use #define to define the explicit constant (also known as symbol constant)

 #include <stdio.h>

#define TWO 2	/* TWO 2 */
#define OW "I am \
chengxuy"

#define FOUR TWO*TWO
#define PX printf("X is %d\n", x)
#define FMT "x is %d.\n"

int main()
{
	int x = TWO;
	
	PX;
	x = FOUR;
	printf(FMT, x);
	printf("%s\n", OW);
	
	return 0;
}


There are three parts of each line of #define. The first part is #define itself; the second part is the selected abbreviation, also known as macro, some macro represents the value. These macros are called category macro, which must follow the naming rules of the C variable; The rest) is called a replacement list or replacement body. Once the prepositioner finds the example of the macro in the program, it will replace the macro with the replacement body (with exceptions). The process of changing from macro to final text is called macro exhibition.

1. Definition constant

Assume that Limit is defined as 20, and later defined it as 25 in the modified file. This process is called a defined constant.

2. Use parameters in #define

Use the parameter in #define to create a class function macro of the shape and action domain function. The macro with parameters looks like a function. One or more parameters can be available in the brackets defined by the class function macro, and then these parameters appear in the replacement body.

#include <stdio.h>

#define SQUARE(X) X*X
#define PR(X) printf("The result is %d.\n", X)

int main()
{
	int x = 5;
	int z = 0;
	
	printf("x=[%d], z=[%d]\n", x, z);
	z = SQUARE(x);
	
	printf("x=[%d], z=[%d]\n", x, z);
	PR(z);
	PR(x);

	return 0;
}

1. Create a string with the number of macro ginseng: #iculum operator

If x is a macro ginseng, then #X is converted to a string "X" -shaped gimmick. This process is called string.
#include <stdio.h>

#define PSQR(x) printf("the square of " #x " is %d.\n", ((x)*(x)))

int main()
{
	int y = 5;

	PSQR(y);
	PSQR(2 + 4);
	
	
	return 0;
}

2. Preparatory optional adhesive: ## operator

With# type, ## computing symbols can be used in the replacement part of the function macro. Moreover, ## can also be used for the replacement part of the object macro. ## The operator forms two marks into a mark. E.g:

#define XNAME(n) x ## n 
#include <stdio.h>

#define XNAME(n) x ## n
#define PRINT_XN(n) printf("x " #n " = %d\n", x ## n); 

int main()
{
	
	int XNAME(1) = 14;;
	int XNAME(2) = 20;;
	int x3 = 20;
	PRINT_XN(1);
	PRINT_XN(2);
	PRINT_XN(3);
	
	return 0;
}

3. Ginseng macro: ... and_ _VA_ARGS_ _

Some functions accept variable parameters. STDVAR.H provides tools for users to define a function with variable parameters.
This function is implemented by writing the final parameters of the macro ginseng list into a omission (that is, three points ...). Predetermine_ _VA_ARGS_ _It can be used in the replacement part, indicating what the ingenious number represents.
#define pr(…) printf(_ _VA_ARGS_ _)

#include <stdio.h>
#include <math.h>

#define PR(X, ...) printf("Message " #X " : " __VA_ARGS__)

int main()
{
	double x = 48.0;
	double y = 9.0;
	
	PR(1, "x = %g\n", x);
	PR(2, "X = %.2f, y= %.4f\n", x, y);

	return 0;
}

Third, files include: #include

Fourth, other instructions

1.#UNDEF instruction

The #Undef instructions are used to cancel the defined #define instruction. E.g

#define LIMIT 400
#undef LIMIT 	// Remove the above definition

Even if there is no definition of Limit, the definition of limit is still effective

2. Condition compilation

You can use other instructions to create conditional compilation. You can use these instructions to tell the compiler to execute or ignore information (or code blocks) according to the conditions of the compilation.
1.#iFDEF,#ELSE and #Endif instructions

#ifdef MAVIS
	#include "horse.h"	// If Mavis is defined with #define, execute the following command
	#define STABLES 5
#else
	#include "cow.h"	// If the Mavis is defined by #Define, the following command is executed
	#define STABLES 5
#endif

2.#iFndef instruction
The #IFNDEF instruction is similar to the IFDEF instruction, and it can also be used with#Else and#Endif.
#IFNDEF instructions to determine whether the lattering symbols behind are unfarished.

3. Stop righteous macro
Magnificent meaning
_ _DATE_ _ Pre -processing date ("MMM DD YYYY" forms of string words, such as Nov 23 2013)
_ _FILE_ _ The string of string of the current source code file name
_ _LINE_ _ Represents the plastic constant of the line number in the current source code file
_ _STDC_ _ Set to 1, indicate that follow C standards
_ _STDC_HOSTED_ _ This machine environment is set to 1, otherwise it will be set to 0
_ _STDC_VERSION_ _ Support C99 standard, set to 199901L; support C11 standard, set to 201112L
_ _TIME_ _ Translation code time, the format is "HH: MM: SS"
_ _func_ _ The string representing the function name (is a predetermined sign)
4、#pragma

#Pragma Put the compiler instruction into the source code. For example, when developing C99, the standard is called C9X. You can use the following compilation instructions to let the compiler support C9X:

#pragma c9x on
5. Internal connection function
inline static void asd()
{
}

Intelligent Recommendation

C syntax summary preprocessor

  Preprocessor can process C programs before compilation Most preprocessors fall into one of three types: Macro definition #define directive defines a macro, #undef directive deletes a macro defi...

Usage of C# preprocessor directives

The C# preprocessor directive is called at compile time. The preprocessor directive tells the C# compiler which code to compile and indicates how to handle specific errors and warnings. The C# preproc...

C language - preprocessor

Macro replacement You can predefine what the code block is, and you can pass parameters. Conditional inclusion #if Evaluate a constant expression The definition is not included only if expr is 0. #ifn...

C# preprocessor directive

Preprocessor instruction Used to instruct the compiler how to handle source code. General rules Some of the most important syntactic rules for preprocessor directives are as follows: Preprocessor dire...

Detailed C language preprocessor

The C preprocessor performs some textual operations on the preprocessor before it is compiled. The main tasks of the C preprocessor include: Delete comments; Insert the contents of the file contained ...

More Recommendation

C language topic - preprocessor

Macro definition is the # define directive Simply a macro definition is a simple replacement of text: #define name text can also divide a long macro definition into several lines. In this case, just a...

C# preprocessor instruction list

Preprocessing instruction description #define It is used to define a series of characters that become symbols. #undef It is used to cancel the definition symbol. #if It is used to test if the symbol i...

Preprocessor in C language (2)

Predefined macro Many macros are defined in ANSI C. You can use these macros in programming, but you cannot directly modify these predefined macros.In short, a predefined macro is the amount already d...

C# -lesson_02-preprocessor command

Preprocessor instruction The preprocessor directive directs the compiler to preprocess the information before the actual compilation begins. All preprocessor directives start with #. And on one line, ...

C language preprocessor

The concept of pre-treatment Macros and conditional compilation:   Macro concept Precautions: The method of definition and use of macros parameters There are precautions macro parameters used app...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top