# Define usage

Source: Internet
Author: User
Tags constant definition define definition

1. Simple define Definition

# Define maxtime1000

A simple maxtime is defined. It represents 1000. Assume that you write

If (I <maxtime ){.........}

The compiler replaces maxtime with 1000 before processing this code.

This definition looks similar to a normal constant definition const, but it is also different, because the definition of define is more like a simple text replacement, rather than being used as a volume, this problem is particularly highlighted below.

2. Define's "Function Definition"

Define can accept the number of workers as a function, for example, the following

# Define max (x, y) (x)> (y )? (X) :( y );

This definition will return the big one in two numbers. Have you seen it? Since this "function" does not have a type check, it is like a function template. Of course, it is definitely not as secure as a template. It can be used as a simple template.

However, there are potential risks, for example:
# Define add (A, B) A + B;
In general, there is no problem, but it is assumed that a fault occurs when C * Add (a, B) * D is encountered, the purpose of the algebraic formula is that a + B is then multiplied by C, D, but because define is used (it is just a simple replacement), the formula is actually changed
C * A + B * d

Another example is as follows:
# Define pin (int *);
Pin A, B;
The intention is that A and B are both int-type pointers, but they actually become int * a, B;
A is an int pointer while B is an int variable.
This should use typedef to replace define, so that both A and B are int type pointers.

Therefore, when defining it, we should develop a good habit. We recommend that you add brackets to all layers.

3. Single Row definition of macros
# Define a (x) T _ # x
# Define B (X) # @ x

# Define C (x) # x
If x = 1, there are:
A (1) ------> T_1
B (1) ------> '1'
C (1) ------> "1"

(Here, I have taken the test on hustli)

3. Define multi-line Definition

Define can replace multiple lines of code, such as the macro definition in MFC (very classic, although disgusting)

# Define macro (arg1, arg2) do {/
/* Declarations *//
Stmt1 ;/
Stmt2 ;/
/*...*//
} While (0)/* (no trailing ;)*/
The key is to add "/" to each line feed "/"

Excerpted from the http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml fix several bugs

4. during large-scale development, especially in cross-platform and system software, Conditional compilation is the most important function of define.

That is:
# Ifdef windows
......
......
# Endif
# Ifdef Linux
......
......
# Endif

You can use # define to set the compiling environment during compilation.

5. How to define and cancel macros

// Definition macro
# Define [macroname] [macrovalue]
// Cancel the macro
# UNDEF [macroname]
Normal macro
# Define Pi (3.1415926)

Macro with limit
# Define max (A, B) (a)> (B )? (A), (B ))
The key is very easy to generate errors, including differences in machine and human understanding.

6. Conditional compilation
# Ifdef XXX... (# Else )... # Endif
For example, # ifdef dv22_aux_input
# Define aux_mode 3
# Else
# Define auy_mode 3
# Endif
# Ifndef XXX... (# Else )... # Endif

7. the header file (. h) can be included by the header file or C file;
Including (repeatedly defined)
Because the header file can be nested, the C file may include the same header file multiple times, and the problem of repeated definition may occur.
Use the Conditional compilation switch to avoid repeated inclusion (repeated definition)
For example
# Ifndef _ headerfilexxx __
# DEFINE _ headerfilexxx __
...
File Content
...
# Endif

The above is just some of the methods I have collected from the Internet for using define, which may not be comprehensive, and # The use of define already exists in this dispute, if you are also very interested in the use of # define, you can refer to our discussion (click the following link)
Http://www.dingge.com/forum/dispbbs.asp? Boardid = 43 & id = 6972 & page = 1

 

# Difference between define and typedef
 
 
1) # define is a pre-processing command. It is a simple replacement during compilation and pre-processing, and does not check the correctness. It does not mean that it is correct or not, possible errors are detected and reported only when the expanded source program is compiled. For example:
# Define PI 3.1415926
In the program: Area = pI * r will be replaced with 3.1415926 * r * R
Assume that you write the number 9 in the # define statement into the letter G for preprocessing.

2) typedef is processed during compilation. It gives an existing type an alias in its own scope, but you cannot use the typedef specifier inside a function definition.

3) typedef int * int_ptr;
And
# Define int_ptr int *
Int_ptr is used to represent int *, but the two are different. As mentioned above, # define performs simple replacement during preprocessing, while typedef is not a simple replacement, instead, declare a type as defined in the variable method. That is to say;

// Refer to (xzgyb (Lao Damo ))
# Define int_ptr int *
Int_ptr a, B; // equivalent to int * a, B; is just a simple macro replacement

Typedef int * int_ptr;
Int_ptr a, B; // A and B all point to the int pointer. If typedef is int *, a new mnemonic is introduced.


This also explains why the following points are true:
// Qunkangli (the maintenance cost is directly proportional to the square of programmer's creativity)
Typedef int * pint;
# Define pint int *

So:
Const pint P; // P cannot be changed, but the content pointed to by P can be changed
Const pint P; // P can be changed, but the content pointed to by P cannot be changed.

Pint is a pointer type. Const pint P is used to lock the pointer. P cannot be changed.
Const pint P is the object indicated by pointer p.

3) You may have noticed that # define is not a statement. Do not add points at the end of the line. Otherwise, a semicolon is replaced.

 

# Define usage

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.