×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

Difference between const and #define in C, C++ programming language

Learn: What are the differences between const data member, variable and #define (pre processor macro) in C and C++ programming language with Examples?

In this chapter, we are going to learn about const data member, variable and define macro. const and #define both are used for handle constants in source code, but they few differences.

#define is pre-processor directive while const is a keyword

#define is used to define some values with a name (string), this defined string is known as Macro definition in C, C++ while const is a keyword or used to make the value of an identifier (that is constant) constant.

#define is not scope controlled whereas const is scope controlled

Macro can be used in anywhere in the program or in other files to by including the related header file, thus macros are not scope controlled, but the constant can be declared inside the function and thus, it can be accessed only within the function/scope in which constant is declared, thus, we can say const member is always scope controlled.

Use of #define and const

Use of const is always a good habit. Because while using const we can control its scope. If it is placed inside any user define function, its effect would be localized to the function and if it is placed outside all user define functions then it becomes global to all files.

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

#include <iostream>
using namespace std;

//macro definition
#define X 30

//global integer constantt
const int Y = 10;

int main()
{
	//local ineteger constant`
	const  int Z = 30;
	
	cout<<"Value of X:  "<<X<<endl;
	cout<<"Value of Y:  "<<Y<<endl;
	cout<<"Value of Z:  "<<Z<<endl;  

	return 0;
}

Output

Value of X: 30 
Value of Y: 10 
Value of Z: 30

In above program, X is global symbolic constant, but it is pre-processed before compilation and Y and also global constant whereas Z is local for main function.

Macros (#define) can be redefined but const cannot

Macro (defined) can be redefined anywhere in the program (by un-defining and then defining) but constant cannot be re-declared or re-defined even we cannot re-assign the value in constant.

C++ Example, to redefine a defined Macro

#include <iostream>
using namespace std;

//macro definition
#define X 30

int main()
{
	cout<<"Value of X: "<<X<<endl;
	#undef X
	#define X 300
	cout<<"Value of X: "<<X<<endl;

	return 0;
}

Output

Value of X: 30
Value of X: 300

Here, you can see that we have changed (redefined) the value of X and it has changed, program executed successfully and value has changed.

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

#include <iostream>
using namespace std;

//constant int 
const int Y=10;

int main()
{
	cout<<"Value of Y: "<<Y<<endl;
	Y=100;	//error, we can not assign value to const
	cout<<"Value of Y: "<<Y<<endl;

	return 0;
}

Output

main.cpp: In function 'int main()': 
main.cpp:10:3: error: assignment of read-only variable 'Y'  
  Y=100; //error, we can not assign value to const  
   ^

Here, you can see that we cannot change the value of a constant, here we tried to change the value of Y, but there is a compilation error.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.