井号#和2井号##和艾特井号@#的使用

// vs2017使用
//井号#和2井号##和艾特井号@#的使用.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<stdio.h>
#include<iostream>
using namespace std;


#define f(a,b) a##b				//##被称为连接符(concatenation),把宏参数与之前的token(参数/字符串空格等)连接起来。
#define g(a) #a					//#:把宏参数转换为字符串。不管该参数宏什么,即“原貌”用字符串显示出来
#define h(a) g(a)


#define A(x) T_##x
#define B(x) #@x
#define C(x) #x


#define WARN_IF(EXP) if(EXP) cerr << #EXP << endl;
#define paster( n ) cout << "token" << #n << " = " << n << endl;
#define _CONS(a, b) int(a##+##b)
#define _STRI(s) #s
#define STRI(s) _STRI(s)

#define PARAM_INVILED (-1)
#define PARAM_INVILED_STR "参数错误"
#define ShowErrInfo(ERRNO) printf("[Code:%d]:%s\n", ERRNO, ERRNO##_STR);

/*##被称为连接符(concatenation),把宏参数与之前的token(参数/字符串空格等)连接起来。

*/
void test_sharp_symbol()
{
	printf("%s\n", h(f(1, 2))); //输出:12
	printf("%s\n", g(f(1, 2))); //输出:f(1,2)
	printf("%s\n", h(A(1)));    // A(1)------〉T_1
	printf("%d\n", B(1));       // B(1)------〉49
	printf("%s\n", C(1));       // C(1)------〉"1"

	int div = 0;
	WARN_IF(div == 0);                //输出: div == 0
	paster(9);                        //输出: token9 = 9
	cout << _CONS(1 + 2, 2) << endl;  //输出: 5
	
	int n = 5;
	#define k       2   
	cout << _STRI(INT_MAX) << endl; //输出: INT_MAX
	cout << STRI(m) << endl;		// prints : m
	cout << STRI(n) << endl;		// prints : n		这里注意如果是局部变量输出的是n
	cout << STRI(k) << endl;		// prints : 2		这里注意如果是define宏,则在进行嵌套的时候,在下一层会取宏值
	cout << STRI(INT_MAX) << endl;  // prints : 2147483647	
}

/*
2、宏嵌套宏

当有'#'或'##'的时候, 宏嵌套宏并不能有效的展开,此时需要多加一层宏进行转换。

如:
*/

#define INT_X 1
#define INT_Y 2

#define INT_SPLICE(x,y) (x##y)

//此时,对宏定义需要如下处理:
#define _INT_SPLICE(x,y) (x##y)

#define INT_SPLICE_2(x,y) _INT_SPLICE(x,y)


/*
2、可变参数宏
在C宏中称为Variadic Macro,也就是变参宏。比如:

#define Log(fmt, ...) printf(fmt,##__VA_ARGS__)

#define Log(fmt, args...) printf(fmt, args)

第一个采用默认的宏__VA_ARGS__, ##这个连接符号充当的作用就是当__VAR_ARGS__为空的时候,消除前面的那个逗号。

第二个宏中,我们显式地命名变参为args,在宏定义中就可以用args来代指变参.

可变参数宏其实比较简单,如下是我发现对printf的一些技巧,以前没有发现,可临时替代项目中的日志系统,输出到屏幕上,用于调试。
*/
#ifdef WIN32
#define TrimFilePath(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
#else //*nix
#define TrimFilePath(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#endif

#define LogDebug(fmt, ...)   \
printf("[DEBUG] [%s(%d)] : " fmt"\n",TrimFilePath(__FILE__),__LINE__,##__VA_ARGS__)
#define LogInfo(fmt, ...)    \
printf("[INFO ] [%s(%d)] : " fmt"\n",TrimFilePath(__FILE__),__LINE__,##__VA_ARGS__)
#define LogWarn(fmt, ...)    \
printf("[WARN ] [%s(%d)] : " fmt"\n",TrimFilePath(__FILE__),__LINE__,##__VA_ARGS__)
#define LogError(fmt, ...)   \
printf("[ERROR] [%s(%d)] : " fmt"\n",TrimFilePath(__FILE__),__LINE__,##__VA_ARGS__)


int main()
{
	test_sharp_symbol();	
	LogDebug("--%s", "a");						//[DEBUG] [xxx.cpp(77)] : --a

	ShowErrInfo(PARAM_INVILED);					//[Code:-1]:参数错误

	printf("%d\n", INT_SPLICE(1, 2));			//没问题,输出12;

	//printf("%d\n", INT_SPLICE(INT_X, INT_Y));	//编译报错;

	printf("%d\n", INT_SPLICE_2(INT_X, INT_Y));	//输出12;

	system("pause");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隨意的風

如果你觉得有帮助,期待你的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值