Table of Contents
A variable is a named storage location used to hold data during program execution. Every variable has a data type that determines the kind of value it can store.
This example demonstrates how variables are declared, assigned values, and used in expressions.
Source Code #
#include <iostream>
int main()
{
int age;
age = 21;
std::cout << "Age: " << age << '\n';
return 0;
}
Output #
Age: 21
Explanation #
int age;declares a variable namedagethat can store integer values.- Declaring a variable allocates storage for the specified data type.
age = 21;assigns the value21to the variable using the assignment operator (=).- The value stored in a variable can be modified by assigning a new value.
std::cout << "Age: " << age << '\n';retrieves the value stored inageand writes it to the standard output stream.return 0;terminates the program successfully.