Variables and Data Types in C++
A variable is a named storage location in the computer's memory that holds a value. Think of it like a labeled box — the label is the variable name, and the contents inside is the value. Data types tell the compiler what kind of data will be stored in that box.
Declaring a Variable
To use a variable in C++, it must be declared first. The syntax is:
data_type variable_name;
data_type variable_name = initial_value;
Examples:
int age = 25;
float price = 49.99;
char grade = 'A';
bool isPassed = true;
Fundamental Data Types in C++
| Data Type | Description | Size | Example |
|---|---|---|---|
int | Whole numbers | 4 bytes | int x = 10; |
float | Decimal numbers (6-7 digit precision) | 4 bytes | float pi = 3.14; |
double | Decimal numbers (15 digit precision) | 8 bytes | double d = 3.14159265; |
char | Single character | 1 byte | char c = 'Z'; |
bool | True or False | 1 byte | bool flag = true; |
string | Sequence of characters (text) | Variable | string name = "Alice"; |
Integer Types and Their Ranges
| Type | Size | Range |
|---|---|---|
short | 2 bytes | -32,768 to 32,767 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
long | 4 or 8 bytes | Larger range than int |
long long | 8 bytes | Very large integers |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
Variable Naming Rules
- Must begin with a letter or underscore (
_). Cannot start with a digit. - Can contain letters, digits, and underscores only — no spaces or special characters.
- C++ keywords cannot be used as variable names (e.g.,
int,return,class). - C++ is case-sensitive:
scoreandScoreare two different variables.
Valid vs Invalid Variable Names:
int score = 90; // valid
int _total = 100; // valid
int 1stValue = 5; // invalid — starts with digit
int my score = 0; // invalid — contains space
int int = 7; // invalid — 'int' is a keyword
Declaring Multiple Variables
Multiple variables of the same type can be declared on one line:
int a = 5, b = 10, c = 15;
cout << a + b + c << endl;
Output:
30Constants — Values That Cannot Change
When a value should never change throughout the program, declare it as a constant using the const keyword.
const float PI = 3.14159;
const int MAX_STUDENTS = 50;
Trying to change a constant after declaration causes a compile-time error.
Type Modifiers
Type modifiers adjust the range or sign of basic types:
signed— Can hold negative and positive values (default for int).unsigned— Only positive values, doubles the upper range.short— Uses less memory than int.long— Uses more memory, holds larger values.
The auto Keyword (C++11)
The auto keyword lets the compiler automatically determine the data type based on the assigned value:
auto x = 42; // int
auto y = 3.14; // double
auto name = "Alice"; // const char*
cout << x << " " << y << endl;
Output:
42 3.14Checking Data Type Size with sizeof
#include <iostream>
using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of bool: " << sizeof(bool) << " byte" << endl;
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Size of bool: 1 byteType Conversion
C++ allows converting one data type into another. This can happen in two ways:
Implicit Conversion (Automatic):
int a = 5;
double b = a; // int automatically converted to double
cout << b; // Output: 5.0
Explicit Conversion (Casting):
double x = 9.7;
int y = (int)x; // Explicitly cast double to int
cout << y; // Output: 9 (decimal part is dropped)
Key Takeaways
- Variables are named storage containers for values.
- Every variable must be declared with a data type before use.
- C++ has several built-in types:
int,float,double,char,bool,string. - Use
constfor values that must never change. - The
autokeyword lets the compiler decide the type automatically.
