コンパイラ内蔵マクロによるデバッグ情報出力

6783 ワード

C 99におけるコンパイラ内蔵マクロに関する情報
6.10.8 Predefined macro names

The rule that these macros may not be redefined or undefined reduces the complexity of the name space that the programmer and implementor must understand; and it recognizes that these macros have special built-in properties.

The macros _ _DATE_ _ and _ _TIME_ _ were added in C89 to make available the time of translation. A particular format for the expansion of these macros was specified to aid in parsing strings initialized by them.

The macros _ _LINE_ _ and _ _FILE_ _ were added in C89 to give programmers access to the source line number and file name.

The macro _ _STDC_ _ allows for conditional translation on whether the translator claims to be standard-conforming. It is defined as having the value1. Future versions of the Standard could define it as 2, 3, etc., to allow for conditional compilation on which version of the Standard a translator conforms to. The C89 Committee felt that this macro would be of use in moving to a conforming implementation.

The macro _ _STDC_VERSION_ _ was added in C95.

A new feature of C99: C99 adds two additional predefined macros: _ _STDC_IEC_559_ _ and _ _STDC_IEC_559_COMPLEX_ _.

基本的には、
 
__DATE__ コンパイル日
__TIME__ コンパイル時間
__FILE__ ファイルパスのコンパイル
__LINE__ 現在のソースの行番号
 
C 99における不定パラメータマクロ定義のサポート
A new feature of C99: C89 introduced a standard mechanism for defining functions with

variable numbers of arguments, but did not allow any way of writing macros with the same

property. For example, there is no way to write a macro that looks like a call to printf.

This facility is now available. The macro definition uses an ellipsis in the same way to indicate a

variable argument list. However, since macro substitution is textual rather than run-time, a

different mechanism is used to indicate where to substitute the arguments: the identifier

_ _VA_ARGS_ _. This is replaced by all the arguments that match the ellipsis, including the

commas between them.

For example, the following macro gives a “debugging printf”:

#ifdef DEBUG

#define dfprintf(stream, ...) \

fprintf(stream, "DEBUG: " _ _VA_ARGS_ _)

#else

#define dfprintf(stream, ...) ((stream, _ _VA_ARGS_ _, 0))

#endif

#define dprintf(...) dfprintf(stderr, _ _VA_ARGS_ _)

For example,

dprintf("X = %d
", x);
expands to dfprintf(stderr, "X= %d
", x);
and thus to one of fprintf(stderr, "DEBUG: " "X = %d
", x);
or ((stderr, "X = %d
", x, 0));
If DEBUG is true, this calls fprintf, but first catenating "DEBUG: " to the format (which must therefore be a simple string). Otherwise it creates a comma expression (so that the arguments are still evaluated) with the value zero. There must be at least one argument to match the ellipsis. This requirement avoids the problems that occur when the trailing arguments are included in a list of arguments to another macro or function. For example, if dprintf had been defined as #define dprintf(format, ...) \ dfprintf(stderr, format, _ _VA_ARGS_ _) and it were allowed for there to be only one argument, then there would be a trailing comma in the expanded form. While some implementations have used various notations or conventions to work around this problem, the Committee felt it better to avoid the problem altogether. Similarly, the _ _VA_ARGS_ _ notation was preferred to other proposals for this syntax.

デバッグマクロ定義サンプル:
#include  



#define __DEBUG__  



#ifdef __DEBUG__  



#define DEBUG(format,...) printf("File: "__FILE__", Line: %05d: "format"/n", __LINE__, ##__VA_ARGS__)  



#else  



#define DEBUG(format,...)  



#endif  

 
参照先:http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html
 
完了