a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b
++a
--a
a++
a--
+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b
!a
a && b
a || b
a == b
a != b
a < b
a > b
a <= b
a >= b
a[b]
*a
&a
a->b
a.b
a(...)
a, b
(type) a
a ? b : c
sizeof
_Alignof
(since C11)
for Assignment operators |
Home » Learn C Programming from Scratch » C Assignment Operators
Summary : in this tutorial, you’ll learn about the C assignment operators and how to use them effectively.
An assignment operator assigns the vale of the right-hand operand to the left-hand operand. The following example uses the assignment operator (=) to assign 1 to the counter variable:
After the assignmment, the counter variable holds the number 1.
The following example adds 1 to the counter and assign the result to the counter:
The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand.
Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the result to the left operand.
The following example uses a compound-assignment operator (+=):
The expression:
is equivalent to the following expression:
The following table illustrates the compound-assignment operators in C:
Operator | Operation Performed | Example | Equivalent expression |
---|---|---|---|
Multiplication assignment | x *= y | x = x * y | |
Division assignment | x /= y | x = x / y | |
Remainder assignment | x %= y | x = x % y | |
Addition assignment | x += y | x = x + y | |
Subtraction assignment | x -= y | x = x – y | |
Left-shift assignment | x <<= y | x = x <<=y | |
Right-shift assignment | x >>=y | x = x >>= y | |
Bitwise-AND assignment | x &= y | x = x & y | |
Bitwise-exclusive-OR assignment | x ^= y | x = x ^ y | |
Bitwise-inclusive-OR assignment | x |= y | x = x | y |
Print Cheatsheet
C is able to perform basic mathematical operations on variables and values using the following symbols:
C can assign values to variables and perform basic mathematical operations using shorthand operators:
C can compare two values and/or variables against each other to return true or false. The operators are as follows:
C can perform logical operations using the following operators:
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
Operator | Meaning |
---|---|
Store the value of the second operand in the object specified by the first operand (simple assignment). | |
Multiply the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. | |
Divide the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. | |
Take modulus of the first operand specified by the value of the second operand; store the result in the object specified by the first operand. | |
Add the value of the second operand to the value of the first operand; store the result in the object specified by the first operand. | |
Subtract the value of the second operand from the value of the first operand; store the result in the object specified by the first operand. | |
Shift the value of the first operand left the number of bits specified by the value of the second operand; store the result in the object specified by the first operand. | |
Shift the value of the first operand right the number of bits specified by the value of the second operand; store the result in the object specified by the first operand. | |
Obtain the bitwise AND of the first and second operands; store the result in the object specified by the first operand. | |
Obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand. | |
Obtain the bitwise inclusive OR of the first and second operands; store the result in the object specified by the first operand. |
Three of the compound assignment operators have keyword equivalents. They are:
Operator | Equivalent |
---|---|
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.
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 member-wise copy assignment of the object's non-static data members and direct base classes; 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.
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 -
a type for which there exists a matching operator *op*= overload for the type of e1
The built-in 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.
The built-in assignment operators return the value of the object specified by the left operand after the assignment (and the arithmetic/logical operation in the case of compound assignment operators). 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
Was this page helpful?
In this C++ tutorial, you shall learn about Multiplication Assignment operator, its syntax, and how to use this operator, with examples.
In C++, Multiplication Assignment Operator is used to find the product of the value (right operand) and this variable (left operand) and assign the result back to this variable (left operand).
The syntax to find the product of a value 2 with variable x and assign the result to x using Multiplication Assignment Operator is
In the following example, we take a variable x with an initial value of 5 , multiply it with a value of 2 and assign the result back to x , using Multiplication Assignment Operator.
In this C++ Tutorial , we learned about Multiplication Assignment Operator in C++, with examples.
App developement, web development, online tools.
In C/C++, left shift (<<) and right shift (>>) operators are binary bitwise operators that are used to shift the bits either left or right of the first operand by the number of positions specified by the second operand allowing efficient data manipulation. In this article, we will learn about the left shift and right shift operators.
The left shift(<<) is a binary operator that takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, left-shifting an integer “ a ” with an integer “ b ” denoted as ‘ (a<<b)’ is equivalent to multiplying a with 2^b (2 raised to power b).
Example: Let’s take a=5 ; which is 101 in Binary Form. Now, if “ a is left-shifted by 2 ” i.e a=a<<2 then a will become a=a*(2^2) . Thus, a=5*(2^2)=20 which can be written as 10100.
Applications of left shift operator.
Right Shift(>>) is a binary operator that takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, right-shifting an integer “ a ” with an integer “ b ” denoted as ‘ (a>>b) ‘ is equivalent to dividing a with 2^b.
Example: let’s take a=5 ; which is 101 in Binary Form. Now, if “ a is right-shifted by 2 ” i.e a=a>>2 then a will become a=a/(2^2) . Thus, a=a/(2^2)=1 which can be written as 01 .
Applications of right shift operators.
1. The left-shift and right-shift operators should not be used for negative numbers. The result of is undefined behavior if any of the operands is a negative number. For example, results of both 1 >> -1 and 1 << -1 is undefined.
2. If the number is shifted more than the size of the integer, the behavior is undefined. For example, 1 << 33 is undefined if integers are stored using 32 bits. For bit shift of larger values 1ULL<<62 ULL is used for Unsigned Long Long which is defined using 64 bits that can store large values.
3. The left-shift by 1 and right-shift by 1 are equivalent to the product of the first term and 2 to the power given element(1<<3 = 1*pow(2,3)) and division of the first term and second term raised to power 2 (1>>3 = 1/pow(2,3)) respectively.
Must Read: Bitwise Operators in C/C++
Similar reads.
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
For example:
The first one I would have hoped would have worked correctly, and the compiler would have done the * first, but it didn’t. This is expected, because the * is on the left side to the assignment operator. Is there any way I can create another operator that operates like the ++ and – operators, kinda like this…
Operation and =* that would have the * on the right hand side, thus being a clear indicator that the * before the assignment operator is reached. I am just not sure how to break the rhs variable into pieces BEFORE it is evaluated.
Any ideas would be helpful.
Your code doesn't make sense if you're hoping to change the precendence of the *= operator.
If I started with this (ignoring the fact that it's not valid C#):
... and then had this code:
... the call to =* would set lhs as 10 , but it would have to set rhs as the result of 15 + 12 .
And that would simply be the same behaviour of the built-in *= operator.
Edit : Based on further comments by OP in the comment section of question, the operator was probably created to make it easier to write the aggregate operations : sum = sum + value , and so does not follow the normal mathematical rules, it is probably more useful then what you propose.
C# you cannot create new operator. The existing operators are built into the language, so to add any new one's at a minimum you would also want to implement some parts in compiler to parse them.
C# does allow operator overloading, but then it leads to defining your own integers and I don't think that is a happy road to go down.
There are languages other then C# which do allow this (notably functional languages where operators are just normal methods).
From C# language specifications
Certain operators can be overloaded. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type (Operator overloading).
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
COMMENTS
Assigns values from right side operands to left side operand. C = A + B will assign the value of A + B to C. +=. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A. -=. Subtract AND assignment operator.
They are derived from the grammar. In C++, the conditional operator has the same precedence as assignment operators, and prefix ++ and -- and assignment operators don't have the restrictions about their operands. Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always ...
1. "=": This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10;b = 20;ch = 'y'; 2. "+=": This operator is combination of '+' and '=' operators. This operator first adds the current value of the variable on left to the value on the right and ...
Correct behavior. CWG 1527. C++11. for assignments to class type objects, the right operand could be an initializer list only when the assignment is defined by a user-defined assignment operator. removed user-defined assignment constraint. CWG 1538. C++11. E1 ={E2} was equivalent to E1 = T(E2) (T is the type of E1), this introduced a C-style cast.
Assignment performs implicit conversion from the value of rhs to the type of lhs and then replaces the value in the object designated by lhs with the converted value of rhs. Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non ...
Code language:C++(cpp) The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand. Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the ...
5. Assignment Operators in C. Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise ...
Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...
The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators: | =. In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place.
The compound-assignment operators combine the simple-assignment operator with another binary operator. Compound-assignment operators perform the operation specified by the additional operator, then assign the result to the left operand. For example, a compound-assignment expression such as. expression1 += expression2. can be understood as.
(Note that this sequencing freedom is specific to C language, in which the result of an assignment in an rvalue. In C++ assignment evaluates to an lvalue, which requires "chained" assignments to be sequenced.) ... The operator precedence of the = operator guarantees that this is executed right-to-left. In other words, it guarantees that sample2 ...
C can assign values to variables and perform basic mathematical operations using shorthand operators: Assignment: = ... Multiplication then assignment: *= Division then assignment: /= Modulo then assignment: %= Comparing values in C. C can compare two values and/or variables against each other to return true or false. The operators are as follows:
Assignment operators table. Expand table. Operator. Meaning. =. Store the value of the second operand in the object specified by the first operand (simple assignment). *=. Multiply the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. /=.
In C++, Multiplication Assignment Operator is used to find the product of the value (right operand) and this variable (left operand) and assign the result back to this variable (left operand). The syntax to find the product of a value 2 with variable x and assign the result to x using Multiplication Assignment Operator is. x *= 2.
Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in Java The bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for the bitwise complement is ~ (Tilde). Example: Input
In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value. Syntax. variable *= value; This above expression is equivalent to the expression: variable = variable * value; Example. C++. #include <iostream>. using namespace std;
C provides 5 Binary Arithmetic Operators for performing arithmetic functions which are as follows: Add two operands. Subtract the second operand from the first operand. Multiply two operands. Divide the first operand by the second operand. Calculate the remainder when the first operand is divided by the second operand.
In C++11 terminology, you can't have two unsequenced modifications of the same scalar or a modification and a value computation using the same scalar, otherwise you have undefined behaviour. x = x * x++;
The left shift (<<) is a binary operator that takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, left-shifting an integer "a" with an integer "b" denoted as '(a<<b)' is equivalent to multiplying a with 2^b (2 raised to power b). Syntax.
You are making an incorrect application of operator precedence: the opreator * is applied before the operator + in an expression, but the operator = and the operator *= are applied after the basic math operators +,-,*,/, hence the rhs is evaluated, then *= uses that. That is the definition in C# - operator evaluation is not a textual substitution (like a C pre-processor macro).