
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between #define and const Keyword in C++
In C++, both #define and const are used to define constants in a program. The #define is a preprocessor directive that creates macros with their fixed values whereas const is a keyword which declare value of variable as constant, meaning its value cannot be changed after intialization. Therefore they have different use cases in different scenarios. In this article, we will learn the differences between these two in detail.
#define in C++
The #define is a preprocessor directive that is used to define or assign macros ( name or string ) with a constant value. So wherever the macro occurs in a code, it gets replaced with its constant value assigned to it before compilation. These macros can be further used anywhere in the program or even in other files, therefore, this is not scope-controlled. But this does not create a real variable. It simply tells the preprocessor to replace a name with a value or code before compilation.
Syntax
Here is the following synatx of #define, which declares macro (name or string not a variable) with its fixed value. Here the macro_name will get replaced with their value during preprocessing (before compilation).
#define macro_name value
Example
Here is the following example code, showcasing how the macro is declared using #define with its value, and is being used in calculation of finding a sum.
#include <iostream> #define A 10 // constant macro; wherever A appears, it will be replaced by the value 10 #define B 20 //wherever B appears, it will be replaced by the value 20 #define SUM (A + B) // wherver SUM appears, it will replaced by value (A + B) using namespace std; int main() { cout << "A: " << A << endl; cout << "B: " << B << endl; cout << "Sum of A and B: " << SUM << endl; return 0; }
Output
A: 10 B: 20 Sum of A and B: 30
const in C++
The const is a keyword that is used to declare a variable constant, meaning its value cannot be changed or modified later, once initialized with const. This can be declared both inside and outside a function, thus, its accessibility depends on the scope where it is defined, and therefore it is scope-controlled. It is used to declare an actual variable in the language, which you can use to take its address, pass it around, cast it, convert it, etc.
Syntax
Here is the following syntax of const keyword, which declares the value of variable as constant.
const data_type variable_name = value;
Example
Here is the following example code showcasing the use of const keyword.
#include <iostream> using namespace std; int main() { const int A = 10; // a constant integer with value 10 const int B = 20; // a constant integer with value 20 // as sum will also int, therefore declared it with int, and const means we can't further change the value of sum const int Sum = A + B; cout << "Sum is: " << Sum << endl; return 0; }
Output
Sum is: 30
Note: Without compiler optimizations, there is a hidden cost of memory allocation associated with both of these in different cases. But with proper optimizations, they are more likely to be the same. Also, preprocessor macros don't have any scope, while const values do.