Define constant variables in C++ header
Hello readers! In this tutorial, we are going to learn how we can define constant variables in the C++ header file. In C++ constant variables are defined as the types of variables whose value can’t be changed once they are defined, they remain fixed all throughout the program.
In C++ a variable can be declared constant in 2 ways:
- Using #define keyword method
Syntax:- #define identifier_name value
Example- #define var3 290
- By using const keyword method
Syntax:- const datatype identifier_name = value
Example- const int var8 = 300
Before moving to the explanation of the above two types, first, we should know what is literal?
Literals: A literal is actually the value that is assigned to a constant variable. Like, if we take const float z= 10.2; In this case, 10.2 is referred to as the constant literal of float type. Now we will see in detail the two types of constant.
C++ Code: constant variables in C++ header
Refer to the code below to see both the ways :
Refer to the comments in the code for a better understanding
#include <iostream> #define var1 89 const int var2=340; //using const keyword method int main() { std::cout<<var1<<" "<<var2; //using std::cout for output }
Output:
89 340
Explanation of the code:
Using #define
declare a constant variable with a constant value. Now using const
keyword declares another constant variable. Declare the main function. The output values of both variables will be printed using std::cout.
1. Using the const keyword: In this case, first, we will look at the syntax of declaring constant in C++ using the const keyword.
Syntax: const datatype name_of_the_constant = initial_value;
In the above syntax –
1. One should never remove the const keyword from the beginning.
2. The data type can be float, integer, double, long long, long long int etc.
3. “name_of_the_contant” should always be like a declared name. You should always remember once you declare a variable as const you should never declare the same variable name inside the programming anywhere.
4. ” initial_value ” should go with the data type.
#include<iostream> using namespace std; int main() { const int a = 10 //Valid defining of constant cout<<a; /* Error 1 : const int z; //Remember always this code will give you an error z=15; Error 2: const int z; // This will also throw an error */ return 0; }
In the above example, we can see two error statements. Those are because of –
1. Error 1: Suppose you are declaring a constant and assigning the value in the next line, that also will throw an error.
2. Error 2: You should never leave a const after declaring it, without assigning a value to it.
3. One should not program in such a way that, a constant can only be initialized with a value according to the user input. In that case, the code will run with an error.
2. Using #define: In this case, first, we will look to the syntax of declaring constant in C++ using #define.
Syntax: #define identifier_name value
In the above syntax –
1. #define should be present in this constant initialization technique.
2. ” identifier_name ” should not be a data type like int, float. It’s a given name. By which we will access the value of it, later inside the code.
3. Next is value initialization to the const.
#include<iostream> using namespace std; #define floatVariable 4.35 //Defining the value int main() { cout<<"Float constant variable value is - "<<floatVariable; //Printing Value return 0; }
Remember: You should not define a constant with #define within the main function or within a class or within a function.
That’s all for the tutorial. Hope you liked it!
Leave a Reply