The Wayback Machine - https://web.archive.org/web/20240717024800/https://www.geeksforgeeks.org/assignment-operators-in-c-c/
Open In App

Assignment Operators in C

Last Updated : 20 Mar, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Assignment operators are used for 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:

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 then assigns the result to the variable on the left. Example:

(a += b) can be written as (a = a + b)

If initially value stored in a is 5. Then (a += 6) = 11.


3. “-=” This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. Example:

(a -= b) can be written as (a = a - b)

If initially value stored in a is 8. Then (a -= 6) = 2.


4. “*=” This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. 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:

(a /= b) can be written as (a = a / b)

If initially value stored in a is 6. Then (a /= 2) = 3.


Below example illustrates the various Assignment Operators:

C
// C program to demonstrate
// working of Assignment operators

#include <stdio.h>

int main()
{

    // Assigning value 10 to a
    // using "=" operator
    int a = 10;
    printf("Value of a is %d\n", a);

    // Assigning value by adding 10 to a
    // using "+=" operator
    a += 10;
    printf("Value of a is %d\n", a);

    // Assigning value by subtracting 10 from a
    // using "-=" operator
    a -= 10;
    printf("Value of a is %d\n", a);

    // Assigning value by multiplying 10 to a
    // using "*=" operator
    a *= 10;
    printf("Value of a is %d\n", a);

    // Assigning value by dividing 10 from a
    // using "/=" operator
    a /= 10;
    printf("Value of a is %d\n", a);

    return 0;
}

Output
Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10




Previous Article
Next Article

Similar Reads

When should we write our own assignment operator in C++?
The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment operator may not be sufficient when we have pointers or a
3 min read
Default Assignment Operator and References in C++
We have discussed assignment operator overloading for dynamically allocated resources here. In this article, we discussed that when we don't write our own assignment operator, the compiler creates an assignment operator itself that does shallow copy and thus causes problems. The difference between shallow copy and deep copy becomes visible when the
2 min read
C++ Assignment Operator Overloading
Prerequisite: Operator Overloading The assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types. Assignment operator overloading is binary operator overloading.Overloading assignment operator in C++ copies all values of one
4 min read
Conversion Operators in C++
In C++, the programmer abstracts real-world objects using classes as concrete types. Sometimes, it is required to convert one concrete type to another concrete type or primitive type implicitly. Conversion operators play an important role in such situations. It is similar to the operator overloading function in class.For example consider the follow
2 min read
Arithmetic Operators in C
Arithmetic Operators are the type of operators in C that are used to perform mathematical operations in a C program. They can be used in programs to define expressions and mathematical formulas. What are C Arithmetic Operators? The C arithmetic operators are the symbols that are used to perform mathematical operations on operands. There are a total
7 min read
C | Operators | Question 2
#include &lt;stdio.h&gt; int main() {     int i = 1, 2, 3;          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (D) Explanation: Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a
1 min read
C | Operators | Question 3
#include &lt;stdio.h&gt; int main() {     int i = (1, 2, 3);          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (B) Explanation: The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the res
1 min read
C | Operators | Question 4
#include &lt;stdio.h&gt; int main() {     int i;        i = 1, 2, 3;     printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (A) Explanation: Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assign
1 min read
C | Operators | Question 5
C/C++ Code #include int main() { int i = 3; printf(\"%d\", (++i)++); return 0; } What is the output of the above program? (A)3 (B)4 (C)5 (D)Compile-time error Answer: (D)Explanation: In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when executed increments the value of variable i(i is a
1 min read
C | Operators | Question 6
What is the output of below program? #include &lt;stdio.h&gt; int foo(int* a, int* b) { int sum = *a + *b; *b = *a; return *a = sum - *b; } int main() { int i = 0, j = 1, k = 2, l; l = i++ || foo(&amp;j, &amp;k); printf(&quot;%d %d %d %d&quot;, i, j, k, l); return 0; } (A) 1 2 1 1 (B) 1 1 2 1 (C) 1 2 2 1 (D) 1 2 2 2 Answer: (A) Explanation: The con
1 min read
Practice Tags :
three90RightbarBannerImg