GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Variables in C++ - GeeksforGeeks Variables in C++Last Updated : 3 Nov, 2026 In C++, variables are containers used to store data in memory. Variables define how data is stored, accessed, and manipulated. A variable has three components:
Output 3 7 DeclarationCreating a variable and giving it a name is called variable definition (sometimes called variable declaration). The syntax of variable definition is: ![]()
where, type is the type of data that a variable can store, and name is the name assigned to the variable. Multiple variables of the same type can be defined as:
The data type of a variable is selected from the list of data types supported by C++. InitializingA variable that is just defined may not contain some valid value. We have to initialize it to some valid initial value. It is done by using an assignment operator '=' as shown: ![]()
Definition and initialization can also be done in a single step as shown:
The integer variable num is initialized with the value 3. Values are generally the literals of the same type.
Accessing and UpdatingThe main objective of a variable is to store the data so that it can be retrieved or update any time. Accessing can be done by simply using its assigned name and updating the value using '=' (assignment operator).
Output 3 7 Rules For Naming VariableThe names given to a variable are called identifiers. There are some rules for creating these identifiers (names):
How are variables used?Variables are the names given to the memory location which stores some value. These names can be used in any place where the value it stores can be used. For example, we assign values of the same type to variables. But instead of these values, we can also use variables that store these values.
Output 10 10 Addition of two integers can be done in C++ using '+' operator as shown:
Output 30 We can do the above operation using the variables that store these two values.
Output 30 Memory Management of VariablesWhen a variable is declared, a fixed-size memory block is allocated to it, and the variable name is used to access that memory location. ![]()
Related ArticleComment ExploreStandard Template Library(STL) Practice & Problems |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |