C语言 宏定义#define #ifdef #undef #endif 调试宏

37 篇文章 0 订阅

 宏定义的注意事项:

#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;
}

#、##、__VA_ARGS__的使用_萧国浪子的博客-CSDN博客___va_args__使用打印信息的接口时,经常见到__VA_ARGS__和##__VA_ARGS__这两个字符串,花时间学习下这部分的知识,发现还有#和##这两个比较有意思的字符串,记下他们的用法:#: 用来把参数转换成字符串;例:#include <iostream>#define LOG(x) do { printf("%s=%d\n",#x,x); }while(0)int ...https://blog.csdn.net/auccy/article/details/88833659

C语言宏定义,内置宏,__FILE__,__LINE__,## 用法_tomtc123的博客-CSDN博客当然宏定义非常重要的,它可以帮助我们防止出错,提高代码的可移植性和可读性等。下面列举一些成熟软件中常用得宏定义1,防止一个头文件被重复包含#ifndef COMDEF_H#define COMDEF_H//头文件内容 …#endif2,重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。typedef  unsignehttps://blog.csdn.net/tomtc123/article/details/8875468 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值