) # Define

Source: Internet
Author: User

1.1. # define variant, that is, # ifndef, can prevent repeated references to header files
# Ifdef and # define combinations are generally used in header files to prevent repeated references of multiple files to the same header file.
In actual use, even if your header file is not referenced by multiple files, it is best to add it to increase program readability, portability, robustness, and so on.
Its usage is generally:
# Ifndef <ID>
# Define <ID>
......... // Include or define.
# Endif
<Identifier> in theory, it can be freely named, but the "identifier" of each header file should be unique.
The naming rule of the identifier is generally that the header file name is in uppercase, followed by an underscore, and the "." In the file name is also underlined, for example, stdio. h corresponds:
# Ifndef _ stdio_h _
# DEFINE _ stdio_h _
......... // Include or define.
# Endif
An example taken from the Internet is as follows:
Chardev. h
# Ifndef chardev_h
# Define chardev_h
# Include <Linux/IOCTL. h>
# Define major_num 100
In chardev. H
# Ifndef chardev_h
# Define chardev_h
After this is done, a certain file will reference this header file, which will contain # define chardev_h
If other files reference this header file, the compiler will determine that chardev_h has been define and other files have referenced this file,
You can find the problem of repeated header file reference.
**************************************** ****************************************
1.2. # define variation, that is, # ifdef, which can be added to the desired module (source file)
[Example]
Add
# Ifdef myself_h
# Include "myself. C"
# Endif
You can add the myself. C code to the source file and add the implemented functions to the myself module.
**************************************** ****************************************
3. # define can be used to define macro constants.
3.1 [explanation]
Some common variables and strings can be macro-defined. during compilation, the system will automatically replace
If the macro definition is not performed, if such variables and strings need to be modified, they need to be modified one by one in the source files, which is less efficient,
This macro definition only needs to be modified once to implement batch modification, which is more efficient.
In addition, numbers or characters are troublesome. It is complicated to input every time and prone to errors. Using this macro definition is convenient and easy to maintain.
[Example]
# Define PI 3.1415926
[Note]
(1) variables in macro definition are generally written in uppercase to distinguish them from ordinary variables in lower case. Of course, it is legal if you deliberately write them in lower case.
(2) # There is no semicolon (;) at the end of the define line. "Some people don't pay attention to it and will add it in a superfluous way. Some companies will also examine this detail during their recruitment.
(3) If the variables in the macro definition are internally related to the previous one, it is best to use the previous macro definition variables.
[Example]
# Define PI 3.1415926
# Define radius 5
When expressing the area of the circle, you can use the following representation:
# Define area (PI) * (RADIUS ))
// Brackets are added here to avoid a boundary effect mentioned later.
**************************************** ****************************************
3.2 [disadvantages]
Macro definition has some disadvantages:
(1) unable to check the type of variables in the macro definition. This disadvantage is relative to the const variable.
[Summary of the differences between define and const]
The variable defined by define is a variable in the compile-time period. During compilation, the system replaces all the variables,
Instead of checking the type and other attributes of its variables, it is relatively insecure and may have potential problems, but it is not found.
Because it is only a replacement during the compilation period, the Defined variables do not allocate memory during runtime and do not occupy memory space.
The variable defined by const is a variable in the run-time period. If the type does not match, when the system is running,
The const variable is also a variable during runtime, and the system allocates memory for it.
(2) Boundary Effect
A. boundary effects without parentheses
When a macro is defined, its components are not enclosed in brackets. When a macro is used for definition, the parameter passed is a variable expression,
After the system expands, the result is not what you want due to the priority.
[Example]
# Define MUL (a, B) a * B
In use, such a call:
Int A = 1, B = 2, c = 3, D = 0;
D = MUL (a + B, c)
After compilation, it becomes
D = a + B * C
Instead of what we want
D = (a + B) * C
[Solution]
The solution is also very simple, that is, to avoid such problems by adding brackets to each component.
That is, the macro definition is as follows:
# Define MUL (A, B) (a) * (B ))
**************************************** ****************************************
3. Problems Caused by no brackets during define data types

When define is used to define a new data type, unexpected results may appear because no parentheses are added.
This is actually the boundary effect mentioned above. The reason for this is to talk about it separately,
This is because this is not a problem of common computing results, but a problem of data type, the problem is more serious.
[Example]
# Define DPS struct s * // note that there is no semicolon at the end
When used, the following problems are encountered: DPS P1, P2;

After the extension is replaced during compilation, it becomes struct s * P1, P2;

P2 is not the pointer type of s we want, but a common data structure type of S, which produces a boundary effect.

The corresponding solution is also very simple, that is, when such a new data type is defined, typedef is used
Change the macro definition:
Typedef struct s * TPS; // note that there is a semicolon at the end
And then use: TPS P1, P2;
It will be normal.

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.