C++ Variables - Learn C++ Programming

Today, We will learn C++ VariablesLearn C++ Programming, The definition of a variable is “To Variables are containers for storing the data/values”.

In C++, there are so many different types of variables defined with different keywords for example:

  • int – storing integers (whole numbers) without decimals, such as 250 or -250
  • double – storing floating-point numbers, with decimals, such as 25.2 or 25.2
  • char – storing a single character, such as ‘a’ or ‘B’ Char value also store single quotes
  • string – string is storing text, such as “Hello World”. A string variable is used for store more than two quotes
  • bool – storing two values states: true or false

Declaring Variable (Creating Variables)

To create a variable and You much create it specify the type and assign it a value.

Syntax:

type variables = value;

So, Here’s the type is one of the C++ types, (such as int) and the variable is the name of the variable such as x or y or myName. The equal sign=‘ is used to assign value to the variable.

To create a variable that should be store a number, look at the below the following example.

Example:

To create a variable who’s called myNum with the data-type int and assign it the value 10:

#include <iostream>
using namespace std;

int main(){
     int myNum = 10;
     cout << myNum;
}

output:

10

So, you can also declare a variable without assigning any value, and your assign it a value later.

Example:

#include <iostream>
using namespace std;

int main(){
     int myNum;
     myNum = 10;
     cout << myNum;
}

Output:

10

Note: that if you assign a new value to the existing variable. it will be overwritten to the previous value of a variable.

Example:

#include <iostream>
using namespace std;

int main(){
     int myNum = 10; // The value of myNum is 10
     myNum = 25;     // Now the value of muNum is 25
     cout << myNum;  // the value of output is 25
}

Output:

25

Other Type of C++ Variables:

A shortly explanation of other data type variables.

int myNum = 10;           // Integers(Whole Number without decimals).
double myDoubleNum = 10.99;     // Floating point number(with decimals).
char myLetter = 'A';         // Character 
string myString = "Hello";   // string (Text)
bool myBoolVal = true;        // Boolean (True and False)

So, In this code section, we use 1st int is a data type stands for integer. Then myNum is a variable name. Then we use assignment operator = and, we assign the value 10 to integer variable.

But, we use the 2nd number, double data type. Then we give the myDoubleNum variable name. Then we assign a Floating point number with decimal number 10.99.

Click here to read Complete about Data Types and Variables.

Display C++ Variables

So, the cout object is use for displaying the variables with the << operator.

To get combine output text and variables, use the << operator and use the spaces.

Example:

#include <iostream>
using namespace std;

int main(){
     int myMarks = 95;
     cout << "I get " << myMarks << " Marks in Computer Science";
}

Output:

I get 95 Marks in Computer Science

Add Variables Together

To assign two variables with data type int, like the example below, and add a variable to another variable, you can also use a + operator.

Example:

#include <iostream>
using namespace std;

int main(){
     int x = 10;
     int y = 15;
     int sum = x + y;
     cout << "Sum :" << sum;
}

Output:

Sum :25

So, here I am more talking about variableS, and telling you that in the previous chapter about variables, we talked concisely.

There are several types of C++ variables. So, peoples don’t hesitate and learn this in cool weather. Because this is not difficult. Just you need some consternation on education.

Recent Topic

  1. Introduction to C++ Programming
  2. C++ Syntax – Learn C++ Programming
  3. Comment in C++ – Single-line & Multi-line
  4. C++ Output (Display Text) – C++ New Lines
  5. C++ Variables – Learn C++ Programming

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

Your email address will not be published. Required fields are marked *