Write a good-looking macro definition in C language. Using macro definition can prevent errors, improve portability, readability, and convenience. The following lists some macro definitions commonly used in mature software ......1. prevent a header file from being repeatedly contained# Ifndef comdef_h# Define comdef_h// Header file content# Endif2. redefine some
C1 condition include
As follows:
Code
# Ifndef main_h _
# Define main_h _
Other content
# Endif
When we see the header file, the function is to prevent this header file from being included multiple times.
Repeated definitions occur when multiple include operations are performed. Therefore, you must use this definition in each header file.
If you still don't know how to use it, you can see the standard header file of C, such as fcntl. h.
2. Conditional compilation
As follows:
Code
# Ifdef _ deb
#define宏定义在C系开发中可以说占有举足轻重的作用. The underlying framework does not have to say, in order to compile optimization and convenience, as well as cross-platform capabilities, macros are heavily used, it can be said that the bottom of development left define will be unable to move. When developing at a higher level, we put more emphasis on business logic and seem to have little use and reliance on macros.But the benefits of using macro definitions are self-exp
Many C + + books have advised us that C-language macros are the root of all evils, but things are not as bad as we think, like Goto. Macros have a big role in automatically generating code for us. If the template can produce various types of code for us (type substitution), then the macro can actually generate new code for us on the symbol (that is, symbol substitution, increment).
Some of the syntax questions about macros can be found on Google. Bel
People who are new to MFC programming are often intimidated by various macro definitions and preprocessing commands generated by the MFC wizard. However, preprocessing and macro definition are a powerful tool in C language. They can be used for simple source code control, version control, warning, or some special functions. A classic exampleThe most classic example of using preprocessing and
directory of this document
Introduction to preprocessing Directives
One, macro definition without parameters
Second, macro definition with parameters
Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experie
Introduction to preprocessing Directives
One, macro definition without parameters
Second, macro definition with parameters
Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are no
In gnu c, macros can accept variable parameters, just like functions, such:# Define pr_debug (fmt, arg ...)/Printk (KERN_DEBUG fmt, # arg)
Use variadic macros to transmit Variable Parameter tables You may be familiar with Using Variable Parameter tables in functions, such:
Void printf (const char * format ,...);
Until recently, variable parameter tables can only be applied to real functions and cannot be used in macros.
The C99 compiler standard has finally changed this situation. It allows you
Http://tech.e800.com.cn/articles/2009/727/1248665385863_1.html
Many C ++ books advise us that C language macros are the first of all evil, but things are not as bad as we think, just like Goto. MacroA major role is to automatically generateCode. If the template can generate various types of code (type replacement) for us ),In fact, macros can generate new Code (replacement and addition of symbols) on the symbols ).
You can find some macro Synt
Macro definition of C Language notes
(1) Symbolic Constants
Macro definition is a replacement strategy in C language, that is, using preprocessing commands # define equals a string of (lengthy) texts with a name (macro, then you can batch use Macros in the source code. In the preprocessing phase, replace the macro in t
In the C and C + + languages, an identifier is allowed to represent a string, called a macro, which can be a constant, an expression, a format string, and so on. When compiling preprocessing, all occurrences of the "macro name" in the program are substituted with the string in the macro definition, which is called "macro
(i) Symbolic constants
A macro definition is a substitution policy in C that uses a preprocessing command #define equate a string of (verbose) text with a name (called a macro), and then you can use the macro in bulk in the source code. In the preprocessing phase, replace the macros in the source code with the original text. For example, in the source code:
#def
There are two types of macro definitions: macro definitions without parameters and macro definitions with parameters.
Macro definition without ParametersThe general format of macro definition without parameters is:
# Define macro
Some people regard macros as the beasts of the flood, and even require them to be completely abandoned from C/C ++. Some people regard macros as the supreme template, which is widely used in logic code. I personally think this is a matter of opinion and wisdom. It is unnecessary to abandon it. Let's look at some classic applications of macro in MFC and ATL, you will find that if you do not use macros to Implement Message ing and object ing, it will ma
1. How do I differentiate between macro names and macro strings in a macro definition? What should I pay attention to for macros with parameters?
In a macro definition, the macro name and macro string are distinguished by spaces.
The difference between a macro and a function with parameters? Macro substitution does not take up a run time, only the compile time, and the function call takes up the elapsed time (allocation Unit, reservation field, value Pass, return), so each execution is loaded so it is slower to execute.? When defining a macro, do not type a space between the
1. How do I distinguish "macro name" from "macro string" in macro definition "? What should I pay attention to for macros with parameters?
In macro definition, "macro name" and "macro string" are distinguished by "space. During
1, preprocessing instructions
(1) Concept: C language in the source program before compiling, will first of some special preprocessing instructions to explain (such as #include
(2) Preprocessing instructions are all preceded by "#" and do not end with a semicolon.
(3) Preprocessing instructions can be placed anywhere in the file, his scope is from where it appears to the end of the file, we often put it in the source program head, so that its scope is the entire source program file.
(4) Preproce
Solo windLinks: C Language macro definitions (2)1. How do I distinguish between macro names and macro strings in a macro definition? What should be noted for macros with parameters?In the macro definition, the macro name and
Introduction to preprocessing Directives1.C language before compiling the source program, some special pre-processing instructions (such as the previous use of the # include directive) to produce a new source program (this process is called the compilation preprocessing), and then the usual compilation2. In order to differentiate between preprocessing directives and generic C statements, all preprocessor directives begin with the sign "#" and end without semicolons3. A preprocessing instruction
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.