C / C ++ Line Number

For debugging purposes, can I get the line number in C / C ++ compilers? (standard method or specific methods for specific compilers)

eg

if(!Logical) printf("Not logical value at line number %d \n",LineNumber); // How to get LineNumber without writing it by my hand?(dynamic compilation) 
+84
c ++ c c-preprocessor line-numbers
May 17 '10 at 14:25
source share
7 answers

You must use the __LINE__ and __FILE__ preprocessor __FILE__ . These are predefined macros and part of the C / C ++ standard. During pre-processing, they are replaced by a constant string, respectively, containing an integer representing the current line number and the current file name.

Other preprocessor variables:

  • __func__ : function name (this is part of C99 , not all C ++ compilers support it)
  • __DATE__ : form line "Mmm dd yyyy"
  • __TIME__ : form string "hh: mm: ss"



Your code will be:

 if(!Logical) printf("Not logical value at line number %d in file %s\n", __LINE__, __FILE__); 
+133
May 17 '10 at 14:27
source share

As part of the C ++ standard, there are certain predefined macros that you can use. Section 16.8 of the C ++ Standard defines, among other things, the __LINE__ macro.

__LINE__ : Line number of the current line of the source (decimal constant).
__FILE__ : The estimated name of the source file (literal character string).
__DATE__ : Date of translation of the source file (character string Literal ...)
__TIME__ : Translation time of the source file (character string Literal ...)
__STDC__ : Is __STDC__
__cplusplus : The name __cplusplus is determined by the value 199711L when compiling a C ++ translation unit

So your code will look like this:

 if(!Logical) printf("Not logical value at line number %d \n",__LINE__); 
+55
May 17, '10 at 14:27
source share

You can use a macro with the same behavior as printf (), except that it also includes debugging information such as function name, class, and line number:

 #include <cstdio> //needed for printf #define print(a, args...) printf("%s(%s:%d) " a, __func__,__FILE__, __LINE__, ##args) #define println(a, args...) print(a "\n", ##args) 

These macros should behave the same with printf (), including java stacktrace information. Here is the main example:

 void exampleMethod() { println("printf() syntax: string = %s, int = %d", "foobar", 42); } int main(int argc, char** argv) { print("Before exampleMethod()...\n"); exampleMethod(); println("Success!"); } 

The result is the following result:

main (main.cpp: 11) Before exampleMethod () ...
exampleMethod (main.cpp: 7) printf () syntax: string = foobar, int = 42
main (main.cpp: 13) Success!

+12
Nov 07 '14 at 21:15
source share

Use __LINE__ (double underscore LINE with double underscore), the preprocessor will replace it with the line number that it encounters.

+11
May 17 '10 at 14:27
source share

Checkout __FILE__ and __LINE__ macros

+8
May 17 '10 at 14:27
source share

Try __FILE__ and __LINE__ .
You can also find __DATE__ and __TIME__ . Although, if you do not need to debug the program on the client computer and, therefore, you need to log this information, you should use normal debugging.

+4
May 17, '10 at 15:24
source share

Since I also encounter this problem now, and I can’t add an answer to another, but also valid, question asked here , I will give an example of solving the problem: getting only the line number where the function was called in C ++ using templates.

Background: in C ++, non-type integer values ​​can be used as an argument to a template. This differs from the typical use of data types as template arguments. Therefore, the idea is to use such integer values ​​to call the function.

 #include <iostream> class Test{ public: template<unsigned int L> int test(){ std::cout << "the function has been called at line number: " << L << std::endl; return 0; } int test(){ return this->test<0>(); } }; int main(int argc, char **argv){ Test t; t.test(); t.test<__LINE__>(); return 0; } 

Output:

function is called in line number: 0

function is called in the line of number: 16

It is worth mentioning here that in C ++ 11 Standard, you can set default template values ​​for functions using the template. In pre C ++ 11, the default values ​​for non-type arguments seem to work only for template template arguments. Thus, in C ++ 11, there is no need to have duplicate function definitions, as described above. In C ++ 11, it is also valid for const char * arguments, but they cannot be used with literals like __FILE__ or __func__ , as mentioned here .

So in the end, if you are using C ++ or C ++ 11, this can be a very interesting alternative than using a macro to invoke the calling string.

+1
Feb 09 '16 at 20:51
source share



All Articles