In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the introduction of # define in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
# introduction to define:
The general form of no parameter in # define macro definition is: # define identifier constant
The general form of # define macro definition parameter is: # define identifier (parameter table) expression
# operator:
# # operator:
Variable macros. And _ _ VA_ARGS__:
Macro definitions commonly used in development projects:
# introduction to define:
In C language, you can use # define to define an identifier to represent a constant. The characteristic is that the defined identifier does not occupy memory, but is only a temporary symbol, which does not exist after precompilation and does not make a type definition. Precompilation is also called preprocessing. Pre-compilation is pre-compilation processing. This operation is done automatically by the system before formal compilation.
# define is also known as macro definition, and the identifier is the name of the defined macro, or macro for short. The naming rules for identifiers are the same as those for variables. The function of # define is to define the identifier as the following constant. Once defined, the constant can be directly expressed in the program with an identifier, that is, text substitution. The variable name represents a variable, but the macro name represents a constant that can be assigned to a variable, but never a constant.
The biggest advantage of macro definition is that it is easy to modify the program. Using a macro definition, you can use a macro instead of a constant that is often used in a program. In this way, when you need to change the value of this constant, you don't need to change the whole program one by one, just modify the constant in the macro definition. And when the constant is long, using macros can replace it with shorter meaningful identifiers, which makes programming more convenient and less error-prone. Therefore, the advantage of macro definition is that it is convenient and easy to maintain.
The general form of no parameter in # define macro definition is: # define identifier constant
Note that there is no semicolon at the end, because the macro is not a statement and there is no need to add a semicolon at the end, otherwise it will be replaced into the process. Another point is that macro names are best made up of uppercase letters and underscores to distinguish variable names.
Let's take a look at an example of a # define macro definition with no parameters:
# include#define PI 3.1415926 include#define PI / identifier or macro name PI constant is a floating point function is pi # define R 2 action / identifier or macro name is R constant is a radius of circle # define PRINT "circle area =% lf\ n" / identifier or macro name PRINT constant is a string action that replaces the first parameter int main () {printf (PRINT,PI*R*R) of the printf () function. / / where PRINT is replaced by "circle area with half meridian 2 =% lf" PI is replaced by 3.1415926 R by 2 printf ("circle area with half meridian 2 =% lf\ n", 3.1415926 circle 2); / / this is the code return 0 replaced by the above code;} # define macro definition has parameters in the general form: # define identifier (parameter table) expression
For a macro definition with parameters, there can be no space in the macro name, no space between the macro name and the formal parameter table, and there can be a space between the formal parameters in the formal parameter table.
Let's take a look at an example of a # define macro definition with parameters:
# include#define SQUARE (x) x*x// identifier or macro name SQUARE expression is the square of main () {printf ("% d% d\ n", SQUARE (3), SQUARE (4)); / / SQUARE (3) is replaced with 3% 3 SQUARE (4) is replaced with 4% 4 printf ("% d% d\ n", 3% d\ n); / / this is the code return 0 after the above code is replaced.
Let's change the code a little bit:
# include#define SQUARE (x) x*x// identifier or macro name SQUARE expression is the square main () {printf ("% d% d\ n", SQUARE (2% 1), SQUARE (3 # 1)); return 0;}
It's just that the parameter 3 has been changed to 2: 1, and 4 has been changed to 3: 1. Some friends may say, "2, 2, 1, 3, 4, 3, 3, 1, 4. The answer is the same as before. Not really, because the # define macro definition is just a simple text replacement, so what should it be replaced with? SQUARE (2: 1) and SQUARE (3: 1) are replaced by 2: 1, 2: 1 and 3: 1, respectively, so the final answer is naturally 5 and 7. So how to put an end to this problem?
It's simple, just add an extra layer of parentheses when passing parameters:
# include#define SQUARE (x) x*x// identifier or macro name SQUARE expression is the square main () {printf ("% d% d\ n", SQUARE ((2x1)), SQUARE ((3x1); / / SQUARE ((2x1)) is replaced by (2x1) * (2x1) SQUARE ((3x1)) is replaced with (3x1) * (3x1) printf ("% d% d\ n", (2x1) * (2x1) * (2x1) * (3x1)) / / this is the code return 0;} replaced by the above code.
If you find this too cumbersome and troublesome, add parentheses to each parameter in the macro-defined expression:
# include#define SQUARE (x) (x) * (x) / identifier or macro name SQUARE expression is the square of main () {printf ("% d% d\ n", SQUARE (2x1), SQUARE (3x1)); / / SQUARE ((2x1)) is replaced by (2x1) * (2x1) SQUARE ((3x1)) is replaced by (3x1) * (3x1) printf ("% d% d\ n", (2x1) * (2int 1), (3x1) *) / / this is the code return 0;} replaced by the above code.
Let's change the code a little bit:
# include#define SQUARE (x) (x) * (x) / identifier or macro name SQUARE expression is the square main () {printf ("% d% d\ n", 9/SQUARE (3), 16/SQUARE (4)); return 0;}
Here, we add a division before passing the parameters, so 3-3 is followed by 9, then divided by 9, which is equal to 1-4-4-16, and then divided by 16, which equals 1. Then the expected final answer is 1 and 1, but it's not. Another text replacement, 9/SQUARE (3) and 16/SQUARE (4) are replaced with 9 / (3) * (3) and 16 / (4) * (4), respectively, and the final answer is naturally 9 and 16. So how to put an end to this problem?
It is also very simple, as long as you add an extra layer of parentheses when passing parameters:
# include#define SQUARE (x) (x) * (x) / identifier or macro name SQUARE expression is the square main () {printf ("% d% d\ n", 9 / (SQUARE (3)), 16 / (SQUARE (4) / / 9 / (SQUARE (3)) and 16 / (SQUARE (4)) are replaced with 9 / ((3) * (3)) and 16 / ((4) * (4)) printf ("% d% d\ n", 9 / (3) * (3), 16 / (4) * (4); / / this is the code return 0;} replaced by the above code.
If you still find this too cumbersome and troublesome, add parentheses to the entire expression directly in the macro definition:
# include#define SQUARE (x) ((x) * (x)) / identifier or macro name SQUARE expression is the square of main () {printf ("% d% d\ n", 9/SQUARE (3), 16/SQUARE (4)) / / 9 / (SQUARE (3)) and 16 / (SQUARE (4)) are replaced with 9 / ((3) * (3)) and 16 / ((4) * (4)) printf ("% d% d\ n", 9 / (3) * (3), 16 / (4) * (4); / / this is the code return 0;} replaced by the above code.
An expression can also write multiple statements:
# include#define AB (aforme b) asancifug5 revising bountiful jungle 3int main () {int iExhibit 3pr job5rep mpurt0jig0; AB (mmenn); / / AB (mrecaln) is replaced with msancifu5penjig3 printf ("% d% d\ n", mpeng n); return 0;} # operator:
The function of the # operator is to manipulate the macro parameter after # as a string, that is, to put a pair of double quotation marks on both sides of the parameter after # to make it a string. For example, if param is a formal parameter of a macro, the # param in the replacement text is converted to "param" by the system, which is called serialization. The code is as follows:
# include#define TEST (param) # param// identifier or macro name TEST expression is # param to convert the param parameter to the string int main () {printf ("% s\ n", TEST (first line break before line break\ nsecond line break)) / / TEST (before line breaks\ nFirst line breaks\ nsecond line breaks) is replaced with "before line breaks\ nfirst line breaks\ nsecond line breaks" printf ("before line breaks\ nfirst line breaks\ nsecond line breaks\ n"); / / this sentence is the code return 0;} # operator replaced by the above code:
The # # operator can also be used in replacement text, which acts as a glue, concatenating two macro parameters into a number. The code is as follows:
# include#define TEST (param1,param2) (param1##param2) / / identifier or macro name TEST expression is (param1##param2) is used to concatenate the param1 parameter and param2 parameter and concatenate the number int main () {printf ("% d\ n", TEST (122.34)); / / TEST (12line 34) is replaced with (1234) printf ("% d\ n", (1234)); / / this is the code return 0 replaced by the previous sentence of code } variable macros. And _ _ VA_ARGS__:
Variable macros. The main purpose of and _ _ VA_ARGS__ is to facilitate the management of print information in the software. You usually need to print out some important parameters when writing code or DEBUG, but you don't want to print them when the software is released, so variable parameter macros are used. The code is as follows:
# include#define PRINT (...) The printf (_ _ VA_ARGS__) / / identifier or macro name PRINT expression is printf (_ _ VA_ARGS__) _ _ VA_ARGS__ is used in the replacement text to indicate the ellipsis. What does it stand for? int main () {PRINT ("hello\ n"); / / PRINT ("hello\ n") is replaced by printf ("hello\ n") printf ("hello\ n"); / / this is the code return 0;} replaced by the above code.
In the macro definition, the last parameter of the parameter list is the ellipsis, and _ _ VA_ARGS__ is used in the replacement text to indicate the ellipsis. What does it represent.
Macro definitions commonly used in development projects:
Prevent header files from being duplicated:
# contents of ifndef COMDEF_H#define COMDEF_H// header file # endif
To get a byte or word on a given address:
# define MEM_B (X) (* ((byte*) (x)) # define MEM_W (X) (* ((word*) (x)
Find the maximum and minimum values:
# define MAX (xpene y) ((x) > (y)? (X): (y)) # define MIN (xpene y) ((x) field)
Get an offset of field in the structure:
# define FPOS (type,field)\ (dword) & ((type*) 0)-> field)
Convert two bytes into a word according to LSB format:
# define FLIPW (ray) (word) (ray) [0] * 256) + (ray) [1])
Convert a WORD to two bytes in LSB format:
# define FLOPW (ray,val) (ray) [0] = ((val) / 256); (ray) [1] = ((val) & 0xFF)
Get the address of a variable:
# define B_PTR (var) ((byte*) (void*) & (var)) # define W_PTR (var) ((word*) (void*) & (var))
Get the high and low bytes of a word:
# define WORD_LO (xxx) ((byte) ((word) (xxx) & 255)) # define WORD_HI (xxx) ((byte) ((word) (xxx) > > 8)
Use macros to get the number of elements in an array:
# define ARR_SIZE (a) (sizeof (a) / sizeof ((a) [0])) "introduction to # define in C language" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.