×

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++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.