Don't miss an insight. Subscribe to Techopedia for free.

  • Assignment Operator

Table of Contents

What does assignment operator mean, techopedia explains assignment operator.

An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands. Unlike in C++, assignment operators in C# cannot be overloaded directly, but the user-defined types can overload the operators like +, -, /, etc. This allows the assignment operator to be used with those types.

The following are the characteristics of assignment operators:

In C#, an expression using an assignment operator might be "x op y", where x and y are operands and "op" represents the operator. The simple assignment operator "=" is used to store the value of its right-hand operand into the memory location denoted by the left-hand operand. The result is its return value. The other assignment operators that perform indicated operation on the two operands and assign a resulting value to the left operand are called compound assignment operators. These include:

Share this Term

Related Terms

Related Reading

Top 10 highest paying it certifications and how to get them, why does ai have biases, enterprise cybersecurity solutions: 5 steps to take today, 4 principles of responsible artificial intelligence systems.

Trending Articles

7 Sneaky Ways Hackers Can Get Your Facebook Password

7 Sneaky Ways Hackers Can Get Your Facebook Password

Mastering the Foundations of AI: Top 8 Beginner-Level AI Courses to Try

Mastering the Foundations of AI: Top 8 Beginner-Level AI Courses to Try

Top 10 Highest Paying IT Certifications and How to Get Them

How to Trace an IP Address

Tech moves fast! Stay ahead of the curve with Techopedia!

Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Latest Articles

Image for Why Does AI Have Biases?

By: Devin Partida | Editor-in-Chief for ReHack.com

Image for Top 10 Highest Paying IT Certifications and How to Get Them

By: Leah Zitter | Contributor

Image for Enterprise Cybersecurity Solutions: 5 Steps to Take Today

By: John Meah | Cyber Security Consultant

Image for 4 Principles of Responsible Artificial Intelligence Systems

By: Kaushik Pal | Contributor

programming assignment operator

Related Articles

Assignment Operators in C/C++

Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error. Different types of assignment operators are shown below:

Please Login to comment...

New Course Launch!

Improve your Coding Skills with Practice

Start your coding journey now.

Logo for Rebus Press

Want to create or adapt books like this? Learn more about how Pressbooks supports open publishing practices.

Kenneth Leroy Busbee

An assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. [1]

The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within most programming languages the symbol used for assignment is the equal symbol. But bite your tongue, when you see the = symbol you need to start thinking: assignment. The assignment operator has two operands. The one to the left of the operator is usually an identifier name for a variable. The one to the right of the operator is a value.

Simple Assignment

The value 21 is moved to the memory location for the variable named: age. Another way to say it: age is assigned the value 21.

Assignment with an Expression

The item to the right of the assignment operator is an expression. The expression will be evaluated and the answer is 14. The value 14 would be assigned to the variable named: total_cousins.

Assignment with Identifier Names in the Expression

The expression to the right of the assignment operator contains some identifier names. The program would fetch the values stored in those variables; add them together and get a value of 44; then assign the 44 to the total_students variable.

Programming Fundamentals by Kenneth Leroy Busbee is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License , except where otherwise noted.

Share This Book

CProgramming Tutorial

Assignment Operators in C

The following table lists the assignment operators supported by the C language −

Try the following example to understand all the assignment operators available in C −

When you compile and execute the above program, it produces the following result −

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Assignment operators

expression assignment-operator expression

assignment-operator : one of   =   *=   /=   %=   +=   -=   <<=   >>=   &=   ^=   |=

Assignment operators store a value in the object specified by the left operand. There are two kinds of assignment operations:

simple assignment , in which the value of the second operand is stored in the object specified by the first operand.

compound assignment , in which an arithmetic, shift, or bitwise operation is performed before storing the result.

All assignment operators in the following table except the = operator are compound assignment operators.

Assignment operators table

Operator keywords.

Three of the compound assignment operators have keyword equivalents. They are:

C++ specifies these operator keywords as alternative spellings for the compound assignment operators. In C, the alternative spellings are provided as macros in the <iso646.h> header. In C++, the alternative spellings are keywords; use of <iso646.h> or the C++ equivalent <ciso646> is deprecated. In Microsoft C++, the /permissive- or /Za compiler option is required to enable the alternative spelling.

Simple assignment

The simple assignment operator ( = ) causes the value of the second operand to be stored in the object specified by the first operand. If both objects are of arithmetic types, the right operand is converted to the type of the left, before storing the value.

Objects of const and volatile types can be assigned to l-values of types that are only volatile , or that aren't const or volatile .

Assignment to objects of class type ( struct , union , and class types) is performed by a function named operator= . The default behavior of this operator function is to perform a bitwise copy; however, this behavior can be modified using overloaded operators. For more information, see Operator overloading . Class types can also have copy assignment and move assignment operators. For more information, see Copy constructors and copy assignment operators and Move constructors and move assignment operators .

An object of any unambiguously derived class from a given base class can be assigned to an object of the base class. The reverse isn't true because there's an implicit conversion from derived class to base class, but not from base class to derived class. For example:

Assignments to reference types behave as if the assignment were being made to the object to which the reference points.

For class-type objects, assignment is different from initialization. To illustrate how different assignment and initialization can be, consider the code

The preceding code shows an initializer; it calls the constructor for UserType2 that takes an argument of type UserType1 . Given the code

the assignment statement

can have one of the following effects:

Call the function operator= for UserType2 , provided operator= is provided with a UserType1 argument.

Call the explicit conversion function UserType1::operator UserType2 , if such a function exists.

Call a constructor UserType2::UserType2 , provided such a constructor exists, that takes a UserType1 argument and copies the result.

Compound assignment

The compound assignment operators are shown in the Assignment operators table . These operators have the form e1 op = e2 , where e1 is a non- const modifiable l-value and e2 is:

an arithmetic type

a pointer, if op is + or -

The e1 op = e2 form behaves as e1 = e1 op e2 , but e1 is evaluated only once.

Compound assignment to an enumerated type generates an error message. If the left operand is of a pointer type, the right operand must be of a pointer type, or it must be a constant expression that evaluates to 0. When the left operand is of an integral type, the right operand must not be of a pointer type.

Result of assignment operators

The assignment operators return the value of the object specified by the left operand after the assignment. The resultant type is the type of the left operand. The result of an assignment expression is always an l-value. These operators have right-to-left associativity. The left operand must be a modifiable l-value.

In ANSI C, the result of an assignment expression isn't an l-value. That means the legal C++ expression (a += b) += c isn't allowed in C.

Expressions with binary operators C++ built-in operators, precedence, and associativity C assignment operators

Submit and view feedback for

Additional resources

certificatebadge

Assignment Operators in C

Operators are a fundamental part of all the computations that computers perform. Today we will learn about one of them known as Assignment Operators in C. Assignment Operators are used to assign values to variables. The most common assignment operator is = . Assignment Operators are Binary Operators.

Introduction

Assignment Operators help us to assign the value or result of an expression to a variable and the value on the right side must be of the same data type as the variable on the left side. They have the lowest precedence level among all the operators, and it associates from right to left. The most commonly used Assignment Operator is = . Also, Assignment Operators fall under the category of Binary Operators.

For example, x = 4 ; then that means value 4 is assigned to variable x or we can say that variable x holds value 4 .

Explanation

Let me explain to you more about Assignment Operators. Don't worry after this section, you will fully understand the definition of Assignment Operators in C.

Our example is x = 4 , so what does it tell us?

So can we say that always variables are on the Left Hand Side of the assignment operator and values are always on the Right Hand Side of the operator? YES . Please take a look at the image it will help you to understand more about the above wording.

LHS and RHS Operands

The above diagram helps us to understand that the RHS value is assigned to the LHS variable.

The LHS and RHS are known as Operands.

So the operand on the LHS of the assignment operator must be a variable and operand on RHS must be a constant , variable or expression . For example:

As mentioned, precedence levels of assignment operators are lower than all the operators we have discussed so far and it associates from right to left. Now you may wonder what do you mean by it associates from right to left? Let's understand this together.

For example:

This is totally correct and means we can also assign the same value to multiple variables with one single line of code .

So what do you get from the above line of code? Simply, variable_x , variable_y and variable_z hold the same value. YES!! TRUE. But how?

The main question is here how value is assigned to them? is first variable_x get 10 or variable_y or variable_z ? What do you say? This answer is given by the line: It associates from right to left .

So that means we have to read the line from the right side to the left. Like, at first 10 is given to variable_z then variable_y gets the value present in the variable_z and after that variable_x get the value present in the variable_y . So the above wording is equivalent to the following expression.

This is the simplest explanation about assignment operator associativity.

The most basic Assignment operator is = . It requires two operands for its work. For example, = x doesn't make any sense but variable = x makes sense because it clearly says the variable variable stores the value of x . Therefore, Assignment operators are Binary operators.

Hopefully, every point in the definition is now clear to all of you.

List of All Assignment Operators in C

We have 2 types of Assignment Operators in C :

Simple Assignment Operator in C

It is the operator used to assign the Right Operand to Left Operand . There is only one simple Assignment Operator and that is = . The general Syntax is like Left Operand = Right Operand .

Compound Assignment Operators in C

Any Binary Operator with a simple Assignment Operator will form Compound Assignment Operators.

The general syntax is like Left operand operation = Right operand . Here, the operation is what you want + , - , *, etc.

Let's take an example:

Here read carefully. After which, you will never forget how to read the syntax of a compound assignment operator.

So we read like this FIRST ADD 10 to variable_x , THEN WHATEVER THE RESULT, ASSIGN THAT RESULT TO variable_x . That means the above line of code is equal to

List of Assignment operators in C

This is the complete list of all assignment operators in C. To read the meaning of operator please keep in mind the above example.

Example program for Assignment Operators in C

This is a simple Assignment Operator.

+= Operator

This is the Addition Assignment Operator . In which the left operand becomes equal to the addition of the right operand and left operand.

In this program, x+=y means x+y , so we assign the result of x+y to x .

-= Operator

This is the Subtraction Assignment Operator .

In which left operand becomes equal to the subtraction of right operator from left operand.

The program performs the subtraction of two numbers i.e. x-=y means x = x-y . So the output is :

*= Operator

The main purpose of this operator is that this left operand becomes equal to the product of the left and right operand. This is the Multiplication Assignment Operator .

The program performs the multiplication of two numbers and then the result of the multiplication is assigned to the variable x .

/= Operator

This one is Division Assignment Operator . In this, the left operand becomes equal to the division of the left and right operand.

This program performs a division of two numbers and the result is assigned to x variable i.e. x/=y is the same as x = x/y .

%= Operator

It is well known Modulus Assignment Operator . In this , left operand becomes equal to the modulo of left and right operand.

In this program, the user checks the remainder of two number and assign that remainder to x variable.

<<= Operator

This is called the Left Shift Assignment Operator . For example x <<= y so in this, x becomes equal to x left shifted by y .

The program basically shifts every bit of x to the left side by y places and then assigns the result to x .

>>= Operator

This is called the Right Shift Assignment Operator . For example x >>= y so , x becomes equal to x right shifted by y .

The program has defined the result of expression when x is right-shifted by y places and the result is going to store in x variable.

&= Operator

This operator is called the Bitwise AND Assignment Operator . Left operand becomes equal to the bitwise AND of left and right operand.

The program performs Bitwise AND operation on every bit of x and y . After that result is going to be stored in variable x .

|= Operator

This is called the Bitwise Inclusive OR Assignment Operator Left operand becomes equal to bitwise OR of left and right operand.

like Bitwise AND Assignment Operator , this program also performs Bitwise OR operation on every bit of x and y . And after that result is going to store in x .

^= Operator

This is called the Bitwise Exclusive OR Assignment Operator Left operand becomes equal to bitwise XOR of left and right operand.

This will perform Bitwise XOR operation on every bit of x and y . After that result is going to store in x .

This is the detailed explanation with programs of all the assignment operators in C that we have. Hopefully, This is clear to you.

Happy Coding folks!!!

certificate icon

IMAGES

  1. PPT

    programming assignment operator

  2. C Programming Tutorial

    programming assignment operator

  3. The Assignment Operator in Java

    programming assignment operator

  4. 13. Assignment Operator in C++ (Hindi)

    programming assignment operator

  5. Assignment Operators

    programming assignment operator

  6. assignment-operators

    programming assignment operator

VIDEO

  1. Assignment 2 structured programming

  2. Programming Problem 3

  3. [C++ Programming] 8. Operator Overloading

  4. Programming Technique tutorial week 6 part 2

  5. [C++ Programming] 8. Operator Overloading

  6. Compilers: Programming Assignment

COMMENTS

  1. What is Assignment Operator?

    An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming

  2. Assignment Operators in C/C++

    Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right

  3. Assignment

    The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated

  4. Assignment Operators in C

    Assignment Operators in C ; = Simple assignment operator. Assigns values from right side operands to left side operand, C = A + B will assign the value of A + B

  5. Assignment operator (C++)

    In the C++ programming language, the assignment operator, = , is the operator used for assignment. Like most other operators in C++, it can be overloaded.

  6. Assignment (computer science)

    Many other notations are also in use. In some languages, the symbol used is regarded as an operator (meaning that the assignment statement as a whole returns a

  7. C Assignment Operators

    An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand.

  8. Assignment operators

    Assignment operators store a value in the object specified by the left operand. There are two kinds of assignment operations:.

  9. Assignment operators

    An assignment expression stores a value in the object designated by the left operand. There are two types of assignment operators: ... The left operand in all

  10. Assignment Operators in C

    Assignment Operators help us to assign the value or result of an expression to a variable and the value on the right side must be of the same