Loading

Preprocessor Directives

#define


Example 1

// Define a constant value
#define PI 3.14159

Example 2

// Define a parameterized macro
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
// Use the macro with doubles or ints
double dMaxDouble   = MAX(dA,dB);
int iMaxInt         = MAX(iA,iB);

Example 3

// Define a replacement over more than one line by putting \ at the end of each line.
#define MY_MESSAGE "This is my \
really long message that \
stretches over several lines."

Example 4

// Define a null replacement
#define THIS_IS_NOTHING

#undef


Example 1

// Tell the preprocessor to stop using a definition
#define PI 3.14159
// This constant gets set via the #define directive
const int kiFirstPi = PI;
#undef
// This constant does not get set and should cause an error
const int kiSecondPi = PI;

#include


Example 1

// Include the header for the math library
#include <cmath>
// Now we can call math functions
double dSin = sin(1.23);

Example 2

// Include one a header from my own library
#include "CMyClass.h"

Example 3

// Include a header from a relative path
#include "MyLib\MyHeaders\CMyClass.h"

Example 4

// Include a header from a fully defined path
#include "C:\Dev\C++\MyLib\MyHeaders\CMyClass.h"

#if


Example 1

// Simple conditioanl
#if 1
    // This is some code that we might want to include or exclude
    // Change the 1 above to 0 to exclude this from compilation
#endif

#endif


Example 1

// Exclude this block of code from compilation
#if 0
    // Code that will not be compiled
#endif

#ifndef


Example 1

// This is most commonly used for header guards
// against multiple inclusions

// Below is the header file for "CMyClass.h"
#ifndef CMY_CLASS_H
#define CMY_CLASS_H

class CMyClass {
    // header code
};

#endif

#ifdef


Example 1

// This does not compile since MY_SWITCH was not yet defined
#ifdef MY_SWITCH
    // Code that does not get compiled
#endif

#define MY_SWITCH
#ifdef MY_SWITCH
    // Code that does get compiled
#endif
#undef MY_SWITCH

#ifdef MY_SWITCH
    // More code that does not get compiled because of #undef
#endif

Example 2

// This directive is often used to compile
// sections of code for debug builds only.
#ifdef _DEBUG
    // Code for debugging only
#endif

#else


Example 1

// Compile different sections of code depending on the compiler
#if MICROSOFT
    // Microsoft-specific code
#else
    // Code for other compilers
#endif

#elif


Example 1

// Compile different sections of code depending on the compiler
#if MICROSOFT
    // Microsoft-specific code
#elif BORLAND
    // Borland-specific code
#elif GCC
    // gcc-specific code
#else
    // Code for other compilers
#endif

#error


Example 1

// Stops compilation and displays the error message
#ifdef _CONSOLE
    // Some code for console applications
#else
    #error Console application required!
#endif

#line


Example 1

// Change the source line for the __LINE__ macro
#line 834

Example 2

// Change the source line and file for the __LINE__ and __FILE__ macros
#line 17 "MyCodeFile.cpp"

#pragma


Example 1

// These are compiler-specific directives-ignored if not
// recognized by the compiler

// This is the equivalent of a header guard, but is
//Visual C++-specific
#pragma once

operator #


Example 1

// Used to replace with a string literal
#define MY_NAME(user) #user

// The line below is the same as char* cpMyName = "XoaX.net";
char* cpMyName = MY_NAME(XoaX.net);

Example 2

// Creating a path with multiple replacements
#define MY_PATH(user,loc) "C:\" #user "\projects\" #loc

// The line below is the same as
// char* cpMyPath = "C\XoaX.net\projects\src"
char* cpMyPath = MY_PATH(XoaX.net, src);

operator ##


Example 1

// A simple concatenation example
#define MY_CONCAT(a,b) a ## b

char caTitle[] = "XoaX.net Video Tutorials";

// This concatenates 1 and 3 to create 13 and access the 'o'
std::cout << caTitle[MY_CONCAT(1,3)] << std::endl;

defined


Example 1

// We can use this as an alternative to #ifdef
#if defined(MY_DEF)
    // Some code
#endif

Example 2

// We can use "defined" in more general situations
// with Boolean operations
#if (defined(DEF1) && !defined(DEF2)) || defined(DEF3)
    // Some code
#endif

Null directive


Example 1

// This has no effect
#

Predefined Names


Example 1

// The name __LINE__ gives the current line in the file
const unsigned int kuiLineNumber = __LINE__;

Example 2

// The name __FILE__ gives the current file
char* cpFileName = __FILE__;

Example 3

// The name __DATE__ gives the current date
char* cpDate = __DATE__;

Example 4

// The name __TIME__ gives the current time
char* cpTime = __TIME__;

Example 5

// The name __cplusplus is defined as true (nonzero) when compiling C++
#if __cplusplus
    // Some C++ specific code
#endif

Example 6

// The name NULL is defined as zero and is usually used with pointers
int* ipIntPtr = NULL;