codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

56 / 87 topics
55C++11 Features56Preprocessors and Macros57Templates (Function & Class Templates)58Namespaces59File Handling, Buffers, istream & ostream60Exception Handling, Asserts & Debugging61Multithreading
Tutorials/C++ Programming/Preprocessors and Macros
⚡C++ Programming

Preprocessors and Macros

Updated 2026-05-12
30 min read

Preprocessors and Macros

In this tutorial, we will explore the powerful features of C++ preprocessors. The preprocessor is a tool that processes your source code before it is compiled. It handles tasks such as macro expansion, file inclusion, conditional compilation, and more. Understanding preprocessors is crucial for writing efficient and maintainable C++ code.

Introduction

The C++ preprocessor is a separate phase in the compilation process that runs before the actual compilation of the program. It processes directives like #define, #include, #ifdef, #ifndef, and #pragma. These directives allow you to perform tasks such as defining constants, including header files, conditionally compiling code, and more.

Core Content

10.1 Preprocessor Directives

Preprocessor directives start with a hash symbol (#) and are followed by specific keywords. They instruct the preprocessor on how to process the source code.

10.1.1 #define

The #define directive is used to define macros, which can be constants or functions. Macros are expanded before the actual compilation of the program.

Example: Defining a Constant

C++
1#include <iostream>
2
3#define PI 3.14159
4
5int main() {
6 double radius = 5.0;
7 double area = PI * radius * radius;
8 std::cout << "Area of circle with radius " << radius << " is " << area << std::endl;
9 return 0;
10}
Output

Warning

Be cautious with macro functions. They do not follow the normal rules of function calls, such as argument evaluation order and operator precedence.

10.1.2 #include

The #include directive is used to include the contents of another file into your source code. This is typically used to include header files that contain declarations of functions, classes, and constants.

Example: Including a Header File

C++
1#include <iostream>
2#include "myheader.h"
3
4int main() {
5 std::cout << "Hello from myheader.h!" << std::endl;
6 return 0;
7}
Output

10.1.4 #pragma

The #pragma directive is used to issue special commands to the compiler. These commands are not part of the C++ language but are specific to the compiler.

Example: Using #pragma

C++
1#include <iostream>
2
3#pragma warning(disable : 4996) // Disable a specific warning in MSVC
4
5int main() {
6 char* str = "Hello, World!";
7 std::cout << str << std::endl;
8 return 0;
9}
Output

Tip

Include guards are essential for preventing redefinition errors in large projects.

Practical Example

Let's create a simple program that uses macros, conditional compilation, and include guards.

C++
1// config.h
2#ifndef CONFIG_H
3#define CONFIG_H
4
5#define MAX_SIZE 100
6#define DEBUG_MODE
7
8#endif // CONFIG_H
C++
1// utils.h
2#ifndef UTILS_H
3#define UTILS_H
4
5#include <iostream>
6
7void printMessage(const std::string& message) {
8 #ifdef DEBUG_MODE
9 std::cout << "DEBUG: ";
10 #endif
11 std::cout << message << std::endl;
12}
13
14#endif // UTILS_H
C++
1// main.cpp
2#include "config.h"
3#include "utils.h"
4
5int main() {
6 printMessage("Program started.");
7
8 int size = MAX_SIZE;
9 printMessage("Maximum size is: " + std::to_string(size));
10
11 return 0;
12}
Output
DEBUG: Program started.
DEBUG: Maximum size is: 100

Summary

ConceptDescription
#defineDefines macros, which can be constants or functions.
#includeIncludes the contents of another file into your source code.
#ifdef, etc.Used for conditional compilation based on whether certain macros are defined.
#pragmaIssues special commands to the compiler.
Include GuardsPrevent multiple inclusions of the same header file, avoiding redefinition errors.

What's Next?

In the next tutorial, we will explore templates, which provide a way to write generic code that can operate on different data types. Templates are a powerful feature of C++ that allows you to write more flexible and reusable code.

Stay tuned!


PreviousC++11 FeaturesNext Templates (Function & Class Templates)

Recommended Gear

C++11 FeaturesTemplates (Function & Class Templates)