__FILE__,__LINE__,__DATE__,__TIME__ pre-defined names commonly used in C + +

Source: Internet
Author: User

C + + has four commonly used pre-defined names, namely: __file__,__line__,__date__,__time__

__FILE__: Path plus name of the record file

__LINE__: The number of rows that the record file has been compiled

__DATE__: Compile date for log files

__TIME__: Compile time for log files

Can be used as a variable directly, generally used as a program debugging

Example:

#include <iostream>

using namespace Std;

int main ()
{
 
cout << "File =" << __file__ << ' \ n '
<< "line =" << __line__ << ' \ n '
<< "DATE =" << __date__ << ' \ n '
<< "time =" << __time__
<< Endl;
GetChar ();
}

Operation Result:

File = G:/program/study/c++/test1.cpp
line = 17
DATE = May 27 2004
Time = 09:59:01

For printing logs, debugging is very convenient

The following function is the time type to convert the __DATE__ macro to time_t for easy operation

Static time_t Getdatefrommacro (CharConst *time) {
Char s_month[5];
    int month, day, year;
    struct tm t = {0};
    static const Char month_names[] = "Janfebmaraprmayjunjulaugsepoctnovdec";

    sscanf (Time, "%s%d%d", S_month, & Day, &year); Month = (strstr (Month_names, S_month)-month_names)/3;

    T.tm_mon = month;
    t.tm_mday = day;
    t.tm_year = year-1900;
    T.TM_ISDST =-1;

    return mktime (&t);
}

__file__,__line__,__date__,__time__ predefined names commonly used in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.