Variables in C++

Share Your Love

First of all, as variables a basic topic in C++, readers must know some facts about C++, because if anyone asks you, you’re well prepared to answer. So, C++ is a general-purpose cross-platform language that was developed by “Bjarne Stroustrup” as an extension of ‘C’ with classes. It’s used to develop(create) high-performance applications and can also be found in operating systems and GUIs (Graphical User Interface).

Variables are memory locations that we assign to store data or a particular value that can be changed when needed. It can be declared in different ways according to which it will have memory requirements.

Declaration:-

data type variable name = value to be stored;

For Example:

int v=4;

Inside Program:

#include <iostream>
using namespace std;
int main ()
{
  int a, b;                   //Main declaration of integers
  int sum;
  a = 3;                      //initialisation of variables
  b = 2;
 sum = a + b;
 cout << sum;            // print the sum
 return 0;               // terminate the program:
}

EIv uMFdoNXGwpRSDp5NLcR6TQs ECNUOdTYWIHBZV2U

Contributed By: Shiva Patra

Let’s jump to types of variables

1) int:

It is used to store integer values like 1,234,-489.

The declaration is already given in above example.

2) char:

It is used to store characters like ‘a’, ’B’, ’V’. Make sure the values are surrounded by quotes.

Declaration Example:

char myfirst = 'S'; 

Inside Programming:

#include <iostream>
using namespace std;
int main ()
{
  char myfirst = 'S';         // Declaration and initialiation of character variable
 cout <<  myfirst;            // print the character
 return 0;               // terminate the program:
}

UsPYzOM91u4AYVk9fr9qw I3edcOiV02asYKnIgMGpp 4lyXFu

Contributed By: Shiva Patra

3) bool:

It stores the Boolean value which can be true or false.

Declaration Example:

bool statementvalue = true;

Inside Programming:

#include <iostream>
using namespace std;
int main ()
{
  bool statementvalue = true;       // Boolean (true or false)
 cout <<  statementvalue;            // output will be 1 as it's the true value
 return 0;               // terminate the program:
}

Jx9WgpR0BXoPwVrGInopQBmcb3ml2ubAMJl6I SyKweAvMPGxY8akPhBsuNtxG984oKa5KU8fIxbXfQ2ENebb9fCk2iSMa3nUrXTgrrXc2sxyXv6dRjQ4fUQNj3fcX7 emBM8g

Contributed By: Shiva Patra

4)double and float:

These two are used to store large decimal point numbers like 3.68, 4.567.

Declaration Example:

 double floatval=3.68;

Inside Programming:

#include <iostream>
using namespace std;
int main ()
{
  double floatval = 3.68;    // Floating point number (with decimals)
   cout << floatval;            // output will be 1 as it's the true value
 return 0;               // terminate the program:
}

U4qOYiOHdyQzNe5PrRpq8659tHsInw FulhDRvmdV4iWHY8 yvJ5 2j7egiMqzozTecSgvF6LPiseiDiTnc8p8be vrBY52neVjPm9Q82XKpTJ96pjfRwL28h4XsMWC4 Gdh6Q

Contributed By: Shiva Patra

5) String:

It is used to store strings or text values and is also surrounded by double quotes.

There are more types of variables like null, void, etc.

Now, moving to the scope of the variables which means from where the variables will be accessible and can be manipulated. It decides the area of functioning of the variables. According to this property, the variables are classified in two types:

a) Local variables:

Local variables are variables whose existence and availability are valid inside the curly braces of the code in which it is declared. We can’t use the value of that variable outside the area of the braces.

Inside Programming:

#include <iostream>
using namespace std;
int main () {
   int a, b;            // Variables a and b are local variables declared here:
   int result;

   a = 16;
   b = 14;
   result = a + b;
   cout << result;
   return 0;
}
#include <iostream>
using namespace std;
int main () {
   int a, b;            // Variables a and b are local variables declared here:
   int result;

   a = 16;
   b = 14;
   result = a + b;
   cout << result;
   return 0;
}

cYnzhtiUhd7fpVUpEzVRiOpGQ2csCSyIBd11MPgLDaxdfgKni1nRO jpi76Qb9a7bCLu1ANSoKKB5YSn5 bTzcLPfAGTxx0m1VHI21ld0XO KhjkIabyPpx pNrAEiaUgGABpA

Contributed By: Shiva Patra

b) Global variables:

Global variables are variables which can be used throughout the program once they are declared. They can be used in any class or by any function whenever needed. But, it should be very clear that they should be declared outside the main() function.

Inside Programming:

#include <iostream>
using namespace std;
int sum;                 // sum is global variable declared
int main () {
   int x, y;               
   x = 34;
   y = 56;
   sum = x + y;
cout << sum;
   return 0;
}

63 D

Static Variables:

Static Variables are one more kind of variable which once initialized they remain constant during function calls.

Seems like we covered much of the topic, get a reference from other sources too if you need extra information but I think it’s enough for someone to know about variables in C++.

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Shiva Patra
Shiva Patra

Hello, this is G. Shiva. Patra, your content writer for this website, is currently pursuing BCA for a graduation degree. I am here to write content based on C++ and similar kind of stuff.
I like the tech world as it grows really fast and you have great things to learn every day which keep you away from boredom. So, I have some knowledge on very different topics related to tech like the dark web, deep web, onion rooting, IoT, Cybersecurity, and I always keep track of new devices and software used by them as every tech enthusiast does.

Articles: 9

Newsletter Updates

Enter your email address below to subscribe to our newsletter