A C/C++ program performs many tasks and operations that help you to resolve many problems by storing values into computer memory. But, how does the compiler understand the names given to these values? A variable helps to specify the existence of these values by defining and declaring them.
In simple words, a variable is a storage space associated with a unique name to identify them. When you want to store some data on your system in the computer memory, is it possible for you to be able to remember these memory addresses? The answer is no, and that is the reason why we use variables.
Variables in C++ programming help us to store values depending upon the size of the variable. With the help of variables, we can decide what amount and type of data store in a variable. When you assign a data type and name to some space in the memory, variables are defined.
Variables reserve some memory in the storage space, that you can access later in the program. By declaring a variable, you inform the operating system to reserve memory indicated by some name. i.e. variable_name.
A variable must not start with a digit or an underscore.
A variable can begin with an alphabet.
Variables in C++ are case-sensitive which means that uppercase and lowercase characters are treated differently.
A variable must not contain any special character or symbol.
White spaces are not allowed while naming a variable.
Variables should not be of the same name in the same scope.
A variable name cannot be a keyword.
The name of the variable should be unique.
Let’s see some examples of both valid and invalid variable names.
Valid variable names
ticketdata
ticket_data
Invalid variable names
56ticketdata
ticket@data
ticket data
A variable definition in C++ defines the variable name and assigns the data type associated with it in some space in computer memory. After giving its definition, this variable can be used in the program depending upon the scope of that variable.
By defining a variable, you indicate the name and data type of the variable to the compiler. The compiler allocates some memory to the variable according to its size specification.
Must contain data_type of that variable.
Example:
int start;
float width;
char choice;
The variable name should follow all the rules of the naming convention.
After defining the variable, terminate the statement with a semicolon (;) otherwise it will generate a termination error. Example:
int sum;
The variable with the same data type can work with a single line definition. Example:
float height, width, length;
int var;
Here, a variable of integer type with the variable name var is defined. This variable definition allocates memory in the system for var.
Another example,
char choice;
When we define this variable named choice, it allocates memory in the storage space according to the type of data type in C++, i.e., character type.
Variable initialization means assigning some value to that variable. The initialization of a variable and declaration can occur in the same line.
int demo = 23;
By this, you initialize the variable demo for later use in the program.
Here are videos demonstrating how to use variables and data types in a C++ program: