编译器内置宏定义__DATE__、__TIME__、 __FILE__、__LINE__

概述

__DATE__ :表示当前日期的字符串,格式为month/day/year(月/日/年).
__TIME__ :表示当前时间,格式为hour:minute:second(时:分:秒).
__FILE__:表示正在处理的当前文件名字符串
__LINE__ :表示正在处理的当前行号
__FUNCTION__ :表示正在处理的函数名
__STDC__:表示编译器是否遵循ANSI C标准,若果是,它就是个非零值 

示例

#include  

int main()
{
	printf("%s\n",__FILE__);
	printf("%d\n",__LINE__);
	printf("%s\n",__DATE__);
	printf("%s\n",__TIME__);
	printf("%s\n",__FUNCTION__);
}

输出
在这里插入图片描述
示例:打印程序的编译时间

#include 

const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
                            "Sep", "Oct", "Nov", "Dec"};
 
void getdate(char* pDest,int size)
{
	char temp [] = __DATE__;
	unsigned char i;
	unsigned char month, day, year;
 
  	year = atoi(temp + 9);
  	*(temp + 6) = 0;
  	day = atoi(temp + 4);
  	*(temp + 3) = 0;
  	for (i = 0; i < 12; i++)
  	{
    	if (!strcmp(temp, months[i]))
    	{
      		month = i + 1;
     	 	break;
    	}
  	}
	*pDest++ = (year%100)/10+0x30;
	*pDest++ = year%10+0x30;
	*pDest++ = (month%100)/10+0x30;
	*pDest++ = month%10+0x30;
	*pDest++ = (day%100)/10+0x30;
	*pDest++ = day%10+0x30;	
	*pDest++ = ' ';
	memcpy(pDest,__TIME__,strlen(__TIME__));
}
int main()
{
	char datestr[128] = {0};
	getdate(datestr,sizeof(datestr));
	printf("Compile the date: %s\n",datestr);
}

该示例转自:https://blog.csdn.net/zhufuronglovewenzhen/article/details/38941387

参考资料
http://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html
https://www.xuebuyuan.com/1320374.html?mobile=0

你可能感兴趣的:(C/C++,linux)