C Language Macro Definition Tips

Source: Internet
Author: User
Tags mul

Network Source: http://blog.chinaunix.net/uid-14022540-id-2849095.html

    • usage of "#" and "# # " in macros

I. GENERAL usage

We use # to turn the macro parameter into a string , and use # # to fit the two macro parameters together .

 #include <stdio.h> #define  CONS (A, b) ((int) (a# #e # #b))  #define  STR (s) #s int   main () {printf (   %s\n   "    ,str (ABC)); printf (  %d\n   ", CONS (2 , 3  ));  return  0   /*  [email protected]:/work/dcc# GCC *.c;./a.out ABC2000  */ 

Second, when the macro parameter is another macro: you need to pay attention to the macro definition of Useful ' # ' or ' # # ' where the macro parameter is no longer expanded .

1, non- ' # ' and ' # # ' situations, naturally unfold:

#define TOW (2)

#define MUL (A, B) (a*b)

printf ("%d*%d=%d\n", TOW, TOW, MUL (Tow,tow));

This line of macros will be expanded to :

printf ("%d*%d=%d\n", (2), (2), ((2) * (2)));

the parameter TOW in the MUL will be expanded to (2).

2, when there is ' # ' or ' # # ' , use ' # ' or ' # # ' The local macro parameters are no longer expanded :

#define A (2)

#define CONS (A, b) ((int) (a# #e # #b))

printf ("%s\n", CONS (A, a)); Compile error

This line is :

printf ("%s\n", int (AeA));

But the solution to this problem is simple . add an extra layer of intermediate conversion macros : the macro that involves #, # #, uses another macro to wrap it.

#include <stdio.h>#define_get_file_name (f) (#f)#defineGet_file_name (f) _get_file_name (f)intMain () {//according to the definition of Get_file_name (f), it does not use the # directly, so the internal macro is fully expanded to get the correct result .     Static CharFilename[] = Get_file_name (__file__);//"TEST.c"printf"%s\n", FILENAME); //_get_file_name (f) When expanded, refers to the macro __file__//according to the definition of _get_file_name, the inside of the macro does not expand, but only treats it as a string, without getting the correct result.     Static CharFilename2[] = _get_file_name (__file__);//__file__printf"%s\n", FILENAME2); return 0;} /*[email protected]:/work/dcc# gcc *.c;./a.out "test.c" __file__*/

    • The following is a list of some of the macro definitions commonly used in software :

1, prevents a header file from being repeated included

#ifndef Comdef_h

#define Comdef_h

Header File Contents

#endif

2 . Redefine some types to prevent differences in the number of types of bytes produced due to different platforms and compilers, which facilitates portability .

typedef unsigned char Boolean; /* Boolean value type. */

typedef unsigned long int uint32; /* Unsigned-bit value */

typedef unsigned short uint16; /* Unsigned-bit value */

typedef unsigned char uint8; /* Unsigned 8 bit value */

typedef signed Long int int32; /* Signed-bit value */

typedef signed short Int16; /* Signed-bit value */

typedef signed CHAR int8; /* Signed 8 bit value */

typedef unsigned char byte; /* Unsigned 8 bit value type. */

typedef unsigned short word; /* unsinged-bit value type. */

typedef unsigned long DWORD; /* Unsigned-bit value type. */

3 to get a byte or a word on the specified address

#define MEM_B (x) (* (BYTE *) (x))

#define MEM_W (x) (* ((Word *) (x)))

4, maximum and minimum values are calculated

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

#define MIN (x, Y) (((x) < (y))? (x): (y))

7, convert two bytes into a Word in LSB format

#define FLIPW (Ray) (((Word) (Ray) [0]) + (Ray) [1])

8, convert a Word to two bytes in LSB format

#define FLOPW (Ray, Val) \

(Ray) [0] = ((val)/256); \

(Ray) [1] = ((val) & 0xFF)

convert one letter to uppercase

#define UPCASE (c) (((c) >= ' A ' && (c) = ' 0 ' && (c) = ' 0 ' && (c) = ' A ' && (c) = ' a ' && (c) (Val))? (val) +1: (val))

use some macro to track debugging

When the _DEBUG is defined, the output data information and the row where the file resides

#include <stdio.h>#include<time.h>#define_debug#ifdef _DEBUG#defineDebugmsg (msg, date) Do{printf ("msg:%s; file:%s, line:%d, date:%s\n", MSG, __file__, __line__, date); } while(0)    #else    #defineDebugmsg (Msg,date)#endif#if1intMain () {time_t now; Time (&Now ); Debugmsg ("Hello", CTime (&Now )); return 0;}#endif

macro definition To prevent use is wrong

Enclosed in parentheses .

with do{}while (0) statement contains multiple statements to prevent errors

Eg: #define ADD (A, b) do{a+b;\

a++;} while (0)

C Language Macro Definition Tips

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.