「C 言語のソースプログラム中で行頭が # で始まるのは C 言語の命令 ではなく、cpp (C preprocessor) の命令であり、マクロと呼ぶ」というような 話を聞いたか読んだことがある (と期待する) が、cpp は実際にどういうことを しているのだろうか? gcc -E とすると、cpp だけを行なうように指示す ることになる。
| サンプル・プログラム macro.c |
#include <stdio.h>
#include <math.h>
#ifdef M_PI /* システムが M_PI を定義してあれば、それを使う */
#define PI M_PI
#else /* システムが M_PI を定義していなければ、自分で定義 */
#define PI 3.141592653589793
#endif
#define max(a,b) (((a)>(b)) ? (a) : (b))
int main()
{
printf("max(π^2,10)=%g\n",
max(PI*PI,10.0));
return 0;
}
|
| gcc -E の結果 |
| oyabun% gcc -E macro.c
(途中省略) int main()
{
printf("max(π^2,10)=%g\n",
((( 3.141592653589793 * 3.141592653589793 )>( 10.0 )) ? ( 3.141592653589793 * 3.141592653589793 ) : ( 10.0 )) );
return 0;
}
|