![]() |
In C++, constants are the tokens that are used to represent those values that cannot be changed during the program execution. It is defined only once and remains the same throughout the execution of the program. C++ provides various different methods to define constants. In this article, we will discuss such methods to define constants and see how they are different form each other. Different Ways to Define Constants in C++In general, we can define constants using the const keyword but C++ also provides more methods to define constants. Some of the methods to define constants in C++ are:
Constants defined using each of these keywords have some similar and different properties. Let’s discuss them one by one. 1. Constants Using const KeywordThis method is one of the most prominent and most basic methods of defining constants. Here, we use the keyword const as a prefix in the variable declaration. Syntaxconst variable_type variable_name = value; We have to assign the value to the constant variable at the declaration as we cannot change its value after declaration. ExampleC++
Output main.cpp: In function ‘int main()’: 2. Defining Constant Using #defineDefining constants as a macro (using #define) is different from the method using const keyword as the constants are just a placeholder for the actual value. This alias is replaced by its value during the preprocessing. Syntax#define constantName value Here, ConstantName is the name which is used as an alias to its value. ExampleThe below example demonstrates the declaration of constant using #define. C++
Output
The area of right angle triangle is: 50 3. Defining Constant Using enumEnumeration (or enum) is a data type in which we assign some name to the integer values. We can use this property to create constant values. Enumerations are generally used where multiple constants are required and we can only use the integer values. Syntaxenum enum_Name ExampleThe below demonstrates the use of enum to create multi constants. C++
Output
The no. of Red Balls is 4 The no. of Green Balls is 6 The no. of Blue Balls is 10 4. Defining Constant Using constexprIn the C++11 and later versions, we can also use ‘constexpr’ keyword to declare constants. It its the modern and safe way to declare the constants in C++.This ‘constexpr’ keyword ensures that the value is processed at compile time instead of runtime to improve the execution efficiency. Syntaxconstexpr DataType constantName = value;
ExampleThe below example demonstrates the use of constexpr to define constant. C++
Output
Factorial of 5 is: 120 ConclusionIn conclusion, constants in C++ programming are used to represent values that remain unchanged during program execution. The const keyword and other methods like #define, enum, and constexpr, allows us to declare and define constants as required. We can choose any of the above method based on our convenience and |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |