×

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

C++ Exception Handling

Exception is an unexpected problem that occurs at program run time. This problem might include condition such as division by zero, running out of memory space, array out of bonds, etc....

2 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.