CProgramming Tutorial

  • C Programming Tutorial
  • C - Overview
  • C - Features
  • C - History
  • C - Environment Setup
  • C - Program Structure
  • C - Hello World
  • C - Compilation Process
  • C - Comments
  • C - Keywords
  • C - Identifiers
  • C - User Input
  • C - Basic Syntax
  • C - Data Types
  • C - Variables
  • C - Integer Promotions
  • C - Type Conversion
  • C - Constants
  • C - Literals
  • C - Escape sequences
  • C - Format Specifiers
  • C - Storage Classes
  • C - Operators
  • C - Decision Making
  • C - While loop
  • C - Functions
  • C - Scope Rules
  • C - Pointers
  • C - Strings
  • C - Structures
  • C - Bit Fields
  • C - Typedef
  • C - Input & Output
  • C - File I/O
  • C - Preprocessors
  • C - Header Files
  • C - Type Casting
  • C - Error Handling
  • C - Recursion
  • C - Variable Arguments
  • C - Memory Management
  • C - Command Line Arguments
  • C Programming Resources
  • C - Questions & Answers
  • C - Quick Guide
  • C - Useful Resources
  • C - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

Assignment Operators in C

In C, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable or an expression. The value to be assigned forms the right hand operand, whereas the variable to be assigned should be the operand to the left of = symbol, which is defined as a simple assignment operator in C. In addition, C has several augmented assignment operators.

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

Simple assignment operator (=)

The = operator is the most frequently used operator in C. As per ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed. You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.

You can use a literal, another variable or an expression in the assignment statement.

Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.

In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.

On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −

Augmented assignment operators

In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.

For example, the expression a+=b has the same effect of performing a+b first and then assigning the result back to the variable a.

Similarly, the expression a<<=b has the same effect of performing a<<b first and then assigning the result back to the variable a.

Here is a C program that demonstrates the use of assignment operators in C:

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

Library homepage

  • school Campus Bookshelves
  • menu_book Bookshelves
  • perm_media Learning Objects
  • login Login
  • how_to_reg Request Instructor Account
  • hub Instructor Commons
  • Download Page (PDF)
  • Download Full Book (PDF)
  • Periodic Table
  • Physics Constants
  • Scientific Calculator
  • Reference & Cite
  • Tools expand_more
  • Readability

selected template will load here

This action is not available.

Engineering LibreTexts

3.5: Assignment Operator

  • Last updated
  • Save as PDF
  • Page ID 11248

  • Kenneth Leroy Busbee
  • Houston Community College via OpenStax CNX

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 C++ programming language the symbol used 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.

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. 

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 assigned to the variable named: total_cousins.

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.

Definitions

  • BYJU'S GATE
  • GATE Study Material
  • GATE Notes For CSE
  • Introduction To C Programming
  • Operators In C

Assignment Operators in C

We use this type of operator to transform as well as assign the values to any variable in an operation. In any given assignment operator, the right side is a value, and the left side is a variable. The value present on the right side of the operator must have the same data type as that of the variable present on the left side. In any other case, the compiler raises an error.

In this article, we will take a look into the Assignment Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering) . Read ahead to know more.

Table of Contents

  • Working Of Assignment Operators In C
  • Example Of Assignment Operators In C
  • Practice Problems On Assignment Operators In C

Types of Assignment Operators in C

An assignment operator is basically a binary operator that helps in modifying the variable to its left with the use of the value to its right. We utilize the assignment operators to transform and assign values to any variables.

Here is a list of the assignment operators that you can find in the C language:

  • basic assignment ( = )
  • subtraction assignment ( -= )
  • addition assignment ( += )
  • division assignment ( /= )
  • multiplication assignment ( *= )
  • modulo assignment ( %= )
  • bitwise XOR assignment ( ^= )
  • bitwise OR assignment ( |= )
  • bitwise AND assignment ( &= )
  • bitwise right shift assignment ( >>= )
  • bitwise left shift assignment ( <<= )

Working of Assignment Operators in C

Here is a table that discusses, in brief, all the Assignment operators that the C language supports:

Example of Assignment Operators in C

Let us look at an example to understand how these work in a code:

#include <stdio.h>

int x = 21;

printf(“Line A – = Example of the Value of y = %d\n”, y );

printf(“Line B – -= Example of the Value of y = %d\n”, y );

printf(“Line C – += Example of the Value of c = %d\n”, c );

printf(“Line D – /= Example of the Value of y = %d\n”, y );

printf(“Line E – *= Example of the Value of y = %d\n”, y );

y <<= 2;

printf(“Line F – <<= Example of the Value of y = %d\n”, y );

printf(“Line G – %= Example of the Value of y = %d\n”, y );

y &= 2;

printf(“Line H – &= Example of the Value of y = %d\n”, y );

y >>= 2;

printf(“Line I – >>= Example of the Value of y = %d\n”, y );

printf(“Line J – |= Example of the Value of y = %d\n”, y );

printf(“Line K – ^= Example of the Value of y = %d\n”, y );

The compilation and execution of the program mentioned above will produce a result as follows:

Line A – = Example of the Value of y = 21

Line B – -= Example of the Value of y = 21

Line C – += Example of the Value of y = 42

Line D – /= Example of the Value of y = 21

Line E – *= Example of the Value of y = 441

Line F – <<= Example of the Value of y = 44

Line G – %= Example of the Value of y = 11

Line H – &= Example of the Value of y = 2

Line I – >>= Example of the Value of y = 11

Line J – |= Example of the Value of y = 2

Line K – ^= Example of the Value of y = 0

Here is another example of how the assignment operators work in the C language:

int y = 10;

printf(“z = x + y = %d \n”,z);

printf(“z += x = %d \n”,z);

printf(“z -= x = %d \n”,z);

printf(“z *= x = %d \n”,z);

printf(“z /= x = %d \n”,z);

printf(“z %= x = %d \n”,z);

c &= x ;

printf(“c &= x = %d \n”,z);

printf(“z ^= x = %d \n”,z);

printf(“z |= x = %d \n”,z);

z <<= 2 ;

printf(“z <<= 2 = %d \n”,z);

z >>= 2 ;

printf(“z >>= 2 = %d \n”,z);

The output generated here will be:

z = x + y = 15

z += x = 20

z -= x = 15

z *= x = 75

z &= x = 0

z ^= x = 10

z |= x = 10

z <<= 2 = 40

z >>= 2 = 10

z >>= 2 = 2

Practice Problems on Assignment Operators in C

1. What would be the output obtained from the program given below?

#include<stdio.h>

p += p += p += 3;

printf(“%d”,p);

Answer – A. 20

p+=p+=p+=3; it can written as p+=p+=p=p+3; p=2; Or, p+=p+=5; p=5; Or, p+=p=5+5; p=5; Or, p+=10; p=10; Or, p=p+10; p=10; Or, p=20. So, finally p=20.

2. Which of these is an invalid type of assignment operator?

D. None of these

Answer – D. None of these

All of these are valid types of assignment operators.

How does the /= operator work? Is it a combination of two other operators?

Yes, the /+ operator is a combination of the = and / operators. The / operator divides the current value of the available variable first on the left using the available value on the right. It then assigns the obtained result to the available variable on the left side.

What is the most basic operator among all the assignment operators available in the C language?

The = operator is the most basic one used in the C language. We use this operator to assign the value available in the right to the value mentioned on the left side of the operator.

Keep learning and stay tuned to get the latest updates on  GATE Exam  along with  GATE Eligibility Criteria ,  GATE 2023 ,  GATE Admit Card ,  GATE Syllabus for CSE (Computer Science Engineering) ,  GATE CSE Notes ,  GATE CSE Question Paper , and more.

Also Explore,

  • Arithmetic Operators in C
  • Bitwise Operators in C
  • Increment and Decrement Operators in C
  • Logical Operators in C
  • Operators in C
  • Relational Operators in C

Leave a Comment Cancel reply

Your Mobile number and Email id will not be published. Required fields are marked *

Request OTP on Voice Call

Post My Comment

assignment operator names

GATE 2024 - Your dream can come true!

Download the ultimate guide to gate preparation.

  • Share Share

Register with BYJU'S & Download Free PDFs

Register with byju's & watch live videos.

  • Skip to main content
  • Skip to search
  • Skip to select language
  • Sign up for free
  • English (US)

Assignment (=)

The assignment ( = ) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value. This allows multiple assignments to be chained in order to assign a single value to multiple variables.

A valid assignment target, including an identifier or a property accessor . It can also be a destructuring assignment pattern .

An expression specifying the value to be assigned to x .

Return value

The value of y .

Thrown in strict mode if assigning to an identifier that is not declared in the scope.

Thrown in strict mode if assigning to a property that is not modifiable .

Description

The assignment operator is completely different from the equals ( = ) sign used as syntactic separators in other locations, which include:

  • Initializers of var , let , and const declarations
  • Default values of destructuring
  • Default parameters
  • Initializers of class fields

All these places accept an assignment expression on the right-hand side of the = , so if you have multiple equals signs chained together:

This is equivalent to:

Which means y must be a pre-existing variable, and x is a newly declared const variable. y is assigned the value 5 , and x is initialized with the value of the y = 5 expression, which is also 5 . If y is not a pre-existing variable, a global variable y is implicitly created in non-strict mode , or a ReferenceError is thrown in strict mode. To declare two variables within the same declaration, use:

Simple assignment and chaining

Value of assignment expressions.

The assignment expression itself evaluates to the value of the right-hand side, so you can log the value and assign to a variable at the same time.

Unqualified identifier assignment

The global object sits at the top of the scope chain. When attempting to resolve a name to a value, the scope chain is searched. This means that properties on the global object are conveniently visible from every scope, without having to qualify the names with globalThis. or window. or global. .

Because the global object has a String property ( Object.hasOwn(globalThis, "String") ), you can use the following code:

So the global object will ultimately be searched for unqualified identifiers. You don't have to type globalThis.String ; you can just type the unqualified String . To make this feature more conceptually consistent, assignment to unqualified identifiers will assume you want to create a property with that name on the global object (with globalThis. omitted), if there is no variable of the same name declared in the scope chain.

In strict mode , assignment to an unqualified identifier in strict mode will result in a ReferenceError , to avoid the accidental creation of properties on the global object.

Note that the implication of the above is that, contrary to popular misinformation, JavaScript does not have implicit or undeclared variables. It just conflates the global object with the global scope and allows omitting the global object qualifier during property creation.

Assignment with destructuring

The left-hand side of can also be an assignment pattern. This allows assigning to multiple variables at once.

For more information, see Destructuring assignment .

Specifications

Browser compatibility.

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

  • Assignment operators in the JS guide
  • Destructuring assignment

cppreference.com

Copy assignment operator.

A copy assignment operator is a non-template non-static member function with the name operator = that can be called with an argument of the same class type and copies the content of the argument without mutating the argument.

[ edit ] Syntax

For the formal copy assignment operator syntax, see function declaration . The syntax list below only demonstrates a subset of all valid copy assignment operator syntaxes.

[ edit ] Explanation

The copy assignment operator is called whenever selected by overload resolution , e.g. when an object appears on the left side of an assignment expression.

[ edit ] Implicitly-declared copy assignment operator

If no user-defined copy assignment operators are provided for a class type, the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T & T :: operator = ( const T & ) if all of the following is true:

  • each direct base B of T has a copy assignment operator whose parameters are B or const B & or const volatile B & ;
  • each non-static data member M of T of class type or array of class type has a copy assignment operator whose parameters are M or const M & or const volatile M & .

Otherwise the implicitly-declared copy assignment operator is declared as T & T :: operator = ( T & ) .

Due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument.

A class can have multiple copy assignment operators, e.g. both T & T :: operator = ( T & ) and T & T :: operator = ( T ) . If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default . (since C++11)

The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in dynamic exception specification (until C++17) noexcept specification (since C++17)

Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.

[ edit ] Implicitly-defined copy assignment operator

If the implicitly-declared copy assignment operator is neither deleted nor trivial, it is defined (that is, a function body is generated and compiled) by the compiler if odr-used or needed for constant evaluation (since C++14) . For union types, the implicitly-defined copy assignment copies the object representation (as by std::memmove ). For non-union class types, the operator performs member-wise copy assignment of the object's direct bases and non-static data members, in their initialization order, using built-in assignment for the scalars, memberwise copy-assignment for arrays, and copy assignment operator for class types (called non-virtually).

[ edit ] Deleted copy assignment operator

An implicitly-declared or explicitly-defaulted (since C++11) copy assignment operator for class T is undefined (until C++11) defined as deleted (since C++11) if any of the following conditions is satisfied:

  • T has a non-static data member of a const-qualified non-class type (or possibly multi-dimensional array thereof).
  • T has a non-static data member of a reference type.
  • T has a potentially constructed subobject of class type M (or possibly multi-dimensional array thereof) such that the overload resolution as applied to find M 's copy assignment operator
  • does not result in a usable candidate, or
  • in the case of the subobject being a variant member , selects a non-trivial function.

[ edit ] Trivial copy assignment operator

The copy assignment operator for class T is trivial if all of the following is true:

  • it is not user-provided (meaning, it is implicitly-defined or defaulted);
  • T has no virtual member functions;
  • T has no virtual base classes;
  • the copy assignment operator selected for every direct base of T is trivial;
  • the copy assignment operator selected for every non-static class type (or array of class type) member of T is trivial.

A trivial copy assignment operator makes a copy of the object representation as if by std::memmove . All data types compatible with the C language (POD types) are trivially copy-assignable.

[ edit ] Eligible copy assignment operator

Triviality of eligible copy assignment operators determines whether the class is a trivially copyable type .

[ edit ] Notes

If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either a prvalue such as a nameless temporary or an xvalue such as the result of std::move ), and selects the copy assignment if the argument is an lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.

It is unspecified whether virtual base class subobjects that are accessible through more than one path in the inheritance lattice, are assigned more than once by the implicitly-defined copy assignment operator (same applies to move assignment ).

See assignment operator overloading for additional detail on the expected behavior of a user-defined copy-assignment operator.

[ edit ] Example

[ edit ] defect reports.

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

[ edit ] See also

  • converting constructor
  • copy constructor
  • copy elision
  • default constructor
  • aggregate initialization
  • constant initialization
  • copy initialization
  • default initialization
  • direct initialization
  • initializer list
  • list initialization
  • reference initialization
  • value initialization
  • zero initialization
  • move assignment
  • move constructor
  • Recent changes
  • Offline version
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link
  • Page information
  • In other languages
  • This page was last modified on 2 February 2024, at 15:13.
  • This page has been accessed 1,333,785 times.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

This browser is no longer supported.

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

about_Assignment_Operators

  • 2 contributors

Short description

Describes how to use operators to assign values to variables.

Long description

Assignment operators assign one or more values to a variable. The equals sign ( = ) is the PowerShell assignment operator. PowerShell also has the following compound assignment operators: += , -= , *= , %= , ++ , -- , ??= . Compound assignment operators perform operations on the values before the assignment.

The syntax of the assignment operators is as follows:

  • <assignable-expression> <assignment-operator> <value>

Assignable expressions include variables and properties. The value can be a single value, an array of values, or a command, expression, or statement.

The increment and decrement operators are unary operators. Each has prefix and postfix versions.

  • <assignable-expression><operator>
  • <operator><assignable-expression>

The value of the assignable expression must be a number or it must be convertible to a number.

Using the assignment operator

Variables are named memory spaces that store values. You store the values in variables using the assignment operator = . The new value can replace the existing value of the variable, or you can append a new value to the existing value. For example, the following statement assigns the value PowerShell to the $MyShell variable:

When you assign a value to a variable in PowerShell, the variable is created if it didn't already exist. For example, the first of the following two assignment statements creates the $a variable and assigns a value of 6 to $a . The second assignment statement assigns a value of 12 to $a . The first statement creates a new variable. The second statement changes only its value:

Variables in PowerShell don't have a specific data type unless you cast them. When a variable contains only one object, the variable takes the data type of that object. When a variable contains a collection of objects, the variable has the System.Object data type. Therefore, you can assign any type of object to the collection. The following example shows that you can add process objects, service objects, strings, and integers to a variable without generating an error:

Because the assignment operator = has a lower precedence than the pipeline operator | , parentheses aren't required to assign the result of a command pipeline to a variable. For example, the following command sorts the services on the computer and then assigns the sorted services to the $a variable:

You can also assign the value created by a statement to a variable, as in the following example:

This example assigns zero to the $a variable if the value of $b is less than zero. It assigns the value of $b to $a if the value of $b isn't less than zero.

To assign an array (multiple values) to a variable, separate the values with commas, as follows:

To assign a hash table to a variable, use the standard hash table notation in PowerShell. Type an at sign @ followed by key/value pairs that are separated by semicolons ; and enclosed in braces { } . For example, to assign a hashtable to the $a variable, type:

To assign hexadecimal values to a variable, precede the value with 0x . PowerShell converts the hexadecimal value (0x10) to a decimal value (in this case, 16) and assigns that value to the $a variable. For example, to assign a value of 0x10 to the $a variable, type:

To assign an exponential value to a variable, type the root number, the letter e , and a number that represents a multiple of 10. For example, to assign a value of 3.1415 to the power of 1,000 to the $a variable, type:

PowerShell can also convert kilobytes KB , megabytes MB , and gigabytes GB into bytes. For example, to assign a value of 10 kilobytes to the $a variable, type:

Using compound assignment operators

Compound assignment operators perform numeric operations on the values before the assignment.

Compound assignment operators don't use dynamic scoping. The variable is always in the current scope.

In the following example, the variable $x is defined in the global scope. The braces create a new scope. The variable $x inside the braces is a new instance and not a copy of the global variable.

When you use the regular assignment operator, you get a copy of the variable from the parent scope. But notice that $x in the parent scope is not changed.

The assignment by addition operator

The assignment by addition operator += either increments the value of a variable or appends the specified value to the existing value. The action depends on whether the variable has a numeric or string type and whether the variable contains a single value (a scalar) or multiple values (a collection).

The += operator combines two operations. First, it adds, and then it assigns. Therefore, the following statements are equivalent:

When the variable contains a single numeric value, the += operator increments the existing value by the amount on the right side of the operator. Then, the operator assigns the resulting value to the variable. The following example shows how to use the += operator to increase the value of a variable:

When the value of the variable is a string, the value on the right side of the operator is appended to the string, as follows:

When the value of the variable is an array, the += operator appends the values on the right side of the operator to the array. Unless the array is explicitly typed by casting, you can append any type of value to the array, as follows:

When the value of a variable is a hash table, the += operator appends the value on the right side of the operator to the hash table. However, because the only type that you can add to a hash table is another hash table, all other assignments fail.

For example, the following command assigns a hash table to the $a variable. Then, it uses the += operator to append another hash table to the existing hash table, effectively adding a new key-value pair to the existing hash table. This command succeeds, as shown in the output:

The following command attempts to append an integer "1" to the hash table in the $a variable. This command fails:

The assignment by subtraction operator

The assignment by subtraction operator -= decrements the value of a variable by the value that's specified on the right side of the operator. This operator can't be used with string variables, and it can't be used to remove an element from a collection.

The -= operator combines two operations. First, it subtracts, and then it assigns. Therefore, the following statements are equivalent:

The following example shows how to use of the -= operator to decrease the value of a variable:

You can also use the -= assignment operator to decrease the value of a member of a numeric array. To do this, specify the index of the array element that you want to change. In the following example, the value of the third element of an array (element 2) is decreased by 1:

You can't use the -= operator to delete the values of a variable. To delete all the values that are assigned to a variable, use the Clear-Item or Clear-Variable cmdlets to assign a value of $null or "" to the variable.

To delete a particular value from an array, use array notation to assign a value of $null to the particular item. For example, the following statement deletes the second value (index position 1) from an array:

To delete a variable, use the Remove-Variable cmdlet. This method is useful when the variable is explicitly cast to a particular data type, and you want an untyped variable. The following command deletes the $a variable:

The assignment by multiplication operator

The assignment by multiplication operator *= multiplies a numeric value or appends the specified number of copies of the string value of a variable.

When a variable contains a single numeric value, that value is multiplied by the value on the right side of the operator. For example, the following example shows how to use the *= operator to multiply the value of a variable:

In this case, the *= operator combines two operations. First, it multiplies, and then it assigns. Therefore, the following statements are equivalent:

When a variable contains a string value, PowerShell appends the specified number of strings to the value, as follows:

To multiply an element of an array, use an index to identify the element that you want to multiply. For example, the following command multiplies the first element in the array (index position 0) by 2:

The assignment by division operator

The assignment by division operator /= divides a numeric value by the value that's specified on the right side of the operator. The operator can't be used with string variables.

The /= operator combines two operations. First, it divides, and then it assigns. Therefore, the following two statements are equivalent:

For example, the following command uses the /= operator to divide the value of a variable:

To divide an element of an array, use an index to identify the element that you want to change. For example, the following command divides the second element in the array (index position 1) by 2:

The assignment by modulus operator

The assignment by modulus operator %= divides the value of a variable by the value on the right side of the operator. Then, the %= operator assigns the remainder (known as the modulus) to the variable. You can use this operator only when a variable contains a single numeric value. You can't use this operator when a variable contains a string variable or an array.

The %= operator combines two operations. First, it divides and determines the remainder, and then it assigns the remainder to the variable. Therefore, the following statements are equivalent:

The following example shows how to use the %= operator to save the modulus of a quotient:

The increment and decrement operators

The increment operator ++ increases the value of a variable by 1. When you use the increment operator in a simple statement, no value is returned. To view the result, display the value of the variable, as follows:

To force a value to be returned, enclose the variable and the operator in parentheses, as follows:

The increment operator can be placed before (prefix) or after (postfix) a variable. The prefix version of the operator increments a variable before its value is used in the statement, as follows:

The postfix version of the operator increments a variable after its value is used in the statement. In the following example, the $c and $a variables have different values because the value is assigned to $c before $a changes:

The decrement operator -- decreases the value of a variable by 1. As with the increment operator, no value is returned when you use the operator in a simple statement. Use parentheses to return a value, as follows:

The prefix version of the operator decrements a variable before its value is used in the statement, as follows:

The postfix version of the operator decrements a variable after its value is used in the statement. In the following example, the $d and $a variables have different values because the value is assigned to $d before $a changes:

Null-coalescing assignment operator

The null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.

For more information, see Null-coalescing operator .

Microsoft .NET types

By default, when a variable has only one value, the value that's assigned to the variable determines the data type of the variable. For example, the following command creates a variable that has the System.Int32 type:

To find the .NET type of a variable, use the GetType method and its FullName property. Be sure to include the parentheses after the GetType method name, even though the method call has no arguments:

To create a variable that contains a string, assign a string value to the variable. To indicate that the value is a string, enclose it in quotation marks, as follows:

If the first value that's assigned to the variable is a string, PowerShell treats all operations as string operations and casts new values to strings. This occurs in the following example:

If the first value is an integer, PowerShell treats all operations as integer operations and casts new values to integers. This occurs in the following example:

You can cast a new scalar variable as any .NET type by placing the type name in brackets that precede either the variable name or the first assignment value. When you cast a variable, you are defining the type of data that can be stored in the variable.

For example, the following command casts the variable as a string type:

The following example casts the first value, instead of casting the variable:

You can't recast the data type of an existing variable if its value can't be converted to the new data type.

To change the data type, you must replace its value, as follows:

In addition, when you precede a variable name with a data type, the type of that variable is locked unless you explicitly override the type by specifying another data type. If you try to assign a value that's incompatible with the existing type, and you don't explicitly override the type, PowerShell displays an error, as shown in the following example:

In PowerShell, the data types of variables that contain multiple items in an array are handled differently from the data types of variables that contain a single item. Unless a data type is specifically assigned to an array variable, the data type is always System.Object [] . This data type is specific to arrays.

Sometimes, you can override the default type by specifying another type. For example, the following command casts the variable as a string [] array type:

PowerShell variables can be any .NET data type. In addition, you can assign any fully qualified .NET data type that's available in the current process. For example, the following command specifies a System.DateTime data type:

The variable will be assigned a value that conforms to the System.DateTime data type. The value of the $a variable would be the following:

Assigning multiple variables

In PowerShell, you can assign values to multiple variables using a single command. The first element of the assignment value is assigned to the first variable, the second element is assigned to the second variable, the third element to the third variable. This is known as multiple assignment .

For example, the following command assigns the value 1 to the $a variable, the value 2 to the $b variable, and the value 3 to the $c variable:

If the assignment value contains more elements than variables, all the remaining values are assigned to the last variable. For example, the following command contains three variables and five values:

Therefore, PowerShell assigns the value 1 to the $a variable and the value 2 to the $b variable. It assigns the values 3, 4, and 5 to the $c variable. To assign the values in the $c variable to three other variables, use the following format:

This command assigns the value 3 to the $d variable, the value 4 to the $e variable, and the value 5 to the $f variable.

If the assignment value contains fewer elements than variables, the remaining variables are assigned the value $null . For example, the following command contains three variables and two values:

Therefore, PowerShell assigns the value 1 to the $a variable and the value 2 to the $b variable. The $c variable is $null .

You can also assign a single value to multiple variables by chaining the variables. For example, the following command assigns a value of "three" to all four variables:

Variable-related cmdlets

In addition to using an assignment operation to set a variable value, you can also use the Set-Variable cmdlet. For example, the following command uses Set-Variable to assign an array of 1, 2, 3 to the $a variable.

  • about_Arrays
  • about_Hash_Tables
  • about_Variables
  • Clear-Variable
  • Remove-Variable
  • Set-Variable

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .

Submit and view feedback for

Additional resources

  • DSA with JS - Self Paced
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Operator
  • JS Projects
  • JS Cheat Sheet
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
  • JS Web Technology

Related Articles

  • Solve Coding Problems
  • JavaScript Arithmetic Unary Negation(-) Operator
  • JavaScript in Operator
  • Operator precedence in JavaScript
  • JavaScript Ternary Operator
  • JavaScript Instanceof Operator
  • JavaScript Arithmetic Operators
  • JavaScript Arithmetic Unary Plus(+) Operator
  • JavaScript Comparison Operators
  • JavaScript Remainder Assignment(%=) Operator
  • JavaScript Remainder(%) Operator
  • JavaScript Comma Operator
  • JavaScript Pipeline Operator
  • What is the rest parameter and spread operator in JavaScript ?
  • The 'new' operator in Javascript for Error Handling
  • What is JavaScript >>> Operator and how to use it ?
  • What does OR Operator || in a Statement in JavaScript ?
  • What is (~~) "double tilde" operator in JavaScript ?
  • Explain the purpose of the ‘in’ operator in JavaScript
  • How to calculate multiplication and division of two numbers using JavaScript ?

JavaScript Assignment Operators

JavaScript assignment operator is equal (=) which assigns the value of the right-hand operand to its left-hand operand. That is if a = b assigns the value of b to a.

The simple assignment operator is used to assign a value to a variable. The assignment operation evaluates the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables. See the example.

Assignment Operators List: There are so many assignment operators as shown in the table with the description.

Below we have described each operator with an example code:

Addition Assignment : This operator adds the value to the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible. In case of concatenation then we use the string as an operand.

Subtraction Assignment: This operator subtracts the value of the right operand from a variable and assigns the result to the variable.

Multiplication Assignment: This operator multiplies a variable by the value of the right operand and assigns the result to the variable.

Division Assignment : This operator divides a variable by the value of the right operand and assigns the result to the variable.

Remainder Assignment : This operator divides a variable by the value of the right operand and assigns the remainder to the variable.

Exponentiation Assignment: This operator raises the value of a variable to the power of the right operand.

Left Shift Assignment: This operator moves the specified amount of bits to the left and assigns the result to the variable.

Right Shift Assignment: This operator moves the specified amount of bits to the right and assigns the result to the variable.

Bitwise AND Assignment : This operator uses the binary representation of both operands, does a bitwise AND operation on them, and assigns the result to the variable.

Bitwise OR Assignment : This operator uses the binary representation of both operands, does a bitwise OR operation on them, and assigns the result to the variable.

Bitwise XOR Assignment: This operator uses the binary representation of both operands, does a bitwise XOR operation on them, and assigns the result to the variable.

Please Login to comment...

  • javascript-operators
  • Web Technologies
  • akshaysingh98088
  • sagartomar9927
  • vishalkumar2204

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

C# Tutorial

C# examples, c# assignment operators, assignment operators.

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x :

Try it Yourself »

The addition assignment operator ( += ) adds a value to a variable:

A list of all assignment operators:

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

IMAGES

  1. Assignment Operators ~ Computer Languages (clcoding)

    assignment operator names

  2. Operators in C Programming

    assignment operator names

  3. Assignment Operators in C++

    assignment operator names

  4. Python Tutorials: Assignment Operators In python

    assignment operator names

  5. Operators in Java and its Types

    assignment operator names

  6. PPT

    assignment operator names

VIDEO

  1. assignment 1 (C)

  2. Storing values with assignment operator

  3. Arithmetic Operator, Arithmetic Assignment Operator, Bitwise Operator

COMMENTS

  1. Assignment operators

    std::{{} (since C++11) In overload resolution against user-defined operators, for every type , the following function signatures participate in overload resolution: For every enumeration or pointer to member type , optionally volatile-qualified, the following function signature participates in overload resolution: (&)

  2. Assignment Operators in C

    Example: (a *= b) can be written as (a = a * b) If initially value stored in a is 5. Then (a *= 6) = 30. 5. "/=" This operator is combination of '/' and '=' operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. Example:

  3. Assignment Operators in C

    The following table lists the assignment operators supported by the C language − Example Try the following example to understand all the assignment operators available in C − Live Demo

  4. Assignment operators

    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.

  5. Assignment operators

    Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right. becomes equal to becomes equal to the addition of becomes equal to the subtraction of becomes equal to the product of becomes equal to the division of becomes equal to the remainder of

  6. C Assignment Operators

    6 contributors Feedback In this article Syntax See also An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value.

  7. Python's Assignment Operator: Write Robust Assignments

    To create a new variable or to update the value of an existing one in Python, you'll use an assignment statement. This statement has the following three components: A left operand, which must be a variable. The assignment operator ( =) A right operand, which can be a concrete value, an object, or an expression.

  8. 4.6: Assignment Operator

    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. As we have seen, assignment operators are used to assigning value to a variable.

  9. C++ Assignment Operators

    Assignment Operators. Assignment operators are used to assign values to variables. In the example below, we use the assignment operator ( =) to assign the value 10 to a variable called x:

  10. 3.5: Assignment Operator

    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 C++ programming language the symbol used is the equal symbol.

  11. Java Assignment Operators with Examples

    1. (=) operator: This is the most straightforward assignment operator, which is used to assign the value on the right to the variable on the left. This is the basic definition of an assignment operator and how it functions. Syntax:

  12. Operators in C and C++

    Assignment operators All assignment expressions exist in C and C++ and can be overloaded in C++. For the given operators the semantic of the built-in combined assignment expression a ⊚= b is equivalent to a = a ⊚ b, except that a is evaluated only once. Member and pointer operators Other operators Notes:

  13. Assignment operators

    In this article. The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand. The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly convertible to it.

  14. Assignment Operators in C

    Put And Post Difference. Assignment Operators in C: An assignment operator is basically a binary operator that helps in modifying the variable to its left with the use of the value to its right. We utilize these operators to assign values to any variables. Visit to know more about Assignment Operators in C and other CSE notes for the GATE Exam.

  15. Assignment (=)

    The assignment ( =) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value. This allows multiple assignments to be chained in order to assign a single value to multiple variables. Try it Syntax js x = y Parameters x

  16. Assignment Operators in Python

    1) Assign: This operator is used to assign the value of the right side of the expression to the left side operand. Syntax: x = y + z Example: Python3 a = 3 b = 5 c = a + b print(c) Output: 8 2) Add and Assign: This operator is used to add the right side operand with the left side operand and then assigning the result to the left operand. Syntax:

  17. Copy assignment operator

    A copy assignment operator is a non-template non-static member function with the name that can be called with an argument of the same class type and copies the content of the argument without mutating the argument. Implicitly-declared copy assignment operator. Implicitly-defined copy assignment operator. Deleted copy assignment operator.

  18. What are the differences between "=" and "<-" assignment operators?

    The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions. Share.

  19. c++

    The traditional canonical form of the assignment operator looks like this: TestClass& operator=(const TestClass& Other); (you don't want to invoke the copy constructor for assignment, too) and it returns a reference to *this.. A naive implementation would assign each data member individually:

  20. Operators in Programming

    Assignment Operators: Assign values to variables. Examples: = (assign), += (add and assign), -=, *= (multiply and assign), /=, %=. Increment and Decrement Operators: Increase or decrease the value of a variable by 1. Examples: ++ (increment), — (decrement).

  21. about Assignment Operators

    PowerShell also has the following compound assignment operators: +=, -=, *=, %=, ++, --, ??= . Compound assignment operators perform operations on the values before the assignment. Syntax The syntax of the assignment operators is as follows: <assignable-expression> <assignment-operator> <value>

  22. JavaScript Assignment Operators

    Syntax: data=value Example: // Lets take some variables x=10 y=20 x=y // Here, x is equal to 20 y=x // Here, y is equal to 10 Assignment Operators List: There are so many assignment operators as shown in the table with the description. Below we have described each operator with an example code:

  23. C# Assignment Operators

    Assignment Operators. Assignment operators are used to assign values to variables. In the example below, we use the assignment operator ( =) to assign the value 10 to a variable called x: