宏定义的注意事项:
#define 宏名 宏体
宏名:大写字母表示
宏体:1.一定要用括号括起来2.语义一定要清楚
#define SECOND_OF_YEAR (365*24*3600)UL
由于是秒,一定要用无符号常整形
#if 0
这里面可以随便写啥,就相当与注释
#endif
#include <stdio.h>
int main()
{
printf("%s %s\n",__DATE__,__TIME__);
printf("%s\n",__FILE__);
printf("[%s:%d]\n",__FUNCTION__,__LINE__);
printf("Warning msg!\n");
}
/*
ANSI C标准中有几个标准预定义宏(也是常用的):
__FUNCTION__:当前处在哪个函数中
__LINE__:在源代码中插入当前源代码行号;
__FILE__:在源文件中插入当前源文件名;
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;
__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;
__cplusplus:当编写C++程序时该标识符被定义。
*/
/*交换两个数*/
#include <stdio.h>
#define SWAP(T,x,y) {T t=x;x=y;y=t;}
int main()
{
int a=1,b=2;
double c=1.1,d =2.2;
SWAP(int, a,b);
SWAP(double,c,d);
return 0;
}
#include <stdio.h>
#define swap(type,x,y) {type temp =(x);(x) =(y);(y) =temp;}
#define print(x,y) printf(#x"=%d," #y"=%d \n",x,y)
#define paste(front,back) front ## back
int main()
{
int x =1;
int y =2;
swap(int,x,y);
print(x,y);
char* paste(name,1) ="hello world";
printf("%s \n",name1);
return 0;
}
#include <stdio.h>
#define Pi 3.14
#define S(r) Pi*(r)*(r)
int main()
{
printf("%g",S(2));
return 0;
}
#include <stdio.h>
#define MAX(x,y) ((x)>(y)?(x):(y))//remember the braces
int main()
{
int a =1,b=2;
int c =3,d =4;
printf("%d\n",MAX(a,b)+MAX(c,d));
return 0;
}
/*克服i++给宏函数带来的弊端*/
#include <stdio.h>
#define max1(x,y) ({typeof(x) A = x,B = y;\
(A)>(B) ? (A):(B);})
// typeof(x):获取x的类型
int main()
{
int a=1, b=2;
printf("%d \n",max1(a++,b++));
//printf("%d \n",({int A = a++,B = b++; (A)>(B) ? (A):(B);}));
return 0;
}
#include <stdio.h>
/* 受上面程序启发,使用宏定义一个自己的SIZEOF运算符*/
#define SIZEOF(var) ({typeof(var)* p = &var;(char*)(p+1)-(char*)p;})
int main()
{
long i = 0;
printf("%ld \n",SIZEOF(i));
return 0;
}
一个#号,字符串化, #define ABC(x) #x //x所代表的值会被加双引号替换成“x”,当成一个字符串
#include<stdio.h>
#define print(x) printf(#x)
int main()
{
print(hello world\n);
return 0;
}
#include <stdio.h>
#define STR(x) puts(#x)
int main()
{
STR(hello);//puts("hello")
return 0;
}
两个##号,连接符号 #define ABC(x) day#x//相当于day作为前缀,和x的值合成一个新单词
#include<stdio.h>
#define DAY(a) myday##a
int main()
{
int myday1 =1;
int myday2 =2;
printf("%d %d",DAY(1),DAY(2));
return 0;
}
#include <stdio.h>
#define P(XXXXX) print##XXXXX()//##replace the string
void printhello(){
printf("hello \n");
}
void printworld(){
printf("world \n");
}
int main()
{
P(hello);//printhello()
P(world);//printworld()
return 0;
}
C语言#if、##ifdef、#ifndef的用法详解,C语言条件编译详解
宏定义来实现条件编译(#define #undef #ifdef)_麒麒川的博客-CSDN博客_宏定义条件编译
#include <stdio.h>
#ifndef varX
#define varX 1
int x =10000;
#endif
#ifdef varX
#undef varX//cancel define varX
#endif
#ifndef varX
#define varX 2
//int X =10086;//cause conflict
#endif
int main()
{
printf("%d \n",x);
printf("%d \n",varX);
return 0;
}
调试宏命令
#include<stdio.h>
#define DEBUG//打开调试模式,去掉就可以成为发布模式
int main()
{
#ifdef DEBUG
printf("%s\n",__FUNCTION__);
printf("%s\n",__FILE__);
printf("%d\n",__LINE__);
printf("%s\n",__DATE__);
printf("%s\n",__TIME__);
printf("%d\n",__STDC__);
printf("%ld\n",__cplusplus);
#endif
printf("hello world\n");
return 0;
}
如果不加 #define DEBUG相当于调试模式没有打开,通过
gcc -D DEBUG main.c -o main 相当于程序中写了#define debug ,这就相当于一个开关量
#define language cpp
#if language == cpp //不同语言添加不同头文件
#define HDR <iostream>
#elif language == c
#define HDR <stdio>
#endif
#include HDR
using namespace std;
#define swap(T,x,y) {T temp; temp = x; x = y; y = temp;}
int main() {
int a =1;
int b =2;
swap(int,a,b);
cout << a << b << endl;
return 0;
}