C++ Data Types & Variables

Whenever you write a C++ program, you need a place to store information. For example, a student’s marks, a person’s age, or the price of a product. In C++, this information is stored using variables. Every variable has a data type, which tells the compiler what kind of value it will store.

In this tutorial, you will learn about C++ Variables, C++ Data Types, type modifiers, the C++ sizeof Operator, C++ Type Conversion, type casting, the auto keyword, and the const keyword.


C++ Variables

A variable is simply a name given to a memory location where a value is stored. You can think of it as a container that holds data. The value inside the container can be changed whenever needed.

Syntax

dataType variableName;

Example

int age = 20;

Here:

  • int is the data type.
  • age is the variable name.
  • 20 is the value stored in the variable.

You can change the value later in the program.

age = 25;

Now the value of age becomes 25.


C++ Data Types

A data type tells the compiler what type of data a variable can store. For example, age is usually stored as a whole number, while salary may contain decimal values.

Some commonly used C++ Data Types are:

Data TypeUsed For
intWhole numbers
floatDecimal numbers
doubleLarge decimal values
charSingle character
boolTrue or False
stringText

Example

int age = 21;
float marks = 89.5;
char grade = 'A';
bool passed = true;

Choosing the correct data type helps save memory and makes your program more efficient.


Type Modifiers

Sometimes the basic data types are not enough. C++ provides type modifiers to change the size or range of a data type.

The most commonly used modifiers are:

  • short
  • long
  • signed
  • unsigned

Example

short int a = 10;
long int population = 5000000;
unsigned int number = 50;

These modifiers are useful when you need to store very small or very large values.


C++ sizeof Operator

Have you ever wondered how much memory a variable uses? C++ provides the sizeof Operator to find that.

It returns the size of a data type or variable in bytes.

Example

#include <iostream>
using namespace std;

int main()
{
    cout << sizeof(int);

    return 0;
}

Output

4

This means an int usually occupies 4 bytes of memory. The result may vary depending on the compiler and system.

You can also use sizeof with variables.

int age = 20;

cout << sizeof(age);

C++ Type Conversion

Sometimes you perform an operation using two different data types. In such cases, C++ automatically converts one data type into another before performing the operation. This is called C++ Type Conversion.

Example

int a = 10;
double b = 5.5;

double result = a + b;

Here, a is an integer and b is a decimal value. Before adding them, C++ automatically converts a into a double.

This process happens automatically, so the programmer does not have to write any extra code.


Type Casting in C++

Sometimes you don’t want C++ to decide the conversion automatically. Instead, you want to convert the data type yourself. This is called Type Casting in C++.

Example

double marks = 89.75;

int total = (int)marks;

cout << total;

Output

89

The decimal part is removed because the value is converted into an integer.

Type casting gives you more control over how data is converted.


auto Keyword

Instead of writing the data type yourself, you can use the auto keyword. C++ will automatically detect the correct data type based on the assigned value.

Example

auto age = 20;
auto price = 99.99;
auto grade = 'A';

Here, C++ automatically identifies the data types of all three variables.

The auto keyword makes code shorter and easier to write.


const Keyword

There are some values that should never change during program execution. For such values, C++ provides the const keyword.

Example

const float PI = 3.14159;

Now the value of PI cannot be changed.

If you try to assign another value to it, the compiler will generate an error.

Using const makes your program safer because important values cannot be modified accidentally.


Frequently Asked Questions (FAQs)

Q1. What are variables in C++, and why are they important?

C++ Variables are named memory locations used to store data during program execution. They allow programs to save, update, and process information, making them a fundamental part of C++ programming.

Q2. What are the different data types available in C++?

C++ Data Types define the type of data a variable can store. Common data types include int, float, double, char, bool, and string. Choosing the correct data type helps improve memory usage and program efficiency.

Q3. What is the difference between type conversion and type casting in C++?

C++ Type Conversion is performed automatically by the compiler when converting one compatible data type to another, whereas Type Casting in C++ is done explicitly by the programmer to convert a value from one data type to another.

Q4. What is the sizeof operator used for in C++?

The C++ sizeof Operator is used to determine the amount of memory (in bytes) occupied by a data type, variable, or object. It is commonly used when working with memory allocation and optimizing programs.

Q5. What is the purpose of the auto and const keywords in C++?

The auto keyword allows the compiler to automatically determine a variable’s data type, while the const keyword creates variables whose values cannot be modified after initialization.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top