Duanxx C ++: Use _ FILE __,__ LINE __,__ VA_ARGS _ to debug the code, duanxx _ file __

Source: Internet
Author: User

Duanxx C ++: Use _ FILE __,__ LINE __,__ VA_ARGS _ to debug the code, duanxx _ file __
_ FILE __,__ LINE __

What we want to know most when debugging C/C ++ code is, of course, the number of lines of the file to which the program is currently going. There are two macros: __file _ and _ LINE __, return the path of the current FILE and the current row.

 

The test code is as follows:

#include <cstdio>int main(){  printf("%s : %d",__FILE__,__LINE__);  printf("\n");  return 0;}


The running result in Windows is:






The running result of Linux is as follows:




In Windows, __file _ returnsFull pathIn Linux, if g ++ is used for compiling, onlyFile Name.







_ VA_ARGS __

This is a variable parameter macro supported since C99. Previously, in C language, only variable parameters of functions are supported.


For example, the most common printf function:

void printf(const char* format, …);

Ellipsis ..., YesVariable parameters.

 

The macro _ VA_ARGS _ is a variable parameter that implements the macro:

#define debug(…) printf(__VA_ARGS__)

The test code is as follows:

#include <cstdio>#define DEBUG(...) printf(__VA_ARGS__)int main(){  DEBUG("duanxx %d\n",123);  return 0;}

Running results in Windows:



Running result in Linux:








One row debugging macro

With the above description, you can write the following debugging macro:

#define DEBUG(fmt, ...) \  fprintf(stdout, "%s:%d Duanxx Debug   " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)



Test code:

#include <cstdio>#define DEBUG(fmt, ...) \  fprintf(stdout, "%s:%d Duanxx Debug   " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)int main(){  DEBUG("duanxx %d\n",123);  return 0;}




Windows results:





Result in Linux:





In C, how does one use _ FILE _ and _ LINE _?

Note: It is a double underline, not a single underline.

_ FILE _ string containing the current program FILE name
_ LINE _ indicates the integer of the current row number.
_ DATE _ string containing the current DATE
_ STDC _ If the compiler complies with the ansi c standard, it is a non-zero value.
_ TIME _ string containing the current TIME

For example:

Program that outputs the current program file name:

//---------------------------------------------------------------------------

# Include <stdio. h>

Int main (void)
{
Printf ("% s" ,__ FILE __);
Return 0;
}
//---------------------------------------------------------------------------

C language _ VA_ARGS _

_ VA_ARGS _ is to directly convert the... in brackets into the actual string. USE_FEATURE_REMOTE_LOG (OPTBIT_remote,) is equal to OPTBIT_remote.

Your printf error occurs. Check whether the position of your printf is not defined by OPTBIT_remote.

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.