views:

625

answers:

5
+15  Q: 

C/C++ line number

In the sake of debugging purposes, can I get the line number in C/C++ compilers? (standard way or specific ways for certain compilers)

e.g

if(!Logical)
    printf("Not logical value at line number %d \n",LineNumber);
    // How to get LineNumber without writing it by my hand?(dynamic compilation)

Thanks

+15  A: 

As part of the C++ standard there exists some pre-defined macros that you can use. Section 16.8 of the C++ standard defines amongst other things, the __LINE__ macro.

__LINE__: The line number of the current source line (a decimal constant).
__FILE__: The presumed name of the source file (a character string literal).
__DATE__: The date of translation of the source file (a character string literal...)
__TIME__: The time of translation of the source file (a character string literal...)
__STDC__: Whether__STDC__ is predefined
__cplusplus: The name __cplusplus is defined to the value 199711L when compiling a C ++ translation unit

So your code would be:

if(!Logical)
  printf("Not logical value at line number %d \n",__LINE__);
Brian R. Bondy
+7  A: 

Use __LINE__ (that's double-underscore LINE double-underscore), the preprocessor will replace it with the line number on which it is encountered.

meagar
+35  A: 

You should use the preprocessor macro __LINE__ and __FILE__. They are predefined macros and part of the C/C++ standard. During preprocessing, they are replaced respectively by a constant string holding an integer representing the current line number and by the current file name.

Others preprocessor variables :

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

Your code will be :

if(!Logical)
  printf("Not logical value at line number %d in file %s\n", __LINE__, __FILE__);
madgnome
C99 uses \_\_func\_\_ rather than \_\_FUNCTION\_\_, which AFAIK is partially deprecated. The difference can break your code, because \_\_func\_\_ cannot be used for C's constant string concatenation.
Joseph Quinsey
Reference from GCC manual: "\_\_FUNCTION\_\_ and \_\_PRETTY\_FUNCTION\_\_ were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like \_\_func\_\_. In C++, \_\_FUNCTION\_\_ and \_\_PRETTY\_FUNCTION\_\_ have always been variables."
Joseph Quinsey
+6  A: 

Checkout __FILE__ and __LINE__ macros

Anton
A: 

Try __FILE__ and __LINE__.
You might also find __DATE__ and __TIME__ useful.
Though unless you have to debug a program on the clientside and thus need to log these informations you should use normal debugging.

Sanctus2099
Why was I voted down on this and why did mmyers edit my post?
Sanctus2099
@Sanctus2099: It was edited, because Markdown transformed your double underscores to display FILE and LINE in bold font (don't you check how your answer looks like?). Another point might be (at least it looks to me this way now) that you gave an answer 1 hour after an already correct answer was given, so you added no value.
Felix Kling
Double underscore is markup syntax for __bold__. In order to properly display double underscores, you must escape them (like this: \\_\\_) or use backticks to mark them as `raw code` (like this: \`\_\_\`). @mmyers attempted to help, but he only escaped one of the underscores, and thus you were left with the markup syntax for _italics_. Downvotes are a bit harsh here, though, I agree.
Matt B.
Okay I didn't realize the thing about double underscores turning text into bold and I had to go and didn't have time to look at how my answer was looking. I do understand now. Even if my answer was an hour late it was still a good answer. It didn't add any value but it wasn't wrong either so no reason for downvote. That's what you get for trying to help...
Sanctus2099
@Sanctus2099 Some people are quick to down-vote, that's why it's important to make sure your answer is correct. In this case, you posted a wrong answer and left it unedited for 4 hours. You've got nobody to blame but yourself.
meagar
Sorry, it looked fine in the preview. My apologies.
Michael Myers