Predefined Macros in C ++

Source: Internet
Author: User
Tags emit month name microsoft c
 

C/C ++ grand all (reproduced, original post address http://www.cnblogs.com/sevencat/archive/2004/06/10/14872.html)
I. Standard definition macro
The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that implement those standards. older compilers may not provide all of them. their names all start with double underscores.

_ 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. H "is a possible expansion of this macro.

_ Line __
This macro expands to the current input line number, in the form of a decimal integer constant. while we call it a predefined macro, it's a pretty strange macro, since its "Definition" changes with each new line of source code.
_ File _ and _ line _ are useful in generating an error message to report an inconsistency detected by the program; the message can state the source line at which the inconsistency was detected. for example,

Fprintf (stderr, "internal error :"
"Negative String Length"
"% D at % s, line % d .",
Length, _ file __, _ line __);

An # include directive changes the expansions of _ file _ and _ line _ to correspond to the specified ded file. at the end of that file, when processing resumes on the input file that contained the # include Directive, the expansions of _ file _ and _ line _ revert to the values they had before the # include (but _ line _ is then incremented by one as processing moves the line after the # include ).

A # Line directive changes _ line __, and may change _ file _ as well. See line control.

C99 introduces _ FUNC __, and GCC has provided _ function _ for a long time. both of these are strings containing the name of the current function (there are slight semantic differences; see the GCC Manual ). neither of them is a macro; The Preprocessor does not know the name of the current function. they tend to be useful in conjunction with _ file _ and _ line __, though.

_ Date __
This macro expands to a String constant that describes the date on which the Preprocessor is being run. the String constant contains eleven characters and looks like "Feb 12 1996 ". if the day of the month is less than 10, it is padded with a space on the left.
If GCC cannot determine the current date, it will emit a warning message (once per compilation) and _ date _ will expand "??? ?? ???? ".

_ Time __
This macro expands to a String constant that describes the time at which the Preprocessor is being run. The String constant contains eight characters and looks like "23:59:01 ".
If GCC cannot determine the current time, it will emit a warning message (once per compilation) and _ time _ will expand "?? :?? :?? ".

_ Stdc __
In normal operation, this macro expands to the constant 1, to signify that this compiler conforms to ISO Standard C. if gnu cpp is used with a compiler other than GCC, this is not necessarily true; however, the Preprocessor always conforms to the standard unless the-traditional-CPP option is used.
This macro is not defined if the-traditional-CPP option is used.

On some hosts, the system compiler uses a different convention, where _ stdc _ is normally 0, but is 1 if the user specifies strict conformance to the C standard. CPP follows the host Convention when processing system header files, but when processing user files _ stdc _ is always 1. this has been reported to cause problems; for instance, some versions of Solaris provide X Windows headers that have CT _ stdc _ to be either undefined or 1. see invocation.

_ Stdc_version __
This macro expands to the C standard's version number, a long integer constant of the form yyyymml where yyyy and mm are the year and month of the standard version. this signifies which version of the C standard the compiler conforms. like _ stdc __, this is not necessarily accurate for the entire implementation, unless gnu cpp is being used with GCC.
The value 199409l signifies the 1989 c Standard as amended in 1994, which is the current default; the value 199901l signifies the 1999 revision of the C standard. support for the 1999 revision is not yet complete.

This macro is not defined if the-traditional-CPP option is used, nor when compiling C ++ or objective-C.

_ Stdc_hosted __
This macro is defined, with value 1, if the compiler's target is a hosted environment. A hosted environment has the complete facilities of the Standard C library available.

_ Cplusplus
This macro is defined when the C ++ compiler is in use. you can use _ cplusplus to test whether a header is compiled by a C compiler or a C ++ compiler. this macro is similar to _ stdc_version __, in that it expands to a version number. A fully conforming Implementation of the 1998 c ++ standard will define this macro to 199711l. the gnu c ++ compiler is not yet fully conforming, So it uses 1 instead. we hope to complete our implementation in the near future.

_ Objc __
This macro is defined, with value 1, when the objective-C compiler is in use. you can use _ objc _ to test whether a header is compiled by a C compiler or a objective-C compiler.

_ Cycler __
This macro is defined with value 1 when preprocessing assembly language.

General predefined Macros in GCC
_ Gnuc __
_ Gnuc_minor __
_ Gnuc_patchlevel __
These macros are defined by all GNU compilers that use the C Preprocessor: C, C ++, and objective-C. their values are the major version, minor version, and patch level of the compiler, as Integer constants. for example, GCC 3.2.1 will define _ gnuc _ to 3, _ gnuc_minor _ to 2, and _ gnuc_patchlevel _ to 1. they are defined only when the entire compiler is in use; if you invoke the Preprocessor directly, they are not defined.
_ Gnuc_patchlevel _ is new to GCC 3.0; it is also present in the widely-used development snapshots leading up to 3.0 (which identify themselves as GCC 2.96 or 2.97, depending on which snapshot you have ).

If all you need to know is whether or not your program is being compiled by GCC, you can simply test _ gnuc __. if you need to write code which depends on a specific version, you must be more careful. each time the minor version is increased, the patch level is reset to zero; each time the major version is increased (which happens rarely), the minor version and patch level are reset. if you wish to use the predefined macros directly in the conditional, you will need to write it like this:

/* Test for GCC> 3.2.0 */
# If _ gnuc _> 3 |/
(_ Gnuc _ = 3 & (_ gnuc_minor _> 2 |/
(_ Gnuc_minor _ = 2 &&/
_ Gnuc_patchlevel _> 0 ))

Another approach is to use the predefined macros to calculate a single number, then compare that against a threshold:

# Define gcc_version (_ gnuc _ * 10000/
+ _ Gnuc_minor _ * 100/
+ _ Gnuc_patchlevel __)
...
/* Test for GCC> 3.2.0 */
# If gcc_version> 30200

Please people find this form easier to understand.

_ Gnug __
The gnu c ++ compiler defines this. Testing it is equivalent to testing (_ gnuc _ & _ cplusplus ).

_ Strict_ansi __
GCC defines this macro if and only if the-ANSI switch, or a-STD switch specifying strict conformance to some version of Iso c, was specified when GCC was invoked. it is defined to 1. this macro exists primarily to direct GNU libc's header files to restrict their definitions to the minimal set found in the 1989 c Standard.

_ Base_file __
This macro expands to the name of the main input file, in the form of a C string constant. This is the source file that was specified on the command line of the Preprocessor or C compiler.

_ Include_level __
This macro expands to a decimal integer constant that represents the depth of nesting in include files. the value of this macro is incremented on every # include Directive and decremented at the end of every encoded file. it starts out at 0, it's value within the Base file specified on the command line.

_ Elf __
This macro is defined if the target uses the elf object format.

_ Version __
This macro expands to a String constant which describes the version of the compiler in use. you shoshould not rely on its contents having any Participant Form, but it can be counted on to contain at least the release number.

_ Optimize __
_ Optimize_size __
_ No_inline __
These macros describe the compilation mode. _ optimize _ is defined in all optimizing compilations. _ optimize_size _ is defined if the compiler is optimizing for size, not speed. _ no_inline _ is defined if no functions will be inlined into their callers (when not optimizing, or when inlining has been specifically disabled by-fno-inline ).
These macros cause certain GNU header files to provide optimized definitions, using macros or inline functions, of system library functions. you shoshould not use these macros in any way unless you make sure that programs will execute with the same effect whether or not they are defined. if they are defined, their value is 1.

_ Char_unsigned __
GCC defines this macro if and only if the data type char is unsigned on the target machine. it exists to cause the standard header file limits. h to work correctly. you shoshould not use this macro yourself; instead, refer to the standard macros defined in limits. h.

_ Wchar_unsigned __
Like _ char_unsigned __, this macro is defined if and only if the data type wchar_t is unsigned and the front-end is in C ++ mode.

_ Register_prefix __
This macro expands to a single token (not a String constant) which is the prefix applied to CPU Register names in assembly language for this target. you can use it to write assembly that is usable in multiple environments. for example, in the m68k-aout environment it expands to nothing, but in the m68k-coff environment it expands to a single %.

_ User_label_prefix __
This macro expands to a single token which is the prefix applied to user labels (symbols visible to C Code) in assembly. for example, in the m68k-aout environment it expands to an _, but in the m68k-coff environment it expands to nothing.
This macro will have the correct definition even if-F (no-) Underscores is in use, but it will not be correct if target-specific options that adjust this prefix are used (e.g. the OSF/rose-MnO-underscores option ).

_ Size_type __
_ Ptrdiff_type __
_ Wchar_type __
_ Wint_type __
These macros are defined to the correct underlying types for the size_t, ptrdiff_t, wchar_t, and wint_t typedefs, respectively. they exist to make the standard header files stddef. H and wchar. h work correctly. you shoshould not use these macros directly; instead, include the appropriate headers and use the typedefs.

_ Char_bit __
Defined to the number of BITs used in the representation of the char data type. it exists to make the standard header given numerical limits work correctly. you shoshould not use this macro directly; instead, include the appropriate headers.

_ Schar_max __
_ Wchar_max __
_ Shrt_max __
_ Int_max __
_ Long_max __
_ Long_long_max __
Defined to the maximum value of the signed Char, wchar_t, signed short, signed int, signed long, and signed long types respectively. they exist to make the standard header given numerical limits work correctly. you shoshould not use these macros directly; instead, include the appropriate headers.

_ Using_sjlj_exceptions __
This macro is defined, with value 1, if the compiler uses the old mechanic Based on setjmp and longjmp for exception handling.

_ Next_runtime __
This macro is defined, with value 1, if (and only if) The next Runtime (as in-fnext-runtime) is in use for objective-C. if the GNU Runtime is used, this macro is not defined, so that you can use this macro to determine which Runtime (next or GNU) is being used.

_ Lp64 __
_ Lp64
These macros are defined, with value 1, if (and only if) The compilation is for a target where long int and pointer both use 64-bits and INT uses 32-bit.

Macro definition of VC
Standard macros of VC
The compiler recognizes 10 predefined ansi c macros, and the Microsoft C ++ implementation provides several more. these macros take no arguments and cannot be redefined. their value, must t for _ line _ and _ file __, must be constant throughout compilation. some of the predefined macros listed below are defined with multiple values. see the following tables for more information.

ANSI-compliant predefined macros

Macro description
_ Date _ the compilation date of the current source file. the date is a string literal of the form Mmm dd YYYY. the month name Mmm is the same as for dates generated by the library function asctime declared in time. h.
 
_ File _ The Name Of The current source file. _ file _ expands to a string surrounded by double quotation marks.
You can create your own wide string version of _ file _ as follows:

# Include <stdio. h>
# Define widen2 (x) L # x
# Define widen (x) widen2 (X)
# DEFINE _ wfile _ widen (_ file __)
Wchar_t * pwsz = _ wfile __;

Int main ()
{
}
 
_ Line _ the line number in the current source file. The line number is a decimal integer constant. It can be altered with a # Line directive.

_ Stdc _ indicates full conformance with the ansi c standard. defined as the integer constant 1 only if the/za compiler option is given and you are not compiling C ++ code; otherwise is undefined.

_ Time _ the most recent compilation Time of the current source file. The time is a string literal of the form hh: mm: Ss.

_ Timestamp _ the date and time of the last modification of the current source file, expressed as a string literal in the form DDD Mmm date hh: mm: SS yyyy, where DDD is the abbreviated day of the week and date is an integer from 1 to 31.

Microsoft's VC macro
_ Atl_ver defines the ATL version.
 
_ Char_unsigned default char type is unsigned. defined when/J is specified.
 
_ Counter _ expands to an integer starting with 0 and incrementing by 1 every time it is used in a compiland. _ counter _ remembers its state when using precompiled headers. if the last _ counter _ value was 4 after building a precompiled header (PCH), it will start with 5 on each PCH use.
_ Counter _ lets you generate unique variable names. You can use token pasting with a prefix to make a unique name. For example:

# Include <stdio. h>
# Define func2 (x, y) x # Y
# Define func1 (x, y) func2 (x, y)
# Define func (x) func1 (x ,__ counter __)

Int func (my_unique_prefix );
Int func (my_unique_prefix );

Int main (){
My_unique_prefix0 = 0;
Printf ("/n % d", my_unique_prefix0 );
My_unique_prefix0 ++;
Printf ("/n % d", my_unique_prefix0 );
}
 
_ Cplusplus defined for C ++ programs only.
_ Cpplib_ver defined if you include any of the C ++ standard library headers; reports which version of the dinkumware header files are present.

_ Cpprtti defined for Code Compiled with/GR (enable run-time type information ).

_ Cppunwind defined for Code Compiled with/GX (enable Exception Handling ).

_ Debug defined when compiling with/LDD,/MDD,/MLD, And/MTD.

_ DLL defined when/MD or/MDD (multithread DLL) is specified.

_ Funcdname _ valid only within a function and returns the decorated name of the enclosing function (as a string ). _ funcdname _ is not expanded if you use the/EP or/P compiler option.

_ Funcsig _ valid only within a function and returns the signature of the enclosing function (as a string ). _ funcsig _ is not expanded if you use the/EP or/P compiler option.

_ Function _ valid only within a function and returns the undecorated name of the enclosing function (as a string ). _ function _ is not expanded if you use the/EP or/P compiler option.

_ M_alpha defined for DEC Alpha platforms. It is defined as 1 by the Alpha compiler, and it is not defined if another compiler is used.

_ M_ix86 defined for x86 processors. See values for _ m_ix86 for more details.

_ M_ia64 defined for 64-bit processors.

_ M_mppc defined for Power Macintosh platforms (no longer supported ).

_ M_mrx000 defined for MIPS platforms (no longer supported ).

_ M_ppc defined for PowerPC platforms (no longer supported ).
 
_ Managed defined to be 1 when/CLR is specified.

_ Mfc_ver defines the MFC version. For example, 0x0700 represents MFC version 7.

_ Msc_extensions this macro is defined when compiling with the/ze compiler option (the default). Its value, when defined, is 1.

_ Msc_ver defines the major and minor versions of the compiler. for example, 1300 for Microsoft Visual C ++. net. 1300 represents version 13 and no point release. this represents the fact that there have been a total of 13 releases of the compiler.
If you type Cl /? At the command line, you will see the full version for the compiler you are using.
 
_ Msvc_runtime_checks defined when one of the/RTC compiler options is specified.
_ Mt defined when/MD or/MDD (multithreaded DLL) or/Mt or/MTD (multithreaded) is specified.
_ Wchar_t_defined
And

_ Native_wchar_t_defined
Defined when wchar_t is defined. Typically, wchar_t is defined when you use/ZC: wchar_t or when typedef unsigned short wchar_t; is executed in code.
 
_ Win32 defined for applications for Win32 and win64. always defined.

_ Win64 defined for applications for win64.

_ Wp64 defined when specifying/wp64.

As shown in following table, the compiler generates a value for the Preprocessor identifiers that reflect the processor option specified.

Values for _ m_ix86

Option in Development Environment command-line option resulting value
Blend/GB _ m_ix86 = 600 (default. Future compilers will emit a different value to reflect the dominant processor .)
Pentium/G5 _ m_ix86 = 500
Pentium Pro, Pentium II, and Pentium III/g6 _ m_ix86 = 600
80386/G3 _ m_ix86 = 300
80486/G4 _ m_ix86 = 400

_ Cygwin32 _ defined when cygwin is used
_ Mingw32 _ defined when mingw is used

Related Article

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.