C Standard predefined macros

Source: Internet
Author: User

The ansi c standard has several predefined macros: __file _ date _ time ___ _ line _.

_ Line __: inSource codeInsert Current SourceCodeRow number;

_ File __: insert the name of the current source file into the source file;

_ Date __: Insert the current compilation date into the source file

_ Time __: Insert the current compilation time into the source file;

_ Stdc __: when requiredProgramWhen the ansi c standard is strictly followed, this identifier is assigned a value of 1;

_ Cplusplus: This identifier is defined when a C ++ program is compiled.

These macros are useful ~~~~~~~~

When debugging a program or compiling a program, __line _ is useful. It can be used to print the line numbers of logical errors ~~~~~~~, Example:

Switch (X)

{

Case 1:

....;

Break;

 

Case 2:

.....;

Break;

 

Default:

Printf ("logic erro line number % d! /N ",__ line __);

Break;

}

Another example is to use _ date _ and _ time _ to insert the Compilation Time.

Code:

Void print_version_info (void)

{

Printf ("date compiled: % s/n" ,__ date __);

Printf ("timecompiled: % s/n" ,__ time __);

}

Output Format: Mm dd yy and HH: mm: SS

 

 

 

 

 

 

 

 

 

Function: It is useful when debugging programs for macros such as _ file __,__ line __,__ func, because you can easily know which file the program is running
Row, which function is used.


The following example prints the predefined macros.

_ Date __,__ file __,__ line __,__ time __,__ function __
Some predefined macros are specified in the C standard and are often used in programming. The following table lists some predefined macros that are frequently used.

_ Date _ % S _
Date of preprocessing (string text in the form of "Mmm dd YYYY)

_ File _ % s
String text representing the current source code file name

_ Line __
% D
An integer constant representing the row number in the current source code.

_ Time _ % s
Source File Compilation Time, in the format of "HH: mm: SS"

_ Function _ (_ FUNC _) % s
Current function name

_ Pretty_function __
% S

The name of the current function, including the return type and parameter type.

For example: printf ("_ pretty_function __: % s/n", _ pretty_function __);

Output :__ pretty_function __: int main (INT, char **)

 

# Include <stdio. h>
# Include <stdlib. h>
Void why_me ();
Int main ()
{
Printf ("
File is % S./N ", _ file __);
Printf ("
Date is % S./N ", _ date __);
Printf ("
Time is % S./N ", _ time __);
Printf (
"This is line % d./N", _ line __);
Printf (
"This function is % S./N", _ function __);

Why_me ();
Return
0;
}

Void why_me ()
{
Printf (
"This function is % s/n", _ FUNC __);
Printf ("
File is % S./N ", _ file __);
Printf (
"This is line % d./N", _ line __);
}

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.