• Home
  • 3.6 Simple Assignment Operator (=)

3.6 Simple Assignment Operator (=)

View Categories

3.6 Simple Assignment Operator (=)

< 1 min read

Table of Contents

The assignment operator (=) assigns the value of the right operand to the left operand. The left operand must be a modifiable object, such as a variable, while the right operand can be a value, variable, or expression.

The assignment operator copies the computed value of the right-hand side into the left-hand side variable. Any previous value stored in the variable is replaced.

Source Code #

#include <iostream>

int main()
{
    int number;

    number = 100;

    std::cout << "Number: " << number << '\n';

    return 0;
}

Output #

Number: 100

Explanation #

  • The assignment operator (=) stores the value of the right operand in the left operand.
  • number = 100; assigns the integer value 100 to the variable number.
  • Any previous value stored in number is replaced by the new value.
  • The left operand must be a modifiable variable or object.
  • The right operand may be a constant, variable, function return value, or expression.
  • The assignment operator does not compare values; it performs value assignment.
  • std::cout displays the value stored in the variable.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

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