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.
int age = 20; // declare and initialise double price = 9.99; char grade = 'A'; bool isPassed = true; age = 21; // change the value later
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
| Type | Holds | Example |
|---|---|---|
int | Whole numbers | 42, -7 |
float | Decimal numbers (less precise) | 3.14f |
double | Decimal numbers (more precise) | 3.14159 |
char | A single character | 'A' |
bool | true or false | true |
Sizes and ranges
Each type uses a fixed amount of memory. You can check it with sizeof:
#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;
}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
#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;
}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:
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.
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;stringfor text. sizeofshows how many bytes a type uses.autolets the compiler deduce the type from the value.
Frequently Asked Questions
What is a variable in C++?
int age = 20;, which reserves memory for an integer and stores 20 in it.What are the basic data types in C++?
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++?
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++?
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.