语言宏的定义和宏的使用方法(#define)(Definition of language macro and usage of macro (#define))-其他
语言宏的定义和宏的使用方法(#define)(Definition of language macro and usage of macro (#define))
- #define ARRAY_SIZE 100
- double data[ARRAY_SIZE];
没有参数的宏
- #define 宏名称 替换文本
- #define TITLE “*** Examples of Macros Without Parameters ***”
- #define BUFFER_SIZE (4 * 512)
- #define RANDOM (-1.0 + 2.0*(double)rand() / RAND_MAX)
- #include
- #include
- /* … */
- // 显示标题
- puts( TITLE );
- // 将流fp设置成“fully buffered”模式,其具有一个缓冲区,
- // 缓冲区大小为BUFFER_SIZE个字节
- // 宏_IOFBF在stdio.h中定义为0
- static char myBuffer[BUFFER_SIZE];
- setvbuf( fp, myBuffer, _IOFBF, BUFFER_SIZE );
- // 用ARRAY_SIZE个[-10.0, +10.0]区间内的随机数值填充数组data
- for ( int i = 0; i < ARRAY_SIZE; ++i )
- data[i] = 10.0 * RANDOM;
- puts( “*** Examples of Macros Without Parameters ***” );
- static char myBuffer[(4 * 512)];
- setvbuf( fp, myBuffer, 0, (4 * 512) );
- for ( int i = 0; i < 100; ++i )
- data[i] = 10.0 * (-1.0 + 2.0*(double)rand() / 2147483647);
- 10.0 * -1.0 + 2.0*(double)rand() / 2147483647
带参数的宏
- #define 宏名称( [形参列表] ) 替换文本
- #define 宏名称( [形参列表 ,] … ) 替换文本
- #define getchar() getc(stdin)
- #define putchar(x) putc(x, stdout)
- #include
// 包含putchar()的定义 - #define DELIMITER ‘:’
- #define SUB(a,b) (a-b)
- putchar( DELIMITER );
- putchar( str[i] );
- int var = SUB( ,10);
- putc(‘:’, stdout);
- putc(str[i], stdout);
- int var = (-10);
- #define DISTANCE( x, y ) ((x)>=(y) ? (x)-(y) : (y)-(x))
- d = DISTANCE( a, b+0.5 );
- d = ((a)>=(b+0.5) ? (a)-(b+0.5) : (b+0.5)-(a));
可选参数
- // 假设我们有一个已打开的日志文件,准备采用文件指针fp_log对其进行写入
- #define printLog(…) fprintf( fp_log, __VA_ARGS__ )
- // 使用宏printLog
- printLog( “%s: intVar = %d\n”, __func__, intVar );
- fprintf( fp_log, “%s: intVar = %d\n”, __func__, intVar );
字符串化运算符
- #define printDBL( exp ) printf( #exp ” = %f “, exp )
- printDBL( 4 * atan(1.0)); // atan()在math.h中定义
- printf( “4 * atan(1.0)” ” = %f “, 4 * atan(1.0));
- printf( “4 * atan(1.0) = %f “, 4 * atan(1.0));
- 4 * atan(1.0) = 3.141593
- #define showArgs(…) puts(#__VA_ARGS__)
- showArgs( one\n, “2\n”, three );
- puts(“one\n, \”2\\n\”, three”);
- one
- , “2\n”, three
记号粘贴运算符
- #define TEXT_A “Hello, world!”
- #define msg(x) puts( TEXT_ ## x )
- msg(A);
- puts( TEXT_A );
- puts( “Hello, world!” );
- msg();
- puts( TEXT_ );
在宏内使用宏
- // fn_tbl.c: 以表格形式输出一个函数的值。该程序使用了嵌套的宏
- // ————————————————————-
- #include
- #include
// 函数cos()和exp()的原型 - #define PI 3.141593
- #define STEP (PI/8)
- #define AMPLITUDE 1.0
- #define ATTENUATION 0.1 // 声波传播的衰减指数
- #define DF(x) exp(-ATTENUATION*(x))
- #define FUNC(x) (DF(x) * AMPLITUDE * cos(x)) // 震动衰减
- // 针对函数输出:
- #define STR(s) #s
- #define XSTR(s) STR(s) // 将宏s展开,然后字符串化
- int main()
- {
- double x = 0.0;
- printf( “\nFUNC(x) = %s\n”, XSTR(FUNC(x)) ); // 输出该函数
- printf(“\n %10s %25s\n”, “x”, STR(y = FUNC(x)) ); // 表格的标题
- printf(“—————————————–\n”);
- for ( ; x < 2*PI + STEP/2; x += STEP )
- printf( “%15f %20f\n”, x, FUNC(x) );
- return 0;
- }
- FUNC(x) = (exp(-0.1*(x)) * 1.0 * cos(x))
- x y = FUNC(x)
- —————————————–
- 0.000000 1.000000
- 0.392699 0.888302
- …
- 5.890487 0.512619
- 6.283186 0.533488
宏的作用域和重新定义
- #undef 宏名称
纯文本复制
- #include
- #undef isdigit // 移除任何使用该名称的宏定义
- /* … */
- if ( isdigit(c) ) // 调用函数isdigit()
- /* … */
————————
- #define ARRAY_SIZE 100
- double data[ARRAY_SIZE];
Macro without parameters
- #Define macro name replacement text
- #define TITLE “*** Examples of Macros Without Parameters ***”
- #define BUFFER_SIZE (4 * 512)
- #define RANDOM (-1.0 + 2.0*(double)rand() / RAND_MAX)
- #include
- #include
- /* … */
- //Show title
- puts( TITLE );
- //Set the stream FP to “fully buffered” mode, which has a buffer,
- //Buffer size is buffer_ Size bytes
- // 宏_IOFBF在stdio.h中定义为0
- static char myBuffer[BUFFER_SIZE];
- setvbuf( fp, myBuffer, _IOFBF, BUFFER_SIZE );
- //Use array_ Size fills the array data with random values in [- 10.0, + 10.0] intervals
- for ( int i = 0; i < ARRAY_SIZE; ++i )
- data[i] = 10.0 * RANDOM;
- puts( “*** Examples of Macros Without Parameters ***” );
- static char myBuffer[(4 * 512)];
- setvbuf( fp, myBuffer, 0, (4 * 512) );
- for ( int i = 0; i < 100; ++i )
- data[i] = 10.0 * (-1.0 + 2.0*(double)rand() / 2147483647);
- 10.0 * -1.0 + 2.0*(double)rand() / 2147483647
Macro with parameters
- #Define macro name ([parameter list]) replaces text
- #Define macro name ([parameter list,]…) replace text
- #define getchar() getc(stdin)
- #define putchar(x) putc(x, stdout)
- #include
// 包含putchar()的定义 - #define DELIMITER ‘:’
- #define SUB(a,b) (a-b)
- putchar( DELIMITER );
- putchar( str[i] );
- int var = SUB( ,10);
- putc(‘:’, stdout);
- putc(str[i], stdout);
- int var = (-10);
- #define DISTANCE( x, y ) ((x)>=(y) ? (x)-(y) : (y)-(x))
- d = DISTANCE( a, b+0.5 );
- d = ((a)>=(b+0.5) ? (a)-(b+0.5) : (b+0.5)-(a));
Optional parameters
- //Suppose we have an open log file, ready to use the file pointer FP_ Log to write to it
- #define printLog(…) fprintf( fp_log, __VA_ARGS__ )
- // 使用宏printLog
- printLog( “%s: intVar = %d\n”, __func__, intVar );
- fprintf( fp_log, “%s: intVar = %d\n”, __func__, intVar );
Stringing operator
- #define printDBL( exp ) printf( #exp ” = %f “, exp )
- printDBL( 4 * atan(1.0)); // atan()在math.h中定义
- printf( “4 * atan(1.0)” ” = %f “, 4 * atan(1.0));
- printf( “4 * atan(1.0) = %f “, 4 * atan(1.0));
- 4 * atan(1.0) = 3.141593
- #define showArgs(…) puts(#__VA_ARGS__)
- showArgs( one\n, “2\n”, three );
- puts(“one\n, \”2\\n\”, three”);
- one
- , “2\n”, three
Mark paste operator
- #define TEXT_A “Hello, world!”
- #define msg(x) puts( TEXT_ ## x )
- msg(A);
- puts( TEXT_A );
- puts( “Hello, world!” );
- msg();
- puts( TEXT_ );
Use macros within macros
- // fn_ tbl. c: Output the value of a function in tabular form. The program uses nested macros
- // ————————————————————-
- #include
- #include
// 函数cos()和exp()的原型 - #define PI 3.141593
- #define STEP (PI/8)
- #define AMPLITUDE 1.0
- #define ATTENUATION 0.1 // 声波传播的衰减指数
- #define DF(x) exp(-ATTENUATION*(x))
- #define FUNC(x) (DF(x) * AMPLITUDE * cos(x)) // 震动衰减
- //For function output:
- #define STR(s) #s
- #Define XSTR (s) str (s) / / expand the macro s and string it
- int main()
- {
- double x = 0.0;
- printf( “\nFUNC(x) = %s\n”, XSTR(FUNC(x)) ); // Output this function
- printf(“\n %10s %25s\n”, “x”, STR(y = FUNC(x)) ); // Table title
- printf(“—————————————–\n”);
- for ( ; x < 2*PI + STEP/2; x += STEP )
- printf( “%15f %20f\n”, x, FUNC(x) );
- return 0;
- }
- FUNC(x) = (exp(-0.1*(x)) * 1.0 * cos(x))
- x y = FUNC(x)
- —————————————–
- 0.000000 1.000000
- 0.392699 0.888302
- …
- 5.890487 0.512619
- 6.283186 0.533488
Scope and redefinition of macros
- #undef 宏名称
纯文本复制
- #include
- #Undef isDigit / / remove any macro definition with this name
- /* … */
- if ( isdigit(c) ) // 调用函数isdigit()
- /* … */