#include<stdio.h>#defineDEBUG_PRINTprintf("File %s line %d:"\"x=%d,y=%d,z=%d",\__FILE__,__LINE__,\x,y,z)intmain(){int x=1,y=1,z=1;
x *=2;
y += x;
z = x*y;
DEBUG_PRINT;return0;}=====================================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
File test.c line 13:x=2,y=3,z=6
#include<stdio.h>#defineSQUARE(x) x*x intmain(){printf("result = %d",SQUARE(6));return0;}=========================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
result =36
#include<stdio.h>#defineSQUARE(x) x*x intmain(){int a=5;printf("result = %d",SQUARE(a+1));return0;}=========================================
PS E:\project_C\test> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
result =11
#include<stdio.h>#defineSQUARE(x)(x)*(x)intmain(){int a=5;printf("result = %d",SQUARE(a+1));return0;}=======================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
result =36
#include<stdio.h>#defineDOUBLE(x)(x)+(x)intmain(){int a=5;printf("result = %d",10*DOUBLE(a));return0;}=====================================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
result =55
#include<stdio.h>#defineDOUBLE(x)((x)+(x))intmain(){int a=5;printf("result = %d",10*DOUBLE(a));return0;}===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
result =100
14.2.2 #define替换
#include<stdio.h>#definePRINT(FORMAT,VALUE)\printf("The value is "FORMAT "\n",VALUE)intmain(){PRINT("%d",24);return0;}===============================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
The value is 24
#include<stdio.h>#definePRINT(FORMAT,VALUE)\printf("The value of "#VALUE \" is "FORMAT "\n",VALUE)intmain(){int x =20;PRINT("%d",x+3);return0;}========================================
PS E:\project_C\test> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
The value of x+3 is 23
#include<stdio.h>#defineADD_TO_SUM(sum_number,value)\sum ##sum_number += valueintmain(){int sum1 =20;ADD_TO_SUM(1,10);printf("sum1的值为:%d",sum1);return0;}============================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
sum1的值为:30
14.2.3 宏与函数
#include<stdio.h>#defineMAX(a,b)((a)>(b)?(a):(b))intmain(){int a =10,b =15;printf("MAX = %d",MAX(a,b));return0;}===================================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
MAX =15
#include<stdio.h>#include<stdlib.h>#defineMALLOC(n,type)\((type *)malloc((n)*sizeof(type)))intmain(){int*p =MALLOC(25,int);if(p!=NULL)printf("OK");return0;}===========================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
OK
14.2.4 带副作用的宏参数
#include<stdio.h>#defineMAX(a,b)((a)>(b)?(a):(b))intmain(){int x=5,y=8;int z =MAX(x++,y++);printf("x = %d,y = %d,z = %d",x,y,z);return0;}===============================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
x =6,y =10,z =9
14.2.5 命名约定
14.2.6 #undef
14.2.7 命令行定义
14.3 条件编译
#include<stdio.h>#defineDEBUG1intmain(){#ifDEBUGprintf("DEBUG is 1 \n");#endifreturn0;}===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
DEBUG is 1
#include<stdio.h>#defineDEBUG10#defineDEBUG20#defineDEBUG31intmain(){#ifDEBUG1printf("DEBUG1 \n");#elifDEBUG2printf("DEBUG2 \n");#elifDEBUG3printf("DEBUG3 \n");#endifreturn0;}=================================================
PS E:\project_C\test> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
DEBUG3
14.3.1 是否被定义
#include<stdio.h>// #define symbol 0#defineX0#defineABC0#defineBCD0intmain(){// #if defined(symbol)// printf("symbol has been defined");// #endif// #ifdef symbol// printf("symbol has been defined");// #endif// #if !defined(symbol)// printf("symbol has not been defined");// #endif// #ifndef symbol// printf("symbol has been defined");// #endif#ifX>0||(defined(ABC)&&defined(BCD))printf("X>0||(defined(ABC) &&defined(BCD))");#endifreturn0;}
//test.h#ifndef_test_H#define_test_H1#endif========================================================//test.c#include<stdio.h>#include"test.h"intmain(){printf("%d",_test_H);return0;}============================================
PS E:\project_C> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }1
14.5 其他指令
#include<stdio.h>// #define OPTION_A 1intmain(){#ifdefined(OPTION_A)printf("123");#else#errorno defined#endifreturn0;}==============================================
PS E:\project_C\test> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }
test.c: In function 'main':
test.c:9:10: error: #error no defined
#errorno defined
#include<stdio.h>#line10"string"//第10行intmain()//第11行{//第12行printf("%d %s",__LINE__,__FILE__);//第13行return0;}===========================================
PS E:\project_C\test> cd "e:\project_C\test\" ;if($?){ gcc test.c -o test };if($?){.\test }13 string