Pre-definition in C is very useful

Source: Internet
Author: User

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

_ Line __: Insert the current source code line number into the source code;

_ 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 the program strictly follows the ansi c standard, the ID is assigned 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 the line of the file to which the program runs and the function to which the program runs.

The following example prints the predefined macros.

Some predefined macros are specified in the _ date __,__ file __,__ line __,__ function _ c Standard and are often used for 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 ("the file is % S./N", _ file __);
Printf ("the date is % S./N", _ date __);
Printf ("the 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 ("the file is % S./N", _ file __);
Printf ("this is line % d./N", _ line __);
}

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.