C++ Variables
w3hello.com logo
C++ Basics
What is C++
Introduction to C++
C++ Installation
Comments in C++
C++ Variables
C++ Pointer Operators
C++ Data Types
C++ Variable
C++ Loop Types
Discuss C++
C++ While Loop
C++ do while loops
C++ while loops
C++ Increment and Decrement Operators
C++ For Loop
C++ if Statements
C++ nested if statements
C++ Comments
C++ for loops
C++ do...while loop
C++ if-else Statements
C++ switch
C++ if...else statement
C++ OOPs Concepts
Operators in C++
C++ Operators
C++ Loops
C++ Member (dot & arrow) Operators
C++ Basic Input Output (cin, cout, endl)
Operators Precedence in C++
C vs C++
C++ switch Statements
C++ if-else
C++ Exception Handling
Constructors and Destructors in C++
C++ File and Stream
C++ Continue Statement
C++ Object Oriented
C++ Arithmetic Operators
C++ Pointers
Pointers in C++
C++ try-catch
C++ Enumeration
ANSI C++ New Features
C++ Recursion
C++ Modifier Types
C++ Break Statement
C++ ios_base init
C++ Compute the Sum and Average of Two Numbers
C++ Preprocessor
C++ Class Constructor and Destructor
Matrix Multiplication in C++
C++ Strings
C++ Basic Input/Output
C++ Qualifiers and Storage Classes
C++ Assignment Operators
Data Abstraction in C++
C++ Class Access Modifiers
C++ Structs
Interfaces in C++ (Abstract Classes)
String Manipulation in C++
C++ References
C++ Date and Time
C++ Useful Resources
C++ ios_base openmode
C++ Aggregation
C++ Introduction
Call by value and call by reference in C++
C++ ios_base fmtflags
C++ Templates
C++ Namespaces
C++ Fstream fpos
C++ ios_base event
Returning values by reference in C++
C++ Polymorphism
C++ nested loops
C++ Constants
C++ Casting Operators
C++ Objects and Classes
C++ static
Storage Classes in C++
C++ ios_base seekdir
Data Encapsulation in C++
C++ Null Pointers
C++ Inheritance
Armstrong Number in C++
C++ switch statement
C++ Encapsulation
Pointer to C++ Classes
C++ Files and Streams
C++ Interfaces
Data Types in C++
Variable Scope in C++
C++ Destructor
C++ Manipulators
Static Members of a C++ Class
Fibonacci Series in C++
C++ this Pointer
C++ Object Class
Polymorphism in C++
C++ Basics
Previous : Comments in C++ Next : C++ Pointer Operators

C++ Variables

In C++ variable is used to store data in memory location, which can be modified or used in program during program execution.

Variables play a major role in constructing a program, storing values in memory and dealing with them. Variables are required in various functions of every programs. For example, when we check for conditions to execute a block of statements, variables are required. Again for iterating or repeating a block of statement(s) several times, a counter variable is set along with a condition, or simply if we store the age of an employee, we need an integer type variable. So in every respect, variable is used. In this tutorial, you will learn about how variables are declared in C++, how to assign values in them, and how to use them within a C++ program.

What are Variables?

Variables are used in C++ where you will need to store any type of values within a program and whose value can be changed during the program execution. These variables can be declared in various ways each having different memory requirements and storing capability. Variables are the name of memory locations that are allocated by compilers and the allocation is done based on the data type used for declaring the variable.

Variable Definition in C++

A variable definition means that the programmer writes some instructions to tell the compiler to create the storage in memory location. The syntax for defining variables is:

data_type variable_name;
data_type variable_name, variable_name,

Here data_type means the valid C++ data type which includes int, float, double, char, wchar_t, bool and variable list is the lists of variable names to be declared which is separated by commas.

/* variable definition */
int    width, height, age;
char   letter;
float  area;
double d;

Variable Initialization in C++

Variables are declared in the above example but none of them has been assigned any value. Variables can be initialized and initial value can be assigned along with their declaration.

variable_name = value;
variable definition and initialization */
int    width, height=5, age=32;
char   letter='A';
float  area;
double d;

/* actual initialization */
width = 10;
area = 26.5;

There is some rules must be in your knowledge to work with C++ variables.

Rules of Declaring variables in C++

  • Variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
  • The first character must be a letter or underscore.
  • Blank spaces cannot be used in variable names.
  • Special characters like #, $ are not allowed.
  • C++ keywords can not be used as variable names.
  • Variable names are case-sensitive.
  • A variable name can be consisting of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.
  • Variable type can be bool, char, int, float, double, void or wchar_t.

Here’s a Program to Show the Usage of Variables in C++

using namespace std;

int main()
    int x = 5;
    int y = 2;
    int Result;
    Result = x * y;
    cout << Result;

Another program showing how Global variables are declared and used within a program:

using namespace std;

// Global Variable declaration:
int x, y;
float f;

int main()
    // Local variable
    int tot;
    float f;
    x = 10;
    y = 20;
    tot = x + y;
    cout << tot;
    cout << endl;
    f = 70.0 / 3.0;
    cout << f;
    cout << endl;

Previous : Comments in C++ Next : C++ Pointer Operators
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap