
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Variable initialization in C++
Variables are the names given by the user. A datatype is also used to declare and initialize a variable which allocates memory to that variable. There are several datatypes like int, char, float etc. to allocate the memory to that variable.
There are two ways to initialize the variable. One is static initialization in which the variable is assigned a value in the program and another is dynamic initialization in which the variables is assigned a value at the run time.
The following is the syntax of variable initialization.
datatype variable_name = value;
Here,
datatype − The datatype of variable like int, char, float etc.
variable_name − This is the name of variable given by user.
value − Any value to initialize the variable. By default, it is zero.
The following is an example of variable initialization.
Example
#include <iostream> using namespace std; int main() { int a = 20; int b; cout << "The value of variable a : "<< a; // static initialization cout << "\nEnter the value of variable b : "; // dynamic initialization cin >> b; cout << "\nThe value of variable b : "<< b; return 0; }
Output
The value of variable a : 20 Enter the value of variable b : 28 The value of variable b : 28
In the above program, two variables are declared a and b.
int a = 20; int b;
The variable a is initialized with a value in the program while the variable b is initialized dynamically.
cout << "The value of variable a : "<< a; // static initialization cout << "\nEnter the value of variable b : "; // dynamic initialization cin >> b; cout << "\nThe value of variable b : "<< b;
- Related Articles
- Initialization of variable sized arrays in C
- Explain the variable declaration, initialization and assignment in C language
- Initialization of local variable in a conditional block in Java
- Collection Initialization in C#
- Zero Initialization in C++
- Uniform Initialization in C++
- Initialization vs Instantiation in C#
- Why final variable doesn't require initialization in main method in java?
- Is there a difference between copy initialization and direct initialization in C++?
- Initialization of static variables in C
- Static Data Member Initialization in C++
- Why Java wouldn't allow initialization of static final variable in a constructor?
- Initialization of a multidimensional arrays in C/C++
- Initialization of a multidimensional array in C
- Initialization of a multidimensional arrays in C/C++ Program
