Macro definition (GO)

Source: Internet
Author: User

Excerpt from: http://blog.chinaunix.net/uid-17188120-id-4026378.html

1. Simple macro definition

#define Identifier Substitution list (the replacement list can be number, string literal, punctuation, operator, identifier, keyword, character constant.) Note: The replacement list can be empty)

A typical use method:

The defined value is empty

#define BUG

Redefining data types

#define IT INT

2. Macros with parameters

#define Identifier (X1,X2,X3...XN) replacement list (note: x1,x2,x3. is a macro parameter, there cannot be a space between the identifier and its left parenthesis)

How to use:

#define MAX (x, y) > (y)? ( x):(y))

I=max (J+K,M-N);

To be replaced by:

I=max (J+k) > (m-n)? ( J+K):(m-n));

#define SI_EX (N) ((n)%2==0)

if (SI_EX (i)) i++;

To be replaced by:

if ((n)%2==0))

As we can see from the above example, identifiers with parameters (X1,x2,x3 ...). ) is replaced with a list (Y1,y2,y3 ...). ) corresponds to the substitution, but is not related to order.

Multi-line definition for 3.define

Define can replace multiple lines of code, adding a "\" to each line break

#define MAX (x, y) do {\

Statement 1; \

Statement 2; \

/* The notation of the comment */\

} while (0)/* (no trailing;) */\

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

#ifdef WINDOWS

......

#endif

#ifdef LINUX

......

#endif

Compile environment can be set by # define at compile time

5. Canceling a macro

#undef identifiers

6. Conditional compilation

#ifdef XXX ... (#else) ... #endif

Controls whether debug information is displayed

#ifdef Bugprint

#define BUG (ARG1,ARG2) printf (ARG1,ARG2)

#elif

#define BUG

#endif

7. Pre-defined macros

Some useful macros are included in the C language, which provide the current compilation information.

__line__ number of lines of compiled files

(Integral type)

__FILE__ the name of the compiled file (character type)

__date__ Compile date (character type)

__time__ compile time (character type)

__stdc__ if the compiler accepts standard C, then the value is 1. (Integral type)

Usually we compile with these predefined macros for troubleshooting purposes, such as:

In general, we can customize a workaround when the compiler is not prompted when a zero is present.

#define Checkzero (divisor) \

if (divisor==0) \

printf ("***attempt to DIVIDE by ZERO in line%d of file%s ***\n", __line__,__file__); \

Macro definition (GO)

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.