Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++what is the type of the __LINE__ macro

Tags:

As you may see from my other questions many of you may already got the answer for this. Can you please share that knowledge to me?

like image 606
prabhakaran Avatar asked Feb 22 '11 08:02

prabhakaran


People also ask

Where is __ LINE __ defined?

__LINE__ Defined as the integer line number in the current source file. The value of the __LINE__ macro can be changed by using a #line directive. The integral type of the value of __LINE__ can vary depending on context. This macro is always defined.

What does __ file __ mean in C?

__FILE__ This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in ' #include ' or as the input file name argument. For example, "/usr/local/include/myheader.

What does __ LINE __ return?

__LINE__ Macro: __LINE__ macro contains the current line number of the program in the compilation. It gives the line number where it is called. It is used in generating log statements, error messages, throwing exceptions and debugging codes.


2 Answers

C++03 §16.8p1:

__LINE__ The line number of the current source line (a decimal constant).

This will either be int, or if INT_MAX (which is allowed to be as little as 32,767) is not big enough (… I won't ask …), then it will be long int. If it would be bigger than LONG_MAX, then you have undefined behavior, which, for once, is not a problem worth worrying about in a file of at least 2,147,483,647 lines (the minimum allowed value for LONG_MAX).

The same section also lists other macros you may be interested in.

like image 168
Fred Nurk Avatar answered Oct 16 '22 18:10

Fred Nurk


The C++ standard simply has this to say:

__LINE__: The presumed line number (within the current source file) of the current source line (an integer constant).

It does not actually state the type so it's most likely going to be the same type as an unadorned integer would be in your source code which would be an int. The fact that the upper end of the allowed range is 2G - 1 supports that (even though the lower range is 1).

The fact that #line only allows digits (no trailing U to make it unsigned) can also be read to support this.

But, that's only support. I couldn't find a definitive statement within either the C++ or C standards. It just makes sense*a that it will be translated into something like 42 when it goes through the preprocessing phase and that's what the compiler will see, treating it exactly like 42 (an int).


*a: This wouldn't be the first time my common sense was wrong, though :-)

like image 25
paxdiablo Avatar answered Oct 16 '22 20:10

paxdiablo