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 TypeDescriptionSizeExample
intWhole numbers4 bytesint x = 10;
floatDecimal numbers (6-7 digit precision)4 bytesfloat pi = 3.14;
doubleDecimal numbers (15 digit precision)8 bytesdouble d = 3.14159265;
charSingle character1 bytechar c = 'Z';
boolTrue or False1 bytebool flag = true;
stringSequence of characters (text)Variablestring name = "Alice";

Integer Types and Their Ranges

TypeSizeRange
short2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647
long4 or 8 bytesLarger range than int
long long8 bytesVery large integers
unsigned int4 bytes0 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: score and Score are 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:

30

Constants — 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.14

Checking 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 byte

Type 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 const for values that must never change.
  • The auto keyword lets the compiler decide the type automatically.

Leave a Comment

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