C++ Variables

C++ Variables


C++ Variables

 Variable can be defined as a container to hold data. Variable names must start with a letter of the alphabet or an underscore. Variables are of different types, for example: Types of variables:

  • Int- for variable with integral values
  • char- For variables to store character types.
  • bool- For variable to store boolean values( True or False )
  • string - stores text, such as "Hello World! Welcome to OPH". String values are surrounded by double quotes

float and double are also types for variables with large and floating point values.

Initialization of variablesIn C++, there are some ways to initialize variables as in C Language.
Syntax

   type identifier = initial_value;

Example :

   

Declaring Variables:

The syntax to declare a new variable in C++ is straightforward: we simply write the type followed by the variable name (i.e., its identifier).

Syntax:

Example:

Create a variable called Num1of type int and assign it the value 14:

Output : 

Add Variables Together :

To add a variable to another variable, you can use the + operator:

Output:

float and double are also types for variables with large and floating point values