# 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. If 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 some parameters as the function does, as follows:

# Define max (x, y) (x)> (y )? (X) :( y); (add the outermost brackets because macros are simply replaced. Add the brackets to avoid errors caused by priority issues after replacement)

This definition will return the big one in two numbers. Have you seen it? Because 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 hidden risks. The example is as follows:
# Define add (A, B) A + B;
In general use, there is no problem, but if you encounter problems such as: C * Add (a, B) * D, the purpose of the algebraic formula is that a + B is then multiplied by C and 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 instead of 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
Suppose: x = 1, there are:
A (1) ------> T_1
B (1) ------> '1'
C (1) ------> "1"

(For more information, see 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 Parameters
# Define max (A, B) (a)> (B )? (A), (B ))
The key is that errors are easily generated, including differences in Machine understanding and human understanding.

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

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

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.