Use of # define in C language

Source: Internet
Author: User
Tags define definition

1. Simple define Definition

#define MAXTIME 1000

A simple maxtime is defined, it represents 1000, if it is written inside the program

if (I<maxtime) {...}

The compiler replaces the maxtime with 1000 before processing the code.

Such a definition looks similar to a normal const definition, but it is also different because the definition of define is more like a simple text substitution than a volume, and the problem is particularly evident below.

"Function definition" of 2.define

Define can accept some parameters like a function, as follows

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

This definition will return the larger of the two numbers, see? Because this "function" does not have a type check, it is like a function template, of course, it is absolutely not as safe as a template. Can be used as a simple template.

But there are pitfalls to doing so, examples are:
#define ADD (A, b) a+b;
There is no problem when it comes to general use, but if you encounter problems such as C * ADD (b) * d, the algebraic intent is to a+b and then to multiply with c,d, but since the use of define (which is just a simple substitution), the equation actually becomes
C*a + b*d

Also give an example:
#define PIN (int*);
Pin A, B;
The original intention is that A and B are both int type pointers, but actually become int* a, B;
A is an int pointer, and b is a variable of type int.
This is the use of TypeDef instead of define, so that both A and B are int pointers.

So when we define it, we develop a good habit and suggest that all levels be bracketed.

3. Single-line definition of macros

#define A (x) t_# #x
#define B (x) #@x
#define C (x) #x

We assume that: X=1, there are:

A (1)------〉t_1
B (1)------' 1 '
C (1)------"1"

(Refer to Hustli's article here)

Multi-line definition for 3.define

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

#define MACRO (arg1, arg2) do {/
/* declarations */
STMT1; /
STMT2; /
/* ... */ /
} while (0)/* (no trailing;) */
The key is to add a "/" to each line break

Excerpt from http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml patched a few bugs

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

It is:
#ifdef WINDOWS
......
......
#endif
#ifdef LINUX
......
......
#endif

Compile environment can be set by # define at compile time

5. How to define macros, cancel macros

Defining macros
#define [MacroName] [Macrovalue]
Cancel macro
#undef [MacroName]
Normal macro
#define PI (3.1415926)

Macros with parameters
#define MAX (a) > (b)? (a), (b))
The key is to be very prone to errors, including differences in machine and human understanding, and so on.

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 contained in a Beatles file or a C file;
Repeat include (repeat definition)
Because the header file contains can be nested, then the C file may contain multiple times the same header file, there may be a duplicate definition of the problem.
Avoid duplicate inclusions with conditional compilation switches (duplicate definitions)
For example
#ifndef __headerfilexxx__
#define __headerfilexxx__
...
File contents
...
#endif

Use of # define in C language

Related Article

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.