Introduction to some usages of #define in C language

Introduction to some usages of #define in C language




Today, I've put together some #define usages to share with everyone!

A simple define definition

#define MAXTIME 1000

A simple MAXTIME is defined, it represents 1000, if written in the program

If(i

The compiler will process MAXTIME instead of 1000 before processing this code.

This definition looks like the ordinary constant definition CONST, but it also has a difference, because the definition of define is more like a simple text replacement, rather than being used as a quantity, this problem is particularly highlighted in the following.

2.define "function definition"

Define can accept some parameters like functions, as follows

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

This definition will return the larger of the two numbers. See? Because this "function" has no type checking, it looks like a function template. Of course, it is definitely not as safe as a template. Can be used as a simple template.

However, there are hidden dangers in doing so. Examples are as follows:

#define Add(a,b) a+b;

In normal use, there is no problem, but if you encounter problems such as: c * Add (a, b) * d, the algebraic formula is intended to be a + b and then multiplied by c, d, but Because of the use of define (it's just a simple replacement), the equation actually becomes

c*a + b*d

Another example is:

#define pin (int*);

Pin a,b;

The intention is that both a and b are int pointers, but actually become int* a,b;

a is an int pointer, and b is an int variable.

This should use typedef instead of define, so that both a and b are int pointers.

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

3. One-line definition of macros

#define A(x) T_##x

#define B(x) #@x

#define C(x) #x

We assume that: x=1, then there are:

A(1)------〉T_1

B(1)------>'1'

C(1)------>"1"

(here refers to the hustli article)

3. Define multiple line definitions

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

#define MACRO(arg1, arg2) do { /

/* declarations */ /

Stmt1; /

Stmt2; /

/* ... */ /

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

The key is to add a "/" to every newline.

Excerpt from https:// patched several bugs

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

Is:

#ifdef WINDOWS

......

......

#endif

#ifdef LINUX

......

......

#endif

You can set the compiler environment through #define at compile time

How to define a macro, cancel the macro

//define the 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 it is very easy to make mistakes, including differences in the understanding of machines and people, and so on.

6. Conditional compilation

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

E.g

#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 in the header file or C file;

Duplicate (repeated)

Since the header file contains nested files, it is possible for the C file to contain the same header file multiple times, and duplicate definition problems may occur.

Avoid duplicate inclusion with conditional compilation switches (repeated definitions)

E.g

#ifndef __headerfileXXX__

#define __headerfileXXX__

...

//document content

...

#endif

700W Power Station

700W Power Station,Portable Rechargeable Power Station,Rechargeable Power Station,Power Station With Socket Usb

Guangdong pisen electronics co., ltd , https://www.pisenpro.com