You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Macro, Preprocessor Directives (Including pragma) and Other Related Keywords
Macro
Macros are typically defined using the preprocessor directive#define,
which allows for text substitutionprior to the actual
compilation of the code.
They are classified as preprocessor statements.
Macros may represent simple constants or complex expressions.
When the preprocessor encounters a macro, it replaces it with the
corresponding value or code.
They enable the compilation of identical code to produce varied outcomes
based on distinct configurations.
The backslash (\) can be utilized to create multi-line macros.
Excessive use of macros may adversely affect the readability of
the code.
Function-like macros can be difficult to debug.
When a macro is defined, from its definition location to the end of the file,
all code can use it, regardless of whether it is defined inside a code block.
The scope of a macro is global.
This is because the preprocessor doesn’t understand C++ concepts like
functions.
Preprocessor Directives
A preprocessor directive is a command that instructs the C or C++
preprocessor to perform specific operationsbefore the actual
compilation of the source code begins.
These directives always start with the # symbol and are processed by
the preprocessor, which runs before the compiler.
Directives defined in one file do not have any impact on other files
(unless they are #included into another file).
Syntax
#defineMACRO_NAME replacement_text
// Function-like Macros// para_list without Type specifiers.
#definefuncName( para_list ) replacement_text
These preprocessor directives are used for conditional compilation,
allowing code to be included or excluded based on whether a macro is
defined or not.
This can be used as a header guard, similar to #pragma once, to resolve
duplicate definition problems.
However, these methods cannot completely eliminate such problems.
For example:
There are three files: Main.cpp, Fun.cpp, and Header.h.
Both Main.cpp and Fun.cpp include Header.h.
Main.cpp references something from Fun.cpp.
Header.h contains a global variable definition.
When we compile these three files, multiple definition errors occur.
Therefore, we recommend that header files only contain templates,
declarations, and type definitions.
This preprocessor directive is not a macro but is often used with
macros to include header files where macros may be defined.
#include <HeaderFile>:
This syntax instructs the preprocessor to search for the specified header
file exclusively in the standard include directories.
It is typically used for system or library headers.
#include "path/to/HeaderFile":
This syntax tells the preprocessor to look for the specified header file
in the specific path first.
If it is not found there, it then searches the standard include
directories.
Syntax
#include<HeaderFile>
#include"path/to/HeaderFile"
#pragma
Explanation
This preprocessor directive provides additional information to the
compiler, often used for controlling compiler-specific behaviors but can
also be relevant in the context of macros.
The #pragma directive itself does not belong to compiler extensions—it is a
standardized part of the C and C++ language specifications.
However, the specific #pragma directives provided by compilers are often
compiler extensions, meaning their behavior and availability depend on the
compiler.
Different compilers support different #pragma directives. The following
#pragma directives work with GCC/G++ and Clang/Clang++ but may not be
supported by other compilers.
#pragma once is supported by most modern C/C++ compilers.
Syntax
#pragma once // Ensures the file is included only once.
The ## operator is used to concatenate (paste together) two tokens into a
single token during macro expansion. This is useful for generating unique
names or combining identifiers.
#
The # operator converts a macro argument into a string literal. It is used
to create string representations of macro arguments.