
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Ways to Declare Variable as Constant in C and C++
There are multiple ways of declaring constants in C and C++. First of all, we need to understand what constant is.
What is a Constant?
Constant means which can’t be changed. In terms of programming, constants are the fixed values that are assigned to the variables such that they can’t be altered by any other variable or component during the execution of a program. Constants can be of any data type. They are used in the programming for defining the non-changing component of a program. There are some data or variables which have fixed value like Pi have fixed float value as 3.14 therefore it can be declared as constant.
There are multiple ways to declare the variables as constants
Using const keyword − It is the most commonly used way of making the variables as constant. If the program tries to change the value of the constant variable declared as const then the compiler will through the error message.
Example
#include<stdio.h> int main(){ const int value = 5; printf("value of constant variable is : %d ",value); //try to change the value of constant variable value = 8; return 0; }
Output
Output for this code will be −
||=== Build file: "no target" in "no project" (compiler: unknown) ===| C:\Users\dell\OneDrive\Documents\test.c||In function 'main':| C:\Users\dell\OneDrive\Documents\test.c|7|error: assignment of read-only variable 'value'| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
By creating Enum − Enum or Enumeration are also used to create a set of constant values. Enum is a user defined data type used in many programming languages including C and C++ . For example, we can define days of the week as enum because they have fixed data values of string type.
Example
#include<stdio.h> enum months{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; int main(){ int i; printf("values are : "); for (i=Jan; i<=Dec; i++) printf("%d ", i); return 0; }
Output
Output for this code will be −
values are : 0 1 2 3 4 5 6 7 8 9 10 11
Using Macros − Macros are the type of preprocessor directive. They contain the piece of code which is known with a name. It is created using ‘#define’. Whenever the compiler determines the name of the macro in the code it will replace it with the code. Therefore, we can say that macros are kind of constant values.
Example
#include<iostream> using namespace std; #define val 10 int main(){ val++; //we can’t modify the value of constant return 0 ; }
Output
Output for this code will be −
Main.cpp:6:8: error: expression is not assignable
Example
#include<iostream> using namespace std; #define val 10 int main(){ int product = 1; cout<<"Value of Macro val is : "<<val; for(int i=0;i<val;i++){ product=product*val; } cout<<"\nProduct is: "<<product; cout<<"\nValue of Macro val after modifying it is : "<<val; return 0 ; }
Output
Output for this code will be −
Value of Macro val is : 10 Product is: 1410065408 Value of Macro val after modifying it is : 10