版权声明:github博客地址https://ryanlee0129.github.io/wtf-blog/ 引用请注明 https://blog.csdn.net/u013288800/article/details/82469706
#include<stdio.h>
#define DEBUG_INFO(X) std::cout<<__FILE__<<" "<<__LINE__<" "<<X<<std::endl;
std::string str;
DEBUF_INFO(str);
当前源代码函数名:FUNCTION
当前源代码行号:LINE
当前源代码文件名:FILE==%s\n”,FILE);
当前编译日期〔注意和当前系统日期区别开来〕:DATE
当前编译时间〔注意和当前系统日期区别开来〕:TIME
当前系统时间戳:TIMESTAMP
当要求程序严格遵循ANSIC标准时该标识符被赋值为1:STDC
当用C++编译程序编译时,标识符__cplusplus就会被定义:__cplusplus
#define CAT(N) X ## N //## 在宏定义中将两个字符连接起来,构成一个新的标识符
#define PRINTF_XN(n) printf("X" # n "=%d\n",X ## n); //# 将对应变量字符串化
C/C++程序中怎么打印一个变量的名称呢?利用#,看代码:
#include <iostream>
#define VNAME(value) (#value)
int main(int argc, char *argv[]) {
float a = 0.2;
int b = 1;
std::string sss = "hello";
std::string strName = VNAME(sss);
std::cout << VNAME(a) << std::endl;
std::cout << VNAME(b) << std::endl;
std::cout << VNAME(sss) << std::endl;
std::cout << strName << std::endl;
return 0;
}
a
b
sss
sss
一个例子
#include <iostream>
#include<stdio.h>
using namespace std;
#define TEST_INFO(X) cout<<__FILE__<<" LINE"<<__LINE__<<" "<<#X<<" "<<X<<endl
int main(int argc, const char * argv[])
{
int var = 1;
TEST_INFO(var);
return 0;
}