#、##、__VA_ARGS__的使用,自由扩展printf 可变参数输出到终端和追加到文件等

 

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <stdio.h>
//#用来把参数名转换成字符串
#define P(x) printf("%s=%d\n",#x,x); //打印变量名及值。	
#define SQUARE(x) printf("The square of "#x" is %d.\n", ((x)*(x))); //x的平方

//##拼接变量名或函数名等。可以像函数指针一样fun##x()
#define RNAME(x) a##x

//__VA_ARGS__可变参数,即编译时 替换...,##当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错
#define log_printf(fmt,...) printf("[%s|%d]"fmt"\n",__func__,__LINE__,##__VA_ARGS__)
//可变参数输出到终端和追加到文件等 
#define logf_printf(fmt,...)  do { char temp[1024]={0}; sprintf(temp,"[%s|%d]"fmt"\n",__func__,__LINE__,##__VA_ARGS__);  printf(temp);\
std::ofstream outfile;	outfile.open("log.txt", std::ios::out | std::ios::app);  outfile << std::string(temp); outfile.close();	}while(0)

void main()
{
	int a3=8;
	P(a3);
	SQUARE(8);
	log_printf("Test%d", RNAME(3));
	logf_printf("%d",880999);

	system("pause");
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值