The #pragma preprocessor directives C program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Printing date, time, version and file name using the #pragma message preprocessor directives
To show: How to use the C/C++ #pragma message preprocessor directive
The #pragma directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages. Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler. A typical use of the message pragma is to display informational messages at compile time
// The #pragma directives
#include <stdio.h>
#include <stdlib.h>
// displays either "You are compiling using version xxx of BC++" (where xxx is the version number)
// or "This compiler is not Borland C++", date, time console or not... by using several related predefined macro such as __DATE__ etc
#ifdef __BORLANDC__
#pragma message You are compiling using Borland C++ version __BORLANDC__.
#else
#pragma message("")
#pragma message ("This compiler is not Borland C++")
#endif
#pragma message ("Time:" __TIME__)
#pragma message ("Date:" __DATE__)
#pragma message( "Compiling: " __FILE__ )
#pragma message( "Last modified on: " __TIMESTAMP__ )
#pragma message("")
int main(void)
{
return 0;
}
Output example:
// No output
You can see the following statements in the output window after the compilation.
1>------ Build started: Project: myaddr, Configuration: Debug Win32 ------
1>Compiling...
1>myaddr.cpp
1>This compiler is not Borland C++
1>Time:19:44:20
1>Date:Nov 16 2006
1>Compiling: f:\vc2005project\myaddr\myaddr\myaddr.cpp
1>Last modified on: Thu Nov 16 18:45:37 2006
1>Build log was saved at "file://f:\vc2005project\myaddr\myaddr\Debug\BuildLog.htm"
1>myaddr - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
The Microsoft C and C++ compilers recognize the following pragmas:
alloc_text - Names the code section where the specified function definitions are to reside. The pragma must occur between a function declarator and the function definition for the named functions.
auto_inline - Excludes any functions defined within the range where off is specified from being considered as candidates for automatic inline expansion.
bss_seg - Specifies the segment where uninitialized variables are stored in the .obj file.
check_stack - Instructs the compiler to turn off stack probes if off (or â€") is specified, or to turn on stack probes if on (or +) is specified.
code_seg - Specifies the segment where functions are stored in the .obj file.
comment - Places a comment record into an object file or executable file.
component - Controls the collecting of browse information or dependency information from within source files.
conform (Supported only by the C++ compiler) - Specifies the run-time behavior of the /Zc:forScope compiler option.
const_seg - Specifies the segment where const variables are stored in the .obj file.
data_seg - Specifies the data segment where initialized variables are stored in the .obj file.
deprecated - The deprecated pragma lets you indicate that a function, type, or any other identifier may no longer be supported in a future release or should no longer be used.
fenv_access - Disables (ON) or enables (OFF) optimizations that could change flag tests and mode changes.
float_control - Specifies floating-point behavior for a function.
fp_contract - Determines whether floating-point contraction will take place.
function - Specifies that calls to functions specified in the pragma's argument list be generated.
hdrstop - Gives you additional control over pre-compilation file names and over the location at which the compilation state is saved.
include_alias - Specifies that short_filename is to be used as an alias for long_filename.
init_seg (Supported only by the C++ compiler) - Specifies a keyword or code section that affects the order in which startup code is executed.
inline_depth - Specifies the inline heuristic search depth, such that, no function will be inlined if it is at a depth (in the call graph) greater than n.
inline_recursion - Controls the inline expansion of direct or mutually recursive function calls.
intrinsic - Specifies that calls to functions specified in the pragma's argument list are intrinsic.
make_public - Indicates that a native type should have public assembly accessibility.
managed - Enable function-level control for compiling functions as managed or unmanaged.
message - Sends a string literal to the standard output without terminating the compilation.
omp - Takes one or more OpenMP directives, along with any optional directive clauses.
once - Specifies that the file will be included (opened) only once by the compiler when compiling a source code file.
optimize - Specifies optimizations to be performed on a function-by-function basis.
pack - Specifies packing alignment for structure, union, and class members.
pointers_to_members (Supported only by the C++ compiler) - Specifies whether a pointer to a class member can be declared before its associated class definition and is used to control the pointer size and the code required to interpret the pointer.
pop_macro - Sets the value of the macro_name macro to the value on the top of the stack for this macro.
push_macro - Saves the value of the macro_name macro on the top of the stack for this macro.
region, endregion - #pragma region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.
runtime_checks - Disables or restores the /RTC settings.
section - Creates a section in an .obj file.
setlocale - Defines the locale (Country/Region and language) to be used when translating wide-character constants and string literals.
unmanaged - Enable function-level control for compiling functions as managed or unmanaged.
vtordisp (Supported only by the C++ compiler) - Enables the addition of the hidden vtordisp construction/destruction displacement member.
warning - Allows selective modification of the behavior of compiler warning messages.