C++ preprocessor
preprocessor
#include<bits/stdc++.h>
using namespace std;
#define NUM 10
#define STR "String"
#define SUM(x,y) ((x) + (y)) // macro definition by #define
//#undef NUM --> you can undef preprocessor
void main()
{
cout << NUM;
cout << STR;
cout << SUM(2,4);
#if NUM <40
cout << "under 40" <<endl;
#elif NUM > 40
cout << "exceed 40" << endl;
#else
cout << NUM << endl;
#endif
}
- Code Execution
- Preprocessor > Compiler > Linker > Generate Executable file
- the preprocessor is executed before compiling
- Preprocessing statements are defined by #.
- Do not add a ; semicolon at the end of the preprocessing statement.
define
#define
keyword : Declare content to be used as a keyword- Used for macro
- When declaring macro, parentheses are processed considering operator precedence
undef
#undef
: delete keywords declared with #define
#include<bits/stdc++.h>
using namespace std;
#define NUM 40
void main()
{
#ifdef NUM
cout << NUM << "is exist" << endl;
#else
cout << NUM << "is not exist" << endl;
#endif
}
ifdef
#ifdef
: if keyword is defined with#define
, it is true
// header.h
#ifndef _TEST_H_
#define _TEST_H_
/// test code
#endif
ifndef
#ifndef
: true if the keyword is not declared with a #define statement- Prevents duplicate definitions
- avoid redefinition
- avoid predefinition
- EXAMPLE
- #ifndef TEST_H : Check if the
_TEST_H_
keyword is declared - #define TEST_H : If there is no
_TEST_H_
keyword, define_TEST_H_
/// test code
-
#endif
- When accessing the
///test code
again later - Because
_TEST_H_
is already defined by the condition(#ifndef TEST_H), the define statement below is not processed.
// header.h
#pragma once
pragma once
#pragma
command: It performs a special function according to the command.- Compiler dependent
- #pragma once : Include this header file only once