×

Differences between #define & const in C/C++

 Differences between #define & const in C/C++

A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation.

In this chapter, we'll learn about the member, variable, and macro definition of const data. Const and #define are both used in source code for handling constants, but they have few differences.

#define is a preprocessor, while const is keyword

#define is used to identify these values with a tag, this set string is known in C++ as a macro description, while const is a keyword used it to consistent the identification value.

#define does not control scope, while const is controlled scope

Macro can be used to include the relevant header file anywhere in the program or in other files; therefore macros are not constrained by scope, but the constant can be specified within the feature and can therefore only be accessible within the feature/domain where constant is listed, so we can conclude that const member is also managed by sector.

The use of const is often good for practice. Since we can monitor their distance when using const. When placed inside each user defines feature, its effects will be limited to the component, and if placed outside of these user defines operations, then all files are universal.

C++ example, demonstrating use of #define and const

#include <iostream>
using namespace std;
//macro definition
#define P 80
//global integer constantt
const int K = 25;
int main()
{
          //local ineteger constant`
          const  int T = 55;
          cout<<"Value of K:  "<<K<<endl;
          cout<<"Value of P:  "<<P<<endl;
          cout<<"Value of T:  "<<T<<endl; 
          return 0;
}

Output:

 Differences between #define & const in C/C++

K is a global symbolic constant in the above program but is pre-processed before compilation and P and also global constant, whereas T is local for the main function.

Macros (#define) can be redefined, but const cannot.

Macro (defined) can be reinvented anywhere in the system (by undefining and then defining), but constant cannot be redefined or reinvented even if we cannot reallocate the value in constant.

C++ Example, to redefine a defined Macro

#include <iostream>
using namespace std;
//macro definition
#define S 60
int main()
{
          cout<<"Value of S: "<<S<<endl;
          #undef S
          #define S 600
          cout<<"Value of S: "<<S<<endl;
          return 0;
}

Output:

 Differences between #define & const in C/C++

You can see here that we modified the value of S, and it successfully executed the program as well as changes the value.

C++ Example, to redefine (re-assign) a content - (ERROR)

#include <iostream>
using namespace std;
//constant int
const int T=30;
int main()
{
          cout<<"Value of T: "<<T<<endl;
          T=200;        //error, we can not assign value to const
          cout<<"Value of T: "<<T<<endl;
          return 0;
}

Output:

 Differences between #define & const in C/C++

You can see here that we did not affect the periodic value, rather here we've tried to change T 's value, and there's a compilation error.


Related Topics

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.