Variables in C++
A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which 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.
Name of variable: It refers to an identifier that represents a memory location.
Address of variable: It refers to the memory location of the variable.
Contents of variable: It refers to the value stored in memory location referred by variable.
Variables in C++ Declaration
A variable must be defined before you can use it in a program. When you define a variable the type is specified and an appropriate amount of memory reserved. This memory space is addressed by reference to the name of the variable. A simple declaration has the following syntax:
SYNTAX: data_type variable_name;
Data_type: It indicates types of data that can be stored in a variable.
Variable_name: It refers to the memory location of a variable.
Different types of variables are declared as follows:
Rules for declaration Variables
Following are some rules for naming variable in C++ language:
- Variable may include letters, numbers, and underscores (_).
- The first character of the variable must be a letter or underscore _. The use of underscores is not recommended. The variable 9minute, #home, and 2kg are invalid.
- Blank spaces are not allowed in variable names. The variable my var and your car are invalid.
- Both upper and lower cases are allowed. A user-defined variable is conventionally written in lower case. The constants are conventionally written in upper case.
- Special symbols cannot be used as the variable names.
- Reserved words cannot be used as the variable names. the names int, void, and while are invalid variables.
- A variable can be up to 31 characters long for many compilers. If a variable consists of more than 31 characters, only the first 31 characters will be used. The remaining characters will be ignored.
- A variable can be declared only for one data type.
The variable must be meaningful and readable. It helps the user understand the purpose of the variable. For example, a variable to store the marks of the student should be capitalized Subjects. If a variable consists of multiple words, the first letter of each word should be capitalized to increases readability. A variable MyCarNo is more readable than mycarno.
Some example of valid variables:
Subject mar_1 _temp first_name Zitoc
Variables in C++ initialization
A variable can be initialized, i.e. value can be assigned to the variable, during its definition. Initialization is achieved by placing the following immediately after the name of
- an equals sign (=) and an initial value for the variable or
- round brackets containing the value of the variable.
type_name variable= value;
|It indicates the data type of the variable to be initialized.
|It is the name of the variable to be initialized.
|It is the assignment operator used to initialize a variable.
|It is the value to initialize a variable.
int n= 100;
int x=50, y = 30;
char grade = `A`;
float average = 50.66;