C macros _ line __, _ file _ and _ FUNC __

Source: Internet
Author: User

It is possible for a C program to print the currently executing line of source code, the file of the source code, and the name of the current function. the currently executing line is available in a Preprocessor variable called__LINE__:

#include <stdio.h>int main (){    printf ("This is line %d.\n", __LINE__);    return 0;}

This prints out

This is line 5.

To get the name of the file, use the Preprocessor variable__FILE__:

#include <stdio.h>int main (){    printf ("This is line %d of file \"%s\".\n",            __LINE__, __FILE__);    return 0;}

This prints out

This is line 6 of file "/share/websites/www.lemoda.net/c/line-file-func/file.c".

C99 (the most recent version of the C language, at the time of writing) introduced another macro called__func__Which also names the function in use:

#include <stdio.h>#define BLURT printf ("This is line %d of file %s (function %s)\n",\                      __LINE__, __FILE__, __func__)void silly_function (){    BLURT;}int main (){    BLURT;    silly_function ();    return 0;}

This prints out

This is line 13 of file /share/websites/www.lemoda.net/c/line-file-func/func.c (function main)This is line 8 of file /share/websites/www.lemoda.net/c/line-file-func/func.c (function silly_function)

You can change the values used__FILE__And__LINE__Using a line directive, which is a Preprocessor command of the form

#line 20 "some-file"

Which tells the C compiler that the next line after the line directive is line 20some-file.

#include <stdio.h>#define BLURT printf ("This is line %d of file \"%s\" (function <%s>)\n",\                      __LINE__, __FILE__, __func__)#line 99 "grody-to-the-max"void silly_function (){    BLURT;}#line 999 "bassetts-liquorice-allsorts"int main (){    BLURT;    silly_function ();    return 0;}

This prints out

This is line 1001 of file "bassetts-liquorice-allsorts" (function <main>)This is line 101 of file "grody-to-the-max" (function <silly_function>)

This line directive is used by facilities like "Lex" and "YACC" which output C code so that error messages are related to the input file rather than the Output C file.

GCC (the "GNU Compiler Collection") also defines__FUNCTION__And__PRETTY_FUNCTION__. These are both the same
__func__For C. For C ++,__PRETTY_FUNCTION__Is something different.

It is possible for a C program to print the currently executing line of source code, the file of the source code, and the name of the current function. the currently executing line is available in a Preprocessor variable called__LINE__:

#include <stdio.h>int main (){    printf ("This is line %d.\n", __LINE__);    return 0;}

This prints out

This is line 5.

To get the name of the file, use the Preprocessor variable__FILE__:

#include <stdio.h>int main (){    printf ("This is line %d of file \"%s\".\n",            __LINE__, __FILE__);    return 0;}

This prints out

This is line 6 of file "/share/websites/www.lemoda.net/c/line-file-func/file.c".

C99 (the most recent version of the C language, at the time of writing) introduced another macro called__func__Which also names the function in use:

#include <stdio.h>#define BLURT printf ("This is line %d of file %s (function %s)\n",\                      __LINE__, __FILE__, __func__)void silly_function (){    BLURT;}int main (){    BLURT;    silly_function ();    return 0;}

This prints out

This is line 13 of file /share/websites/www.lemoda.net/c/line-file-func/func.c (function main)This is line 8 of file /share/websites/www.lemoda.net/c/line-file-func/func.c (function silly_function)

You can change the values used__FILE__And__LINE__Using a line directive, which is a Preprocessor command of the form

#line 20 "some-file"

Which tells the C compiler that the next line after the line directive is line 20some-file.

#include <stdio.h>#define BLURT printf ("This is line %d of file \"%s\" (function <%s>)\n",\                      __LINE__, __FILE__, __func__)#line 99 "grody-to-the-max"void silly_function (){    BLURT;}#line 999 "bassetts-liquorice-allsorts"int main (){    BLURT;    silly_function ();    return 0;}

This prints out

This is line 1001 of file "bassetts-liquorice-allsorts" (function <main>)This is line 101 of file "grody-to-the-max" (function <silly_function>)

This line directive is used by facilities like "Lex" and "YACC" which output C code so that error messages are related to the input file rather than the Output C file.

GCC (the "GNU Compiler Collection") also defines__FUNCTION__And__PRETTY_FUNCTION__. These are both the same
__func__For C. For C ++,__PRETTY_FUNCTION__Is something different.

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.