• Home
  • 1.5 Variables

1.5 Variables

View Categories

1.5 Variables

< 1 min read

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 named age that can store integer values.
  • Declaring a variable allocates storage for the specified data type.
  • age = 21; assigns the value 21 to 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 in age and writes it to the standard output stream.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

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