Variables and Constants in C++

Variables and Constants in C++

Variables and constants in the C++ programming language are an essential method for storing and processing data values ​​in user-defined programs. They can be manually modified between program declaration and execution, or custom-set and fixed as needed. To create efficient, readable, and maintainable program source code in C++, it’s crucial to understand the proper order of variable and constant declarations and their usage.

Variables and Constants in C++

Variable Concept in C++.

In a C++ program, a user-defined variable is a custom-named object location in a secondary storage memory location that stores a user-defined numeric, text, float, or Boolean value that can be modified during the current program execution. The value of a declared variable can be modified while the active program is running.

Declaring Variables in C++.

To declare a variable in a C++ program, the user must specify it in the existing C++ program. For example,

  • variable’s data type – This indicates in the existing C++ program what data type the variable is defined or declared as, and what value it will hold. For example, int, float, char, boolean, etc.
  • variable’s name – This is the variable’s name identifier in the existing C++ program, which you use to indicate a C++ variable and process and hold the defined value.

Variables declaration syntax.

data_type variable_name;

When declaring a variable in a C++ user program, you can also initialize it as needed. For example,

data_type variable_name = holdvalue;

Multiple C++ variable declaration examples.

int value = 101; // Here an integer variable named value is declared and initialized with the value 101.

float temp = 98.3; // Here a float variable named temp is declared and initialized with the value 98.3.

char score = ‘S’; // Here a character variable named score is declared and initialized with the value S.

bool isValid = true; // Here a Boolean variable named isValid is declared and initialized with the value true.

C++ variable declaration naming rules.

In any C++ program, you can start a variable declaration with a letter or an underscore (_). After that, you can represent the variable with letters, digits, or underscores for the variable name.

  • Valid variables – Here, testVar, _varpqr, q, emp_name, salary, id, etc. are some proper valid C++ program declarations.
  • Invalid variables – Here, 123var, #variable, !value, 90Emp_id, 101Salary, etc. are some invalid variable declaration methods.
  • Reserved keywords – You cannot declare or define a reserved keyword as a variable in C++ programming. Such as, int, float, return, if, void, class, stack, list, etc.
  • Case-sensitive variables – In a C++ program, the variables emp, Emp, and EMP are treated differently by the compiler.
  • Special symbols – You cannot use any special character symbols in a C++ program, except the _ and underscore symbol.

Examples of invalid C++ variables.

int 789value; // This is an invalid variable declaration method because the variable starts with a number.

float &data; // This is an invalid variable declaration method because the ‘&’ symbol is not allowed in variable declarations.

C++ Variable Initialization.

In a C++ program, user-defined variable values ​​can be initialized in several ways.

C++ Direct Initialization Concept.

int data(10); // Here, the integer variable value is assigned with the name data in direct initialization.

Copy Initialization Concept.

int data = 10; // Here, the integer variable value is assigned with the name data in copy initialization.

Uniform Initialization Concept C++11 and later.

int data{10}; // Here, an integer variable value named data is defined in uniform initialization order (better for type safety).

Constant Concept in C++.

In a C++ program, a constant is a user-defined, custom-declared variable whose value cannot be modified after it is declared or initialized in a program. Constant data type variables are used in C++ programs when you want to ensure that the value associated with the declared constant variable cannot be modified during program execution or becomes a fixed constant value later.

Declaring a C++ Constant Variable.

To declare a constant variable in a C++ program, you must use the const keyword before the variable, followed by the defined variable data type, the name of the custom constant variable, and initialize it with a fixed constant value. This ensures in the current program that the variable’s value is fixed after initialization and cannot be modified later.

Syntax of a C++ Constant Variable.

const data_type constant_name = value;

You can also use the constexpr keyword (available in C++11 and later) in C++, which helps the C++ compiler manage constant data types at program compile time.

Syntax of the constexpr keyword.

constexpr data_type constant_name = value

Example of variable and constant usage in a C++ program.

#include <iostream>

using namespace std;

constexpr double PI_value = 3.14159f; // here we declare a compile-time constant variable

const int UPDATE_AGE = 50; // Here we declare a simple Constant variable

int main() {

int emp_age = 34;

long double radius = 7.0;

long double area = PI_value * radius * radius;

cout << “employee age is – ” << emp_age << endl;

cout << “Pi Value Radius is – ” << radius << endl;

cout << “Area of ​​the circle is – ” << area << endl;

cout << “Update Age is – ” << UPDATE_AGE << endl;

// Modifying variables

emp_age = 39;

radius = 9.5;

area = PI_value * radius * radius;

cout << “Here the Updated Age value is – ” << emp_age << endl;

cout << “here the Updated Radius value is – ” << radius << endl;

cout << “here Updated Area of ​​the circle is – ” << area << endl;

// UPDATE_AGE = 57; // it display Error- here it cannot modify a constant value

return 0;

}

Leave a Reply