- C++ Course Basics
- C++ Tutorial
- C++ Basic Syntax
- C++ Identifiers
- C++ Character Set
- C++ Input/Output Operator
- C++ Variables
- C++ Data Types
- C++ Formatting Output
- C++ Operators
- C++ Assignment Operator
- C++ Type Conversion
- C++ Program Control
- C++ if and if-else
- C++ switch
- C++ loops
- C++ break and continue
- C++ Functions
- C++ Functions
- C++ Prototype and Definition
- C++ Function Call
- C++ Function Types
- C++ Friend Function
- C++ Function Overloading
- C++ Arrays and Strings
- C++ Arrays
- C++ One-Dimensional Arrays
- C++ Strings
- C++ String Functions
- C++ Structures
- C++ Structures
- C++ Nested Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ Pointers
- C++ Pointers
- C++ Memory Map
- C++ Declare Initialize Pointers
- C++ Pointers and Structures
- C++ Object-Oriented
- C++ Object-Oriented
- C++ Classes and Objects
- C++ Constructors and Destructors
- C++ Objects as Function Arguments
- C++ Pointers and Objects
- C++ Data Structure
- C++ Linked List
- C++ Stack
- C++ Queues
- C++ File Handling
- C++ File Handling
- C++ Opening and Closing Files
- C++ Steps to Process Files
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Misc
- C++ typedef
- C++ #define
- C++ Date and Time
- C++ Examples
- C++ Examples
C++ Assignment Operator and Statement
This post was written and published to describe one of the most important operators in C++, the assignment operator (=). Another subject covered in this post is assignment statement. So, without further ado, let's get started with the assignment operator.
C++ Assignment Operator
Basically, the assignment operator is used to assign the value of one variable to another. Or assign a value to a variable. Here is the general form to assign a variable's value or value to a variable:
a = b; x = 10;
In the above statements, the value of b is assigned to a, and the value 10 is assigned to x. Here is an example.
#include<iostream> using namespace std; int main() { int a, b=10, x; a = b; x = 10; cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; cout<<"x = "<<x<<endl; return 0; }
Here is the sample output of the above C++ program:
a = 10 b = 10 x = 10
Note: In C++, the "=" character is known as the assignment operator.
C++ Assignment Statement
As you know, an expression is composed of one or more operations. An expression terminated by a semicolon becomes a statement. Statements from the smallest executable unit within a C++ program Statements are terminated with a semicolon.
An assignment statement assigns a value to a variable. The value assigned may be constant, variable, or an expression. The general form of an assignment statement is as follows:
a = cve;
where a is a variable to whom the value is being assigned, and cve can either be a constant, a variable, or an expression. Following are some examples of assignment statements:
x = 16; y = 3.6; z = x + y; z = z × x; a = b + c - d;
I already mentioned that the symbol "=" is known as the assignment operator. The assignment operator can be used serially, which is a common feature of C++.
The assignment operator = returns the value of the assignment as well as actually assigning the value to the left-hand operand. Because of that, assignments can be chained together. This can be useful when assigning the same value to a number of items. For example,
x = y = z = 13
This statement assigns the value 13 to x, y, and z. All the variables in this multiple assignment statement must be declared before. Such a statement works from right to left. First, 13 is assigned to z, then the value of z, which is now 13, is assigned to y, and y's value of 13 is assigned to x.
More Examples
Here are some C++ programs that you might enjoy:
- Add two Numbers
- Check Even or Odd
- Check Prime or Not
- Check Alphabet or Not
- Check Vowel or Not
- Check Armstrong or Not
- Generate Armstrong Numbers
- Find ncR nPr
- Find Factorial of Number
- Find HCF LCM
« Previous Tutorial Next Tutorial »