Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C preprocessor
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Predefined macros=== The preprocessor provides some macro definitions automatically. The C standard specifies that <code>__FILE__</code> expands to the name of the file being processed and <code>__LINE__</code> expands to the number of the line that contains the directive. The following macro, <code>DEBUGPRINT</code>, formats and prints a message with the file name and line number. <syntaxhighlight lang="cpp"> #define DEBUGPRINT(_fmt, ...) printf("[%s:%d]: " _fmt, __FILE__, __LINE__, __VA_ARGS__) </syntaxhighlight> For the example code below that is on line 30 of file <code>util.c</code> and for count 123, the output is: <code>[util.c:30]: count=123</code>. <syntaxhighlight lang="cpp"> DEBUGPRINT("count=%d\n", count); </syntaxhighlight> The first [[ANSI C#C89 and C90|C Standard]] specified that <code>__STDC__</code> expand to "1" if the implementation conforms to the ISO standard and "0" otherwise and that <code>__STDC_VERSION__</code> expand to a numeric literal specifying the version of the standard supported by the implementation. Standard C++ compilers support the <code>__cplusplus</code> macro. Compilers running in non-standard mode must not set these macros or must define others to signal the differences. Other standard macros include <code>__DATE__</code>, the current date, and <code>__TIME__</code>, the current time. The second edition of the C Standard, [[C99]], added support for <code>__func__</code>, which contains the name of the function definition within which it is contained, but because the preprocessor is [[wikt: agnostic|agnostic]] to the grammar of C, this must be done in the compiler itself using a variable local to the function. One little-known usage pattern of the C preprocessor is known as [[X Macro|X-Macros]].<ref name="X_macros">[http://liw.iki.fi/liw/texts/cpp-trick.html Wirzenius, Lars. C "Preprocessor Trick For Implementing Similar Data Types". Retrieved January 9, 2011]</ref><ref>{{cite journal | last = Meyers | first = Randy |date=May 2001 | title = The New C: X Macros | journal = Dr. Dobb's Journal | url = http://www.ddj.com/cpp/184401387 | access-date = 1 May 2008 }}</ref><ref>{{cite journal | last = Beal | first = Stephan |date=August 2004 | title = Supermacros | url = http://wanderinghorse.net/computing/papers/#supermacros | access-date = 27 October 2008 }}</ref> An X-Macro is a [[header file]]. Commonly, these use the extension <code>.def</code> instead of the traditional <code>.h</code> . This file contains a list of similar macro calls, which can be referred to as "component macros." The include file is then referenced repeatedly. Many compilers define additional, non-standard macros. A common reference for these macros is the [http://predef.sourceforge.net/ Pre-defined C/C++ Compiler Macros project], which lists "various pre-defined compiler macros that can be used to identify standards, compilers, operating systems, hardware architectures, and even basic run-time libraries at compile-time." Most compilers targeting [[Microsoft Windows]] implicitly define <code>_WIN32</code>.<ref>[http://msdn.microsoft.com/en-us/library/b0084kay.aspx List of predefined ANSI C and Microsoft C++ implementation macros.]</ref> This allows code, including preprocessor commands, to compile only when targeting Windows systems. A few compilers define <code>WIN32</code> instead. For such compilers that do not implicitly define the <code>_WIN32</code> macro, it can be specified on the compiler's command line, using <code>-D_WIN32</code>. <syntaxhighlight lang="cpp"> #ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */ # include <unistd.h> #elif defined _WIN32 /* _WIN32 is usually defined by compilers targeting 32 or 64 bit Windows systems */ # include <windows.h> #endif </syntaxhighlight> The example code tests if a macro <code>__unix__</code> is defined. If it is, the file <code><unistd.h></code> is then included. Otherwise, it tests if a macro <code>_WIN32</code> is defined instead. If it is, the file <code><windows.h></code> is then included.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)