C++

C++ Variable Types

C++ Variable Types

Let us explore more on C++ variable types. A variable is a named storage needed to carry out logic in the program. A variable is something that can be accessed and modified as per need. 

Since C++ is a strongly typed language, every variable in C++ has a specific type from which compiler determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Like every other programming language, C++ also has rules for naming each object including variables. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive.

We have already seen basic data types and their memory requirements. . C++ also gives advanced datatypes such as Enumeration, Pointer, Array, Reference, Data structures, vectors, maps and Classes.

To define a variable, we use the following syntax.

type variable_list;

int medianSalary = 19000;
flaot result;
string name;

A variable has two operations i.e. declaration and definition that can either be done one at a time or in a single statement. The declaration tells compiler that there is one variable with the given type and name so that it can proceed for further compilation. A variable declaration is to satisfy compilation requirement, its actual value is needed at run time while linking of the program. A variable declaration is useful in multiple file case wherein you can define your variable in one of the files so that it becomes available at the time of linking of the program. For this, you need to use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.

Let us conclude this section by describing two terms that are heavily used in context of variables in both C and C++ i.e. Lvalue and Rvalue. L-value refers to memory location which identifies an object and is the identity of the variable. L-value can appear on either left hand or right hand side of an assignment operator (=). Expressions that are mapped to modifiable locations are called “modifiable l-values“. A modifiable l-value cannot have an array type, an incomplete type, or a type with the const attribute because all these are not modifiable. Any structure or unions with any members with the const attribute will not be "modifiable l-value". An identifier qualifies for modifiable lvalue if it refers to a memory location with type arithmetic, structure, union, or pointer. For example, if sum is an integer variable, then sum is a modifiable l-value since it can change its value. R-value refers to data value that is stored at some address in memory that can’t be assigned any value. This implies that r-value can only and only appear on right of an assignment operator (=).