您的位置:首页 > 编程语言 > C语言/C++

C语言程序获取或者打印当前语句所在的行号、当前源文件的文件名、程序编译的日期等信息(__LINE__、__FUNCTION__、__FILE__、__DATE__、__TIME__)

2017-11-30 11:01 555 查看

C语言程序获取或者打印当前语句所在的行号、当前源文件的文件名、程序编译的日期等信息(__LINE__、__FUNCTION__、__FILE__、__DATE__、__TIME__)

一、在调试C语言程序的时候有时候需要在程序里面获取或者打印当前语句所在的行号、当前源文件的文件名、程序编译的日期、程序被编译的时间、当前编译器符合ISO标准等相关信息。这就需要调用系统里面的已经定义好的宏,方便调试和跟踪程序的运行过程。

       
 

二、相关的宏

     __LINE__         当前语句所在的行号, 以10进制整数标注.

     __FUNCTION__     当前语句所在的函数名

     __FILE__         当前源文件的文件名, 以字符串常量标注.

     __DATE__         程序被编译的日期, 以"Mmm dd yyyy"格式的字符串标注.

     __TIME__         程序被编译的时间, 以"hh:mm:ss"格式的字符串标注, 该时间由asctime返回.

     __STDC__         如果当前编译器符合ISO标准, 那么该宏的值为1

     __STDC_VERSION__ 如果当前编译器符合C89, 那么它被定义为199409L, 如果符合C99, 那么被定义为199901L. 

   

     __STDC_HOSTED__  如果当前系统是"本地系统(hosted)", 那么它被定义为1. 本地系统表示当前系统拥有完整的标准C库.

三、在linux下运行的C语言测试代码。

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>
typedef struct
{
unsigned int Year;
unsigned int Month;
unsigned int Date;
unsigned int Hours;
unsigned int Minutes;
unsigned int Seconds;
}BuildDateTime;

const unsigned char MonthStr[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};

void GetBuildDateTime(BuildDateTime *p_BuildDateTime)
{
unsigned char temp_str[4] = {0, 0, 0, 0}, i = 0;

sscanf(__DATE__, "%s %d %d", temp_str, &(p_BuildDateTime->Date), &(p_BuildDateTime->Year));
sscanf(__TIME__, "%d:%d:%d", &(p_BuildDateTime->Hours), &(p_BuildDateTime->Minutes), &(p_BuildDateTime->Seconds));
for (i = 0; i < 12; i++)
{
if (temp_str[0] == MonthStr[i][0] && temp_str[1] == MonthStr[i][1] && temp_str[2] == MonthStr[i][2])
{
p_BuildDateTime->Month = i + 1;
break;
}
}
}

int GetCompileDateTime(char *szDateTime)
{
const int MONTH_PER_YEAR=12;
const char szEnglishMonth[12][4]={ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
char szTmpDate[40]={0};
char szTmpTime[20]={0};
char szMonth[4]={0};
int i,iYear,iMonth,iDay,iHour,iMin,iSec;//,,

//获取编译日期、时间
sprintf(szTmpDate,"%s",__DATE__); //"Sep 18 2010"
sprintf(szTmpTime,"%s",__TIME__); //"10:59:19"

sscanf(szTmpDate,"%s %d %d",szMonth,&iDay,&iYear);
sscanf(szTmpTime,"%d:%d:%d",&iHour,&iMin,&iSec);

for(i=0;MONTH_PER_YEAR;i++)
{
if(strncmp(szMonth,szEnglishMonth[i],3)==0)
{
iMonth=i+1;
break;
}
}

printf("%d,%d,%d,%d,%d,%d ",iYear,iMonth,iDay,iHour,iMin,iSec);
sprintf(szDateTime,"dddddd",iYear,iMonth,iDay,iHour,iMin,iSec);
return 0;
}

void main( void )
{
char datetime[50];
struct passwd *pwd;
int RTC_YEAR,RTC_MONTH,RTC_DATE,RTC_HOURS,RTC_MINUTES,RTC_SECONDS;
BuildDateTime MyBuildDateTime;
#if (!defined(__STDC__))
printf("not standard C!");//非标准C!
#elif defined(__STDC_VERSION__)
printf("standard C version:%ld", __STDC_VERSION__);//表示标准C
#else
printf("old standard C");//旧的标准C
#endif
char *buffer;
//也可以将buffer作为输出参数
if((buffer = getcwd(NULL, 0)) == NULL)
{
perror("getcwd error");
}
else
{
printf("\ncurrent path is %s\n", buffer); //返回工作目录的绝对路径
free(buffer);
}

pwd = getpwuid(getuid());
printf("login user is %s\n", pwd->pw_name);//获取当前Linux系统的用户名
printf("login user password is %s\n", pwd->pw_passwd);

while (1)
{
GetBuildDateTime(&MyBuildDateTime);
RTC_YEAR = MyBuildDateTime.Year;
RTC_MONTH = MyBuildDateTime.Month;
RTC_DATE = MyBuildDateTime.Date;
RTC_HOURS = MyBuildDateTime.Hours;
RTC_MINUTES = MyBuildDateTime.Minutes;
RTC_SECONDS = MyBuildDateTime.Seconds;
printf("\nRTC_YEAR=%d,RTC_MONTH=%d,RTC_DATE=%d,RTC_HOURS=%d,RTC_MINUTES=%d,RTC_SECONDS=%d\n",RTC_YEAR,RTC_MONTH,RTC_DATE,RTC_HOURS,RTC_MINUTES,RTC_SECONDS);
GetCompileDateTime(datetime);
printf("current file name is %s \n",__FILE__);
printf("\n<%s>line is %d ,file is %s, compile time is %s compile date is %s ,stdc is %d ,__STDC_HOSTED__=%d \n",__FUNCTION__,__LINE__ ,__FILE__ ,__TIME__,__DATE__ ,__STDC__,__STDC_HOSTED__);
sleep(2);
}
}

四、测试结果。



五、Read The Fucking Source Code,看代码理解一下吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐