Variables, Data Types & Expressions in C++

What You’ll Learn

By the end of this post, you will:

  • Understand what variables and data types are
  • Learn how to declare and initialize variables
  • Use basic arithmetic operators and expressions
  • Learn about type conversion and constants

What is a Variable?

A variable is a named storage location in your program that holds a value. You can think of it like a labeled container.

In C++, before you use a variable, you must:

  1. Declare its type
  2. Assign it a value

Example:

int age = 18;
double temperature = 36.5;
char grade = 'A';
bool passed = true;

Each variable has a data type that defines what kind of data it can store.


Built-in Data Types in C++

Data TypeDescriptionExample
intInteger valuesint count = 10;
doubleFloating-point numbersdouble pi = 3.14;
charSingle characterchar grade = 'B';
boolBoolean (true/false)bool flag = true;
stringText (needs <string>)string name = "Ali";

Note:

To use string, you must include the string library:

#include <string>

Variable Declaration and Initialization

Declaration:

int age;

Initialization:

age = 18;

Or both together:

int age = 18;

Multiple declarations:

int a = 1, b = 2, c = 3;

Using cout with Different Data Types

#include <iostream>
#include <string>
using namespace std;

int main() {
int year = 2025;
double gpa = 3.9;
string name = "ARJ";
char grade = 'A';

cout << "Name: " << name << endl;
cout << "Year: " << year << endl;
cout << "GPA: " << gpa << endl;
cout << "Grade: " << grade << endl;

return 0;
}

Output:

Name: ARJ
Year: 2025
GPA: 3.9
Grade: A

Expressions and Arithmetic Operators

An expression is a combination of values and operators that produces a result.

Common Operators:

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulo (remainder)a % b

Example:

int a = 10, b = 3;
cout << a + b << endl; // 13
cout << a - b << endl; // 7
cout << a * b << endl; // 30
cout << a / b << endl; // 3 (integer division)
cout << a % b << endl; // 1

Note:

If either operand is a double, division gives a floating-point result:

double result = 10.0 / 3;
cout << result; // 3.33333

Type Conversion

C++ automatically converts between types in some cases (implicit conversion), but you can also do it manually (explicit conversion).

Implicit:

int x = 5;
double y = x; // x is automatically converted to double

Explicit (Casting):

int a = 7, b = 2;
double result = (double) a / b;
cout << result; // 3.5

Constants

Use const to create a variable that cannot be changed.

const double PI = 3.14159;

Trying to modify PI later in the code will result in a compiler error.


Reading Input: cin

You can use cin to take input from the user.

#include <iostream>
using namespace std;

int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You entered: " << age << endl;
return 0;
}

Mini Exercise

Write a program that:

  • Takes two integers from the user
  • Prints their sum, product, and remainder

Try it yourself before checking the solution.

Sample Output:

Enter two numbers: 7 3
Sum: 10
Product: 21
Remainder: 1

Quiz: Variables, Data Types & Expressions

Quiz: Variables & Data Types

Question 1/7

Common Mistakes to Avoid

  • Forgetting to include #include <iostream> or using namespace std;
  • Not initializing variables before using them
  • Integer division truncating decimal values
  • Using = instead of == in condition checks (covered later)

Summary

In this post, you learned about:

  • Variables and how to declare and initialize them
  • C++ built-in data types like int, double, char, and bool
  • Arithmetic expressions and operators
  • Constants and type casting
  • Taking input with cin

What’s Next?

In the next post, we’ll explore Conditionals and Control Flow — how to make decisions in your code using if, else, and related constructs.

Leave a Comment

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

Scroll to Top