Conditional compilation is used to exclude code segments from specific builds. For instance, in debug mode you might include additional code such as an assertion macro which you wouldn't really want in the release code.
The following example declares and defines an ASSERT() macro when the program is compiled in debug mode (when DEBUG is defined). In non-debug mode (when DEBUG is not defined), all calls to ASSERT() will be ignored during compilation because there is no implementation provided.
#ifdef DEBUG
#define ASSERT(x) \
if(!(x)) \
{ \
std::cout << "ERROR!! Assert " << #x << " failed\n"; \
std::cout << "on line " << __LINE__ << std::endl; \
std::cout << "in file " << __FILE__ << std::endl; \
}
#else
#define ASSERT( x )
#endif _DEBUG
Within your code you can call ASSERT() to ensure your invariants are true whilst in debug mode:
int x=1, y=1;
ASSERT( x == y );
// .. remainder of code...
In debug mode, the compiler expands the code for you, just as if you'd written the following:
int x=1, y=1;
if(!( x==y ))
{
std::cout << "ERROR!! Assert " << #x << " failed\n";
std::cout << "on line " << __LINE__ << std::endl;
std::cout << "in file " << __FILE__ << std::endl;
}
// .. remainder of code...
But in release mode there is no macro to expand, so the assert is effectively ignored:
int x=1; y=1
// .. remainder of code...
Other uses of conditional compilation include catering for UNICODE or MCBS encoding of strings. If your program uses one or the other, you can use macros to ensure the appropriate string handling calls and declarations are made.
They are also used to "guard" include files to ensure an included file is never included more than once during compilation. This is important when several files include the same file for the purpose of syntax checking. During compilation, however, only one copy of the file needs to be physically included. If multiple inclusions were not guarded against, the compiler would try to include several declarations of the same functions and, even though they all come from the exact same file, the compiler treats them as separate entities and immediately stops the compilation. The same thing occurs when a function is declared twice in two separate files.
// myclass.h
#ifndef _myclass_h_ // First time around, this will not be defined.
#define _myclass_h_ // Now it is defined.
// Declarations (with or without implementations) go here.
#endif _myclass_h_ // Marks the end of the inclusion.
When the compiler "sees" this file for the first time, it will be included because _myclass_h_ would be undefined first time around. But all subsequent inclusions will be ignored because _myclass_h_ was defined during the first inclusion.
C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives. For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation: #ifdef __unix__ #include <unistd.h> #elif defined _WIN32 #include <windows.h> #endif The definition of __unix__ and _WIN32 must be mutually exclusive.
Compilation is the process of translating source files into object files.
Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.
An unconditional goto is a goto that has no associated conditional expression. The following example demonstrates conditional and unconditional goto statements. int x=rand(); if (x) goto label_1; // conditional goto (when x is non-zero) else goto label_2; // conditional goto (when x is zero) label_1: // ... goto label_3; // unconditional goto (jump past label_2) label_2: // ... label_3: // ...
Unconditional statements are statements that are invoked unconditionally. Conditional statements have a controlling expression, while unconditional statements do not. For example: void f (bool b) { if (b==true) do_something(); // conditional statement (controlled by the expression b==true) do_something_else(); // unconditional (executes regardless of b's value) }
They are equally portable. Conditional compilation is supported by both languages.
C++ has no platform dependency. If a compiler exists for a platform (and few don't) code can be written for that platform. Where platforms have different methods to do the same thing, conditional compilation can be used to cater for those differences, thus the same source code can be compiled on any platform simply by changing the definitions used by the conditional compilation directives. For instance, a program that caters for Unix and Windows platforms might contain the following conditional compilation: #ifdef __unix__ #include <unistd.h> #elif defined _WIN32 #include <windows.h> #endif The definition of __unix__ and _WIN32 must be mutually exclusive.
Compilation is the process of translating source files into object files.
Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.
An unconditional goto is a goto that has no associated conditional expression. The following example demonstrates conditional and unconditional goto statements. int x=rand(); if (x) goto label_1; // conditional goto (when x is non-zero) else goto label_2; // conditional goto (when x is zero) label_1: // ... goto label_3; // unconditional goto (jump past label_2) label_2: // ... label_3: // ...
Simple 5,600 Plus 1,100 Example:5,600+1,100=6,700
the converse of this conditional is true
The conditional constant= 1.8*1010
Unconditional statements are statements that are invoked unconditionally. Conditional statements have a controlling expression, while unconditional statements do not. For example: void f (bool b) { if (b==true) do_something(); // conditional statement (controlled by the expression b==true) do_something_else(); // unconditional (executes regardless of b's value) }
It's an example of a simple problem in addition. The solution is: 604
It's a simple example of an addition problem. The solution is: 720
There is only one type: the one that creates an object module from a source file.