# Define Summary (online data collection)

Source: Internet
Author: User
1. Define a simple constant: Define a constant to facilitate modification. (do not add a semicolon to the end !) # Define n 1000 is equivalent to const int n = 1000, but slightly different, define is just a simple replacement, rather than using it as a quantity. 2. defining simple functions: note that brackets are often used.

Define can accept some parameters like the function, as follows:

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

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.

so when we define, make a good habit. We recommend that you add brackets to all layers.

3. define a single row macro: There are three main usage types. 1) The prefix ##or Postfix ## is used as a part of a valid identifier. note: It is not a string. macro definition that is used for multiple rows. for example: # define a (x) T _ # X, int A (1) = 10; // equivalent to int t_1 = 10; # define a (x) TX ##__then int A (1) = 10; // equivalent to int T1 _ = 10; 2) Add # @ before to convert the mark to the corresponding character, note: only valid for single tag conversion (incorrect understanding ?) # Define B (X) # @ X then B (a) is 'A', B (1) is '1 '. B (ABC) is not valid. 3) add # to convert the tag to a string. # define C (x) # X then C (1 + 1) is "1 + 1 ". 4. define multi-row macros: note the use of the slash. the last row cannot use a slash. # define declare_rtti (thisclass, superclass) \ virtual const char * getclassname () const \ {return # thisclass;} \ static int istypeof (const char * type) \ {\ If (! Strcmp (# thisclass, type) \ return 1; \ return superclass: istypeof (type); \ return 0 ;\}\ virtual int ISA (const char * type) \{\ return thisclass: istypeof (type) ;\}\ static thisclass * safedowncast (ditkobject * O) \ {\ If (O & O-> ISA (# thisclass) \ return static_cast <thisclass *> (o); \ return NULL ;\} 5. used for Conditional compilation: (common form) # ifndef _ aaa_h # DEFINE _ aaa_h // C/C ++ Code # Endif

In large-scale development, especially for 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 3

# 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

8. Some Considerations: 1) the definition cannot be repeated unless the definition is exactly the same. # define a (x )... And # define a are repeated definitions.

2) You can only define symbols without defining values. For example, # define aaa

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.