We get named storage from a variable that our programs can work with. Each variable in C++ has a unique type that specifies its memory size and organization, the range of values that can be placed inside, and the range of actions that can be performed on the variable.
During program execution, C++ variables are used to store data in a memory region that can be updated or used by the program. A variable's name can include letters, numbers, and the underscore character. Either a letter or an underscore must come first. Because C++ is case-sensitive, uppercase and lowercase letters can be distinguished.
The compiler is instructed by a variable definition of where and how much storage to allocate for the variable. A data type is specified by a variable definition, which also includes a list of one or more variables of that type and they are:
type variable_list;
Here, the type must be a valid C++ data type, such as char, w_char, int, float, double, bool, or any other user-defined object, and variable_list may include one or more identifier names and is separated by commas. Here are several declarations that are valid.
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k, and tells the compiler to create variables named i, j, and k which should be a type of int.
In their declaration, variables can be initialized. The equal sign is followed by a constant expression in the initializer, as shown below:
type variable_name = value;
The following are the fundamental types of variables that are there in C++.
TYPE | DESCRIPTION |
---|---|
char | Usually, it is a single octet and this type is a kind of an integer. |
bool | either the value will be true or false and it is stored. |
int | It is mainly used to indicate integers. |
float | "Floating point" is referred to as "float" informally. It is by definition a basic data type that the compiler has built-in and uses to define numerical values with floating decimal points. The data type float is supported by numerous programming languages, including C, C++, C#, and others. |
double | Double is used to store 64-bit double precision floating point values, while int is used to store 32-bit two's complement integers. |
void | The absence of data is represented using the void |
wchar_t string | It is a kind of wide-character type. “ Hello world” is a kind of text and for storing the text string is used. |
A variable declaration assures the compiler that there is only one variable with the given type and name, allowing the compiler to proceed with further compilation without having complete information about the variable. Only at the time of compilation, a variable declaration has its meaning. At the time of linking the program, the compiler needs an actual variable definition.
A variable declaration is useful when using multiple files and defining your variable in one of the files that will be available when the program is linked. Anywhere you want to declare a variable, use the extern keyword. A variable can be declared more than once in a C++ program, but it can only be defined once in a file, function, or block of code.
Random access memory (RAM) is used to create variables. AS we all know RAM is a temporary memory, so it is very clear that all data saved in it will be also temporary. It is only used during execution. The information kept in the variable is automatically deleted when the program ends.
type_ name variable=value;
The variable declaration describes the section where the variable was originally declared before being used.
While variable definition informs us about the part where the variable assigned memory location and value
Variable definition and declaration were typically done at the same time.
In order to initialize variables, first, we need to specify the type and after that, a value is assigned to it.
type variableName = value;
where type is a C++ type (like int) and variableName is the variable's name (such as x or myName). The variable is given values by using the equal sign.
Take a look at the example below to learn how to build a variable that should store a number:
int myNum= 20;
cout << myNum;
Another option is to declare a variable without giving it a value and give that one later.
type variableName;
variableName = value;
How to do that let us understand it with an example.
int myNum;
myNum= 20;
cout << myNum;
If you give an existing variable a new value, the old value will be replaced. Let us understand it with an example.
int myNum= 20; //here myNum is 20
myNum= 45; //Now myNum 45
cout << myNum; // Outputs 45
In order to declare more than one variable which comes under the same data type, a comma-separated list is used.
int a= 10,b= 30,c=70;
cout << a + b + c;
int a, b, c;
a = b = c = 550;
cout << a + b + c;
The above example clearly shows how to assign multiple variables with a single value.
When displaying variables, the cout object and the << operator are used. Use the << operator to divide the text and a variable before combining them
#include
using namespace std;
int main() {
int myAge = 29;
cout << "I am " << myAge << " years old and working for Learn eTutorials";
return 0;
}
Output:
I am 29 years old and working for Learn eTutorials.
Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so it may not be assigned and can not appear on the left-hand side.
int g = 70; (it is a valid statement)
20= 30; (invalid statement because 20 is a numeric value that cant appear on left-hand side hence compile-time error will occur)
A scope is a section of the program, and variables can generally be defined in one of three places. Local variables are variables that exist just within a function or block. Formal parameters are often known as function parameters. Global variables are variables that are seen or it will always exist outside of all functions. Let's define local and global variables in this section.
Based on the scope of variables in C++, there are three different types of variables:
A local variable is one that is defined within a block, method, or constructor.These variables are generated when the block is entered or the function is called, and they are removed when the block is exited or when the call returns from the function. These variables' scope exists exclusively within the block in which they are declared. That is, we can only access this variable within that block and It is very necessary to initialize the local variable.
Non-static variables known as "instance variables" are those that are declared in a class but not inside of any methods, constructors, or blocks. since the instance variables are declared in a class, they are created when a class object is formed and discarded when the object is destroyed.We can mainly use access specifiers for instance variables. The default access specifier will be used if no access specifier is specified.Instance variable initialization is not required.Only by constructing objects one can access instance variables.
Another name for static variables is class variables. Static Variables: Another name for static variables is class variables.These variables are declared identically to instance variables, with the exception that static variables are declared using the static keyword within a class outside of any method constructer or block. we are only allowed to have one instance of a static variable per class.Static variables are created at the beginning of programme execution and immediately destroyed at its end of the exicution.Static variable initialization is not required and 0 is the default value.
A local variable must be initialized by the user when it is defined because the system does not do this automatically. But the global variables are automatically initialized by the system. A global variable in C++ is described as a variable that can be used or accessed from anywhere in the entire program
Local variables are those that are declared inside a function or block. Only statements included within that function or code block are permitted to use them. Outside of their own function, local variables are unknown or not known.
Here is an example of using local variables:
#include
using namespace std;
int main () {
// Local variable declaration:
int x, y;
int z;
// actual initialization
x = 40;
y = 80;
z = x + y;
cout << c;
return 0;
}
Global variables are typically defined on top of the program and outside of all the functions. Throughout the duration of your program, the global variables will retain their value. Any function may access a global variable. In other words, once a global variable has been declared, it can be used throughout the entire program.
The example for both global and local variables is as follows:
#include
using namespace std;
// Global variable declaration:
int g;
int main () {
// Local variable declaration:
int x, y;
// actual initialization
x = 20;
y = 40;
g = x + y;
cout << g;
return 0;
}