Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 07

Variables and Data Types in C++

In this lesson: Learn C++ variables and data types — sizes, ranges, auto keyword — with examples and output. In Hindi & English.

What is a variable?

A variable is a named box in memory that holds a value. The name lets you read or change that value later. In C++ every variable has a type, which decides what kind of value it can store and how much memory it uses.

Declaring and initialising

To declare a variable you write its type, then its name. You can give it a starting value at the same time — this is called initialising.

C++
int age = 20;          // declare and initialise
double price = 9.99;
char grade = 'A';
bool isPassed = true;

age = 21;              // change the value later
💡 Always initialise

A variable declared but not given a value holds an unpredictable "garbage" value until you assign one. Initialise as you declare when you can.

Built-in data types

TypeHoldsExample
intWhole numbers42, -7
floatDecimal numbers (less precise)3.14f
doubleDecimal numbers (more precise)3.14159
charA single character'A'
booltrue or falsetrue

Sizes and ranges

Each type uses a fixed amount of memory. You can check it with sizeof:

C++
#include <iostream>
using namespace std;
int main() {
    cout << "int: "    << sizeof(int)    << " bytes\n";
    cout << "double: " << sizeof(double) << " bytes\n";
    cout << "char: "   << sizeof(char)   << " bytes\n";
    return 0;
}
Output (typical):
int: 4 bytes
double: 8 bytes
char: 1 bytes

Sizes can vary by system, but int is usually 4 bytes, double 8, and char always 1.

A working example

C++
#include <iostream>
using namespace std;
int main() {
    string name = "Amit";
    int age = 20;
    double marks = 88.5;
    cout << name << " is " << age << " with " << marks << "%\n";
    return 0;
}
Output:
Amit is 20 with 88.5%

Note the string type for text — it comes from the standard library and is far easier than C-style character arrays.

The auto keyword

C++ can figure out a type for you with auto:

C++
auto count = 10;       // int
auto pi = 3.14159;     // double
auto letter = 'Z';     // char

Use auto to avoid repeating long type names; the value on the right decides the type.

Common mistakes

  • Using a variable before giving it a value (garbage result).
  • Storing a decimal in an int — the fractional part is dropped.
  • Putting text in single quotes; 'A' is a char, "A" is a string.
  • Starting a name with a digit or using a keyword as a name.
🏋️ Practice

Declare an int, a double, a char and a bool, give each a value, and print them all on one line with labels. Then print the sizeof each type.

Summary

  • A variable is a named, typed box in memory holding a value.
  • Declare with type name = value; and initialise as you declare.
  • Core types: int, float, double, char, bool; string for text.
  • sizeof shows how many bytes a type uses.
  • auto lets the compiler deduce the type from the value.

Frequently Asked Questions

What is a variable in C++?
A variable is a named storage location that holds a value your program can use and change. You declare it with a type and a name, such as int age = 20;, which reserves memory for an integer and stores 20 in it.
What are the basic data types in C++?
The fundamental built-in types are int for whole numbers, float and double for decimals, char for single characters, and bool for true/false values. Modifiers like short, long, signed and unsigned adjust their size and range.
What is the difference between float and double in C++?
Both store decimal numbers, but double uses more memory (usually 8 bytes) and holds about twice the precision of float (usually 4 bytes). Use double when you need more accurate decimals, which is why it is the common default in C++.
What is the bool data type in C++?
The bool type holds one of two values, true or false. It is used for conditions and logic, and internally true is stored as 1 and false as 0. C++ has bool as a real built-in type, unlike older C.
What does the auto keyword do in C++?
auto lets the compiler deduce a variable's type from the value you assign, so auto x = 5; makes x an int and auto pi = 3.14; makes it a double. It reduces repetition and is handy with long type names, though the value must be given at declaration.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicInput and Output in C++ Your progress: 7 / 81 (9%) · Lesson 8 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free