Are there any pre-defined variables in C?

L

Leiji Liu

Hi,

I am curious if there are any pre-defined varables (constants?) in C? I saw
some code with __LINE__, __FILE__, etc. Are those located in some include
files?

LL
 
A

Andreas Kahari

Hi,

I am curious if there are any pre-defined varables (constants?) in C? I saw
some code with __LINE__, __FILE__, etc. Are those located in some include
files?

LL

__TIME__, __DATE__, __FILE__, __LINE__. These are not variables
but some of the pre-defined macro names in C (see 6.10.8 in the
standard). These hold the values you might think they would
hold at the time of translation (compilation).

__func__ is a pre-defined identifier and behaves as if it was
declared as

static const char __func__[] = "function name";

(see 6.4.2.2), where "function name" is the name of the current
function.

They are not declared in any header.
 
L

Leiji Liu

Thank you, Andreas, I got it.
LL

Andreas Kahari said:
Hi,

I am curious if there are any pre-defined varables (constants?) in C? I saw
some code with __LINE__, __FILE__, etc. Are those located in some include
files?

LL

__TIME__, __DATE__, __FILE__, __LINE__. These are not variables
but some of the pre-defined macro names in C (see 6.10.8 in the
standard). These hold the values you might think they would
hold at the time of translation (compilation).

__func__ is a pre-defined identifier and behaves as if it was
declared as

static const char __func__[] = "function name";

(see 6.4.2.2), where "function name" is the name of the current
function.

They are not declared in any header.
 
L

LibraryUser

Leiji said:
I am curious if there are any pre-defined varables (constants?)
in C? I saw some code with __LINE__, __FILE__, etc. Are those
located in some include files?

No. You should look these things up in the standard. From N869:

6.10.8 Predefined macro names

[#1] The following macro names shall be defined by the
implementation:

__LINE__ The presumed line number (within the current source
file) of the current source line (a decimal
constant).137)

__FILE__ The presumed name of the current source file (a
character string literal).137)

__DATE__ The date of translation of the source file: a
character string literal of the form "Mmm dd yyyy",
where the names of the months are the same as those
generated by the asctime function, and the first
character of dd is a space character if the value
is less than 10. If the date of translation is not
available, an implementation-defined valid date
shall be supplied.

__TIME__ The time of translation of the source file: a
character string literal of the form "hh:mm:ss" as
in the time generated by the asctime function. If
the time of translation is not available, an
implementation-defined valid time shall be
supplied.

__STDC__ The decimal constant 1, intended to indicate a
conforming implementation.

__STDC_VERSION__ The decimal constant 199901L.138)

[#2] The following macro names are conditionally defined by
the implementation:

__STDC_ISO_10646__ A decimal constant of the form yyyymmL
(for example, 199712L), intended to
indicate that values of type wchar_t are
the coded representations of the
characters defined by ISO/IEC 10646,
along with all amendments and technical
corrigenda as of the specified year and
month.

__STDC_IEC_559__ The decimal constant 1, intended to
indicate conformance to the
specifications in annex F (IEC 60559
floating-point arithmetic).

__STDC_IEC_559_COMPLEX__ The decimal constant 1, intended to
indicate adherence to the specifications
in informative annex G (IEC 60559
compatible complex arithmetic).

[#3] The values of the predefined macros (except for
__LINE__ and __FILE__) remain constant throughout the
translation unit.

[#4] None of these macro names, nor the identifier defined,
shall be the subject of a #define or a #undef preprocessing
directive. Any other predefined macro names shall begin
with a leading underscore followed by an uppercase letter or
a second underscore.

____________________

136An implementation is not required to perform macro
replacement in pragmas, but it is permitted except for in
standard pragmas (where STDC immediately follows pragma).
If the result of macro replacement in a non-standard
pragma has the same form as a standard pragma, the
behavior is still implementation-defined; an
implementation is permitted to behave as if it were the
standard pragma, but is not required to.

137The presumed line number and source file name can be
changed by the #line directive.

138This macro was not specified in ISO/IEC 9899:1990 and was
specified as 199409L in ISO/IEC 9899/AMD1:1995 |
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top