Variables
C++ Variables
Definition: A variable is a named storage location in a computer's memory (RAM) that holds a value. You can think of a variable as a labeled container or box where you can store data, modify it while the program is running, and retrieve it later using its given name.
Why: Standard C++ syllabi introduce variables right after basic input and output. Variables are the fundamental building blocks of data manipulation; every advanced programming concept that follows—including conditional logic, loops, structural arrays, and functions—depends entirely on your ability to store and read values from memory.
Syntax for Declaring Variables
To create a variable in C++, you must first specify its Data Type (which tells the compiler how much memory to allocate) followed by a unique Variable Name. You can optionally assign an initial value using the assignment operator (=).
dataType variableName = value;
Example: Storing Different Types of Data
Below is a complete program demonstrating how to declare, initialize, and print various types of variables in C++:
#include <iostream> #include <string> using namespace std; int main() { int age = 20; // Stores whole numbers string city = "Bengaluru"; // Stores text strings float price = 99.5f; // Stores fractional/decimal numbers cout << "Age: " << age << endl; cout << "City: " << city << endl; cout << "Price: " << price << endl; return 0; }
Core Primitive Data Types
C++ is a strongly typed language, meaning you must declare exactly what kind of data a variable will hold. Here are the most common primitive types beginners learn:
| Data Type | Memory Size (Typical) | Values it Stores |
|---|---|---|
int |
4 bytes | Whole numbers without fractions (e.g., -5, 0, 42). |
float |
4 bytes | Single-precision floating-point decimal numbers (e.g., 3.14). Requires an f suffix. |
double |
8 bytes | Double-precision floating-point decimal numbers. Used for high-precision math. |
char |
1 byte | A single character or ASCII symbol, enclosed in single quotes (e.g., 'A', '9'). |
bool |
1 byte | Boolean logical values: either true (1) or false (0). |
string |
Dynamic object size | A collection of multiple characters enclosed in double quotes (e.g., "Hello World"). |
Rules for Naming Variables (Identifiers)
- Allowed Characters: Variable names can consist of letters (A-Z, a-z), digits (0-9), and underscore characters (
_). - Starting Character: A name must begin with a letter or an underscore. It cannot begin with a number (e.g.,
int 1stNumber;is invalid). - No Reserved Keywords: You cannot use words that already have a dedicated meaning in C++ as variable names (e.g., you cannot name a variable
int,main,return, orusing). - CamelCase vs. Underscores: Choose a clear naming convention and be consistent. Most C++ development pipelines prefer camelCase (e.g.,
studentAge) or snake_case (e.g.,student_age) to make long variable names readable.
Key Notes
- Statically Typed: Once a variable is declared with a specific data type, you cannot change its type later in the program. An
intvariable can only ever hold whole numbers. - Garbage Values: If you declare a local variable without assigning a value to it (e.g.,
int score;), its value is unpredictable. It will contain whatever random left-over bits happened to be in that specific RAM address, which can lead to calculation bugs. Always initialize your variables! - The
constModifier: If you want to create a variable whose value can never be modified or rewritten after its initial creation, add theconstkeyword before the data type (e.g.,const float PI = 3.14159f;).