01 Career Opportunities

02 beginner, 03 intermediate, 04 advanced, 05 training programs, c programming assignment operators, free c foundation course: learn c in 21 days, what is an assignment operator in c, types of assignment operators in c, 1. simple assignment operator (=), example of simple assignment operator, 2. compound assignment operators, example of augmented arithmetic and assignment operators, example of augmented bitwise and assignment operators, practice problems on assignment operators in c, 1. what will the value of "x" be after the execution of the following code, 2. after executing the following code, what is the value of the number variable, benefits of using assignment operators, best practices and tips for using the assignment operator, about author, live batches schedule view all.

  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Preprocessors
  • C File Handling
  • C Cheatsheet
  • C Interview Questions

Operators in C

In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples.

What is a C Operator?

An operator in C can be defined as the symbol that helps us to perform some specific mathematical, relational, bitwise, conditional, or logical computations on values and variables. The values and variables used with operators are called operands. So we can say that the operators are the symbols that perform operations on operands.

Operators-in-C

For example,

Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. To dive deeper into how operators are used with data structures, the C Programming Course Online with Data Structures covers this topic thoroughly.

Types of Operators in C

C language provides a wide range of operators that can be classified into 6 types based on their functionality:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Other Operators

1. Arithmetic Operations in C

The arithmetic operators are used to perform arithmetic/mathematical operations on operands. There are 9 arithmetic operators in C language:

Example of C Arithmetic Operators

2. relational operators in c.

The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.

These are a total of 6 relational operators in C:

Example of C Relational Operators

Here, 0 means false and 1 means true.

3. Logical Operator in C

Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false .

Example of Logical Operators in C

4. bitwise operators in c.

The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.

There are 6 bitwise operators in C:

Example of Bitwise Operators

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 an error.

The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.

In C, there are 11 assignment operators :

Example of C Assignment Operators

6. other operators.

Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here: 

sizeof Operator

  • sizeof is much used in the C programming language.
  • It is a compile-time unary operator which can be used to compute the size of its operand.
  • The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
  • Basically, the sizeof the operator is used to compute the size of the variable or datatype.

To know more about the topic refer to this article.

Comma Operator ( , )

  • The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
  • The comma operator has the lowest precedence of any C operator.
  • Comma acts as both operator and separator. 

Conditional Operator ( ? : )

  • The conditional operator is the only ternary operator in C++.
  • Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3.
  • We may replace the use of if..else statements with conditional operators.

dot (.) and arrow (->) Operators

  • Member operators are used to reference individual members of classes, structures, and unions.
  • The dot operator is applied to the actual object. 
  • The arrow operator is used with a pointer to an object.

To know more about dot operators refer to this article and to know more about arrow(->) operators refer to this article.

Cast Operator

  • Casting operators convert one data type to another. For example, int(2.2000) would return 2.
  • A cast is a special operator that forces one data type to be converted into another. 
  • The most general cast supported by most of the C compilers is as follows −   [ (type) expression ] .

addressof (&) and Dereference (*) Operators

  • Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.
  • The pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var. 

Example of Other C Operators

Unary, binary and ternary operators in c.

Operators can also be classified into three types on the basis of the number of operands they work on:

  • Unary Operators: Operators that work on single operand.
  • Binary Operators: Operators that work on two operands.
  • Ternary Operators: Operators that work on three operands.

Operator Precedence and Associativity in C

In C, it is very common for an expression or statement to have multiple operators and in these expression, there should be a fixed order or priority of operator evaluation to avoid ambiguity.

Operator Precedence and Associativity is the concept that decides which operator will be evaluated first in the case when there are multiple operators present in an expression.

The below table describes the precedence order and associativity of operators in C. The precedence of the operator decreases from top to bottom. 

To know more about operator precedence and associativity, refer to this article – Operator Precedence and Associativity in C

In this article, the points we learned about the operator are as follows:

  • Operators are symbols used for performing some kind of operation in C.
  • There are six types of operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators.
  • Operators can also be of type unary, binary, and ternary according to the number of operators they are using.
  • Every operator returns a numerical value except logical, relational, and conditional operator which returns a boolean value (true or false).
  • There is a Precedence in the operators means the priority of using one operator is greater than another operator.

FAQs on C Operators

Q1. what are operators in c.

Operators in C are certain symbols in C used for performing certain mathematical, relational, bitwise, conditional, or logical operations for the user.

Q2. What are the 7 types of operators in C?

There are 7 types of operators in C as mentioned below: Unary operator Arithmetic operator Relational operator Logical operator Bitwise operator Assignment operator Conditional operator

Q3. What is the difference between the ‘=’ and ‘==’ operators?

‘=’ is a type of assignment operator that places the value in right to the variable on left, Whereas ‘==’ is a type of relational operator that is used to compare two elements if the elements are equal or not.

Q4. What is the difference between prefix and postfix operators in C?

In prefix operations, the value of a variable is incremented/decremented first and then the new value is used in the operation, whereas, in postfix operations first the value of the variable is used in the operation and then the value is incremented/decremented. Example: b=c=10; a=b++; // a==10 a=++c; // a==11

Q5. What is the Modulo operator?

The Modulo operator(%) is used to find the remainder if one element is divided by another. Example: a % b (a divided by b) 5 % 2 == 1

Similar Reads

  • C-Operators

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

C Functions

C structures, c reference, c operators.

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

C divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

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 :

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

A list of all assignment operators:

Advertisement

Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.

The return value of a comparison is either 1 or 0 , which means true ( 1 ) or false ( 0 ). These values are known as Boolean values , and you will learn more about them in the Booleans and If..Else chapter.

Comparison operators are used to compare two values.

Note: The return value of a comparison is either true ( 1 ) or false ( 0 ).

In the following example, we use the greater than operator ( > ) to find out if 5 is greater than 3:

A list of all comparison operators:

Logical Operators

You can also test for true or false values with logical operators.

Logical operators are used to determine the logic between variables or values, by combining multiple conditions:

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]

Top Tutorials

Top references, top examples, get certified.

Codeforwin

Assignment and shorthand assignment operator in C

Quick links.

  • Shorthand assignment

Assignment operator is used to assign value to a variable (memory location). There is a single assignment operator = in C. It evaluates expression on right side of = symbol and assigns evaluated value to left side the variable.

For example consider the below assignment table.

The RHS of assignment operator must be a constant, expression or variable. Whereas LHS must be a variable (valid memory location).

Shorthand assignment operator

C supports a short variant of assignment operator called compound assignment or shorthand assignment. Shorthand assignment operator combines one of the arithmetic or bitwise operators with assignment operator.

For example, consider following C statements.

The above expression a = a + 2 is equivalent to a += 2 .

Similarly, there are many shorthand assignment operators. Below is a list of shorthand assignment operators in C.

avatar

Assignment Operators In C | A Complete Guide With Detailed Examples

Assignment Operators In C | A Complete Guide With Detailed Examples

Table of content: 

What are assignment operators in c, how assignment operators in c work step-by-step explanation, types of assignment operators in c, bitwise assignment operators in c.

  • Precedence & Associativity Of Assignment Operators In C

Table Of Assignment Operators In C

Relational vs. assignment operators in c, frequently asked questions.

Operators are symbols or special characters that perform specific operations on one or more operands. They enable various computations and manipulation of data/ operands in C programs. These operands can be variables, constants, or expressions. It is important to understand the concept of various types of operators to utilize them effectively in writing efficient C code. In this article, we will focus on assignment operators in C.

Other types of operators include arithmetic, bitwise, logical, relational, ternary/ conditional, etc. Collectively, they enable complex computations, comparisons, and control flow within programs, allowing developers to manipulate data in a variety of ways.

In simple terms, assignment operators in C programming are used to assign values to variables. This includes assigning initial value as well as updating the value of a variable with a new value based on a certain operation or calculation. The most commonly used assignment operator is the equal-to operator (=), which assigns the right-hand side value to the variable on the left-hand side of the operator.

For example, the expression x = 5 assigns the value of 5 to the variable x.

Other assignment operators in C include addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), division assignment (/=), and modulus/ modulo assignment (%=). They are also referred to as compound assignment operators. As their name suggests, they are used to assign the resultant value of addition, subtraction, multiplication, division, and modulus operations (respectively) to the left-side variable/ operand.

For example, the equation x += 5 is equivalent to x = x + 5. It updates the value of x by adding 5 to its current value.

These assignment operators can be combined with other operators to perform more complex operations in a single statement. For example, x += y * 3 updates the value of x by adding three times the value of y to its current value.

Using basic assignment operators can help make code more concise and easier to read and improve performance by reducing the number of operations required. However, it is important to use them carefully and avoid unintended consequences, such as side effects or errors. We will discuss each of these assignment operators in detail ahead, but first let's look at the basic syntax for this.

Syntax For Assignment Operators In C:

variable_name operator expression;

Here, 

  • The variable_name refers to the name/ identifier of the variable to whom you are assigning (or updating) a value. 
  • The term operator refers to the respective assignment operator you are using.
  • Expression  is the right-hand operand, i.e., it refers to the value you assign to the variable. 

Here are some examples of assignment operators and their syntax:

x = 5; // assigns the value of 5 to the variable x y += 10; // adds 10 to the current value of y and updates y with the result z *= 3; // multiplies the current value of z by 3 and updates z with the result

Given below is a stepwise description of the working mechanism of assignment operators in C.

  • Step 1 (The variable): The first step is to select the variable that we want to assign a value to. This variable can be of any data type, such as integer, float, or char .
  • Step 2 (The operator):  The second step is to select the assignment operator that we want to use. As mentioned, the most commonly used assignment operator is the simple assignment operator (=), to assign the value of the right side operand to the left side operand.
  • Step 3 (The value or expression): The third step is to provide the value (or expression for calculating value) we want to assign to the variable. This value can be a constant or an expression that combines variables and constants using arithmetic or logical operators .
  • Step 4 (The evaluation): After that, the compiler evaluates the expression on the right-hand side of the operator. This evaluation involves performing any necessary logical or arithmetic operations based on the precedence rules of the operators.
  • Step 5 (The assignment): Finally, it assigns the evaluated value to the variable on the left-hand side of the assignment operator. If we are updating the value, this assignment replaces the previous value of the variable with the new value.

Let's look at a basic C program example illustrating both these approaches of using assignment operators.

Code Example:

The initial values of x and y are: 5, 10 The updated value of x is: 15

Explanation:

We begin the simple C program example by including the <stdio.h> header file for input/output operations. 

  • We then initiate the main() function , which is the entry point of the program's execution. 
  • Inside main(), we declare two integer variables , x and y, and assign the values of 5 to x and 10 to y. Then, we print these initial values to the console using the printf() function .
  • Next, we use the addition assignment operator (as mentioned in the code comments ) to add the initial value of y to the initial value of x and assign the result to x.
  • The operation involves adding the value of y, which is 5, to the value of x, which is 10. The output is, hence, 15.
  • Once again, we use the printf() function to display the updated value of x.

All the assignment operators in C programming language can be classified into two major types with subtypes. These types and the operators included in them are mentioned below-

Simple Assignment Operator In C (=)

There is only one assignment operator that falls into this category. This is the simplest assignment operator whose only purpose is to assign the value given on the right to the variable on the left. The basic assignment operator symbol is a single equals sign (=). It is the most commonly used assignment operator in C language. Below is an example of how to use this operator in simple assignment expressions.

The value of x is 5

In the simple C code example , we include the standard input-output header file, <stdio.h>, which contains the library functions for input and output operations in C programming.

  • Inside main() funtion , we declare an integer variable named x and initialize it with the value 5 using the simple assignment operator .
  • This step reserves a memory location to store an integer value and assigns the value 5 to x.
  • Next, we use the printf() function to display the output with a descriptive string message (using formatted string) .
  • Here, we use the  %d format specifier  as a placeholder for the integer value and the newline escape sequence (\n) to shift the cursor to the next line afterwards. 
  • Lastly, the program successfully completes execution with the return 0 statement . It indicates the successful termination of the main() function and the program as a whole.

It will be easier to understand why we call this operator simple when you check out the other category, i.e., compound operators. 

Compound Assignment Operators In C

The compound assignment operators are called as such because they combine a binary operation with the assignment operation. They perform a binary operation on the values of the variable and the value on the right-hand side and then assign the result to the variable on the left-hand side/ left operand.

There are five types of compound assignment operator in C:

  • Subtraction Assignment Operator (-=)
  • Addition Assignment Operator (+=)
  • Multiplication Assignment Operator (=)
  • Division Assignment Operator (/=)
  • Modulus Assignment Operator (%=)

The advantage of using these operators is that they provide a shorthand notation for performing common operations such as addition, multiplication, division, etc. 

Now that you have a broad idea of what compound assignment operators in C are, let's explore each type of assignment operator in detail. 

Addition Assignment Operator In C (+=)

The addition assignment operator adds the value given on its right side to the current value of the variable on the left. It then assigns the resultant value back to the variable on the left.

In other words, it adds the initial value of the first variable to the initial value of a second variable and assigns it back to the first variable. Below is a C program example illustrating the use of this compound assignment operator. 

The value of x is 8

In the  C code example, we first include the essential header files.

  • Inside the main() function , we declare an integer variable, x , and initialize it with the value 5 using the simple assignment operator. 
  • Next, we use the addition assignment operator (+=) to add a value 3 to x (variable on the left side) and assign the result back to it.
  • Here, we add 3 to x's initial value and reassign it back. That is,  x += 3 is equivalent to x = x + 3, which adds 3 to x's value and stores the result back into x.
  • Then, we use the printf() function to display the output to the console, after which the program terminates with the return 0 statement.
Check out this amazing course to become the best version of the C programmer you can be.

Subtraction Assignment Operator In C (-=)

The subtraction assignment operator is used to subtract a given value, mentioned on the right side of the operator, from the initial value of the variable on the left (i.e., the left operand). It then assigns the result back to the original variable on the left. Alternatively, we can also subtract the value of a second variable from the initial variable.

The value of x is 7

In the example C program - 

  • Inside the main() function , we declare an integer variable x and assign the value 10 to it using the simple operator.
  • We then use the subtraction assignment operator (-=) to subtract a value 3 from the initial value of x. The result is assigned back to x using this operator. 
  • Here, the expression x -= 3 is equivalent to x = x - 3, which means we subtract 3 from 5 (i.e., the value of x), and the value of x is updated with the result.
  • After that, we use the printf() function to display the updated value of the variable x to the console.

Multiplication Assignment Operator In C (*=)

The multiplication assignment operator in C (*=) first multiplies a given value (on the right) with the initial/ current value of the variable on the left (left operand). Then, it assigns the result back to the variable on the left. Alternatively, it can also be used to multiply the initial value of a variable by the value of another variable in the program. 

The value of x is 20

In the example C code -

  • Inside the main() function , we declare an integer variable named x and initialize it with the value 5 using the simple assignment operator.
  • Next, we use the multiplication assignment operator to multiply the initial value of x by 4 and then assign the result back to variable x.
  • That is, the expression x *= 4 implies x = x * 4, which multiplies the value of x by 4 and stores the result back into x.
  • Using the printf() function , we display the updated value of x and a string literal message.

Division Assignment Operator In C (/=)

The division assignment operator (/=) first divides the current/ initial value of the variable mentioned on its left side by the value given on its right. Then, the revised value/ result is assigned back to the variable on the left. Alternatively, we can also use this operator to divide the value of one variable by another variable and then update the value of one. 

Let's take a look at an example that illustrates the use of this compound assignment operator in C code.

In the sample C program -

  • We declare an integer variable  x inside the main() function and initialize it with the value 10. 
  • Next, we use the  division assignment operator (/=)  to divide the value of x by 2 and assign the result of this operation back to variable x.  
  • Here, the expression x /= 2 can be expanded as x = x / 2, which ultimately divides the value of x by 2 and stores the result back into x.
  • Finally, we display this output using the printf() function with a formatted string message.

Modulo Assignment Operator In C (%=)

The modulo assignment operator (%=) is a combination of the modulo arithmetic operator and assignment operator. It divides the value of the left-side operand by the right-side operand to calculate the remainder of this division. Then, it assigns the resulting remainder back to the variable on the left. Note that the value on the right can also be the value of another variable in the program. 

Look at the example below to better understand how to apply this compound assignment operator in C programs.

The value of x is 3

In the sample C code -

  • In the main() function , we declare a variable, x, of integer data type and initialize it with the value of 14. 
  • Then, we use the modulo assignment operator ( %=) to calculate the remainder from the division of the variable on the left side by a specified value and assign the result back to that variable.
  • In this example, we divide the value of x by 4 and reassign the remainder to it.
  • That is, x %= 4 is equivalent to x = x % 4, which ultimately calculates the remainder when x is divided by 4 and stores the result back into x.
  • This reassigned/ updated value of x is displayed on the console using the printf() function with a formatted string and %d format specifier. 

We have already discussed the most commonly used assignment operators in C programing language. There are a few other compound assignment operators that you must know about. These are the bitwise assignment operators, which first conduct the respective bitwise operation on the operands and then assign the result back to the left operand in the expression.

There are three types of bitwise assignment operators in C, which we have discussed ahead. 

Bitwise AND Assignment Operator In C (&=)

The bitwise AND assignment operator (&=) performs a bitwise AND operation between the current value of the variable on the left (i.e., the left operand) and the value on the right (i.e., the right operand) and then assigns the result to the variable on the left. The value on the right can also be another variable.

The assignment operator example below showcases how this compound operator works.

The value of x is 4

In the C program sample , we define the main() function (which serves as the entry point) after including the <stdio.h> file.

  • We then declare an integer variable called  x and use the basic assignment operator to initialize it with the value 12. 
  • Next, we use the  bitwise AND assignment operator (&=) to perform a bitwise AND operation between the value of the variable on the left side and a specified value, and then we assign the result back to that variable.
  • The equation  x &= 5 , which implies x = x & 5, performs a bitwise AND between the value of x and 5 and stores the result back into x.
  • We display the result of this operation to the console with the printf() function , a formatted string, and the %d specifier.
Hone your coding skills with the 100-Day Coding Sprint at Unstop and claim bragging rights now!

Bitwise OR Assignment Operator In C (|=)

The bitwise OR assignment operator (|=) performs a bitwise OR operation between the current value of the variable on the left and the value on the right and then assigns the result to the variable on the left. The example below showcases the use this compound assingment operator in C.

Output Window :

The value of x is 13

Code Explanation:

In the C code sample -

  • In the main() function , we declare an integer variable x  and initialize it with the value 12. This reserves a memory location to store an integer value.
  • Next, using the bitwise OR assignment operator , we perform a bitwise OR operation between the value of the variable on the left side (i.e., 12) and a specified value (i.e., 5) and assign the result back to the left-side variable, i.e., x.
  • The expression x |= 5 leads to the evaluation of x = x | 5, which performs a bitwise OR between the value of x and 5 and stores the result back into x.
  • Then, we use the printf() function to display the result/ updated value of left operand x to the console. We also use a formatted string and the %d specifier inside printf().

Left Shift Assignment Operator In C (<<=)

The bitwise left shift assignment operator (<<=) first shifts the bits of the current value of the variable on the left (i.e., left-hand operand) by the number of positions specified by the value on the right. Then, it assigns the result of this operation back to the variable on the left.

The value of x is 40

In this assignment operator example, we first include the essential header file and then initiate the main() function .

  • Inside main(), we declare an integer variable x and initialize it with the value 10. This step reserves a memory location to store an integer value and assigns the value 10 to x.
  • Next, we apply the left shift assignment operator (<<=) on this variable to perform a left shift operation on its value by a specified number of bits (here, 2) and assign the result back to that variable.
  • The equation x <<= 2 expands to x = x << 2, which performs a left shift on the value of x by 2 bits and stores the result back into x.
  • We print the new value of the left-hand side variable x to the console using the printf() function .  

Right Shift Assignment Operator In C (>>=)

The bitwise right shift assignment operator (>>=) begins by shifting the bits of the initial value of the variable on the left (i.e., left-hand operand) by the number of positions specified by the right-hand value and then assigns the result to the variable on the left.

In the assignment operator C example-

  • Inside the main() function (which serves as the entry point for program execution), we declare an integer variable x and initialize it with the value 10. 
  • Next, we employ the right shift assignment operator ( >>=) to shift the bits of the value on the left side (i.e., 10, the value of x) by a specified number of positions (i.e., 1) to the right. Then, assign the result back to the left-hand side variable.
  • The equation x >>= 1 is equivalent to x = x >> 1, which performs a right shift operation on the value of x by 1 position and stores the result back into x.
  • We publish this updated value of the variable x to the console using a formatted string and %d specifier inside the printf() function . 

Also Read: Shift Operators In C | The Ultimate Guide With Code Examples

Precedence & Associativity Of Assignment Operators In C

In C, operators have a defined precedence that determines the order in which expressions are evaluated when multiple operators are present. Precedence helps in resolving which operator should be applied or evaluated first in an expression. Associativity, on the other hand, defines the direction in which operators of the same precedence level are processed.

Precedence of Assignment Operators In C

All assignment operators in C (=, +=, -=, *=, /=, %=, etc.) have lower precedence compared to most other operators, such as arithmetic operators (+, -, *, /), relational operators (<, >, <=, >=) , and logical operators (&&, ||). This means that in an expression where assignment operators are used alongside other operators, the other operators are evaluated first before the assignment takes place.

In other words, the other operators take precedence over assignment operators when evaluating expressions in C programs. For example:

int a = 5; int b = 10; int c = a + b * 2;

In this expression, the multiplication operator (*) has higher precedence than the addition operator (+), and both have higher precedence than the assignment (=). Therefore, b * 2 is evaluated first, then the result is added to a, and finally, the sum is assigned to c.

Assignment Operator Associativity In C

Assignment operators in C have right-to-left associativity. This means that when multiple assignment operators are used in a single expression, the expression is evaluated from right to left.

int a, b, c; a = b = c = 10;

In this case, the value 10 is first assigned to c, then the value of c is assigned to b, and finally, the value of b is assigned to a. Since every assignment operator associates from right to left, this evaluation happens from right to left.

This associativity of assignment operators in C is particularly important to understand when chaining assignments or combining them with other operations.

Here is a table listing all the assignment operators in C programming with a brief description and example.

Both the relational and the assignment operators serve different purposes and have distinct characteristics in programming languages like C. Assignment operators deal with assigning values to operands/ variables. In contrast, relational operators focus on making comparisons between operands, thus enabling logical decision-making in control flow statements .

Here are some points highlighting the differences between relational and assignment operators in C:

Purpose | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators are used to assign values or expressions to variables. They perform the task of storing a value in a variable.

Relational Operators: Relational operators are used to compare values or expressions. They determine the relationship between two operands and produce a Boolean result (true or false) based on the comparison.

Syntax | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators are typically represented by a single character followed by an equal sign, such as (=), (+=), (-=), (*=), etc.

Relational Operators: Relational operators are represented by two or more characters, including equality operators (==, !=), other relational operators (<, >, <=, >=), etc.

Operation | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators in C perform the task of assigning a value or expression to a variable. They modify the value of the left-hand side variable based on the right-hand side value or expression.

Relational Operators: Relational operators compare the values of two operands and return a Boolean result indicating whether the comparison is true or false.

Result Type | Relational Vs. Assignment Operators In C

Assignment Operators: In case of the simple assignment operator, the result is not a value in itself but the assignment of value to the variable on the left-hand side of the operator. But in case of compound assingment operators in C the result is the value assigned to the variable on the left-hand side of the operator, which can be used in further expressions or assignments.

Relational Operators: The result of a relational operator is a Boolean value, either true or false, indicating the outcome of the comparison.

Usage | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators in C are primarily used for variable assignments, updating variables, and performing calculations involving the current value of a variable.

Relational Operators: Relational operators are used in conditional statements (if, while, for) and expressions that require comparison between variables or values.

Precedence | Relational Vs. Assignment Operators In C

Assignment Operators: Assignment operators in C have lower precedence compared to most other operators, which means they are evaluated after most other operations in an expression.

Relational Operators : Relational operators have higher precedence than assignment operators, allowing them to be evaluated before assignment operations.

Looking for mentors? Find the perfect mentor for select experienced coding & software experts here .

The assignment operator in C is one of the most important and commonly used operators in the language. It assigns values to variables, which is essential for writing any program. Compound assignment operators can be used to make code more concise and efficient. These assignment operators are fundamental operators that allow programmers to store and manipulate data effectively throughout their programs. It is important to understand the different types of assignment operators and how to use them correctly to write and run efficient and effective C code .

Also Read: 100+ Top C Interview Questions With Answers (2024)

Q. What is an assignment operator in C?

An assignment operator is an operator in the C language that assigns a value to a variable. This could be a direct value (as in initialization) or the value that results from a compound operation (like the result of addition, subtraction, multiplication, etc. operations).

The most commonly used assignment operator is the equal sign (=), the basic assignment operator. It assigns the value on the right-hand side to the variable on the left-hand side of the operator. 

Q. What is the difference between a simple assignment operator and a compound assignment operator?

The simple assignment operator, the equal-to operator (=), assigns a single value to a variable. On the other hand, a compound assignment operator in C (binary operator) performs a binary operation (such as addition, subtraction, multiplication, etc.) between the current value of a variable and another value or expression and then assigns the result to the variable. Its examples are- addition assignment (+=), multiplication assignment (*=), modulo assignment (%=), etc. 

Q. What happens if I use an assignment operator with incompatible types in C?

Using an assignment operator with incompatible types in the C programming language results in a compilation error . C is a statically typed language, meaning variables must be explicitly declared with their data types, and the compiler enforces strict type-checking during the compilation process . If an attempt is made to assign a value of one type to a variable of a different, incompatible type without an explicit type conversion , the compiler will raise an error.

For example, trying to assign a character ('A') to an integer variable declared as int will lead to a compilation error. This mechanism helps catch potential type-related errors early in the development process, ensuring that the program adheres to the specified type rules and preventing unexpected behavior during runtime. 

Q. Can I chain multiple assignment operators together in C language?

In the C programming language, it is possible to chain multiple assignment operators together in a single statement. That is, you can assign the same value to multiple variables in one line by chaining assignments. For example, the following code is valid in C:

int a, b, c; a = b = c = 5; // This is not allowed in C

This assigns the value 5 to all three variables (a, b, and c). The expression is evaluated from right to left due to the right-associative nature of the assignment operators in C.

Q. Are there any precedence rules for assignment operators?

Yes, there are precedence rules for assignment operators in C language. The assignment operator (=) has a lower precedence than most other operators. This means that when an expression contains both assignment and other operators, the assignment will be performed after the evaluation of the other operators.

For example:

int a, b, c; a = b = c = 5 + 3 * 2;

In this example, the multiplication (*) has higher precedence than the assignment (=). So, 3 * 2 is evaluated first, resulting in 6. Then, 5 + 6 is evaluated, resulting in 11. Finally, the assignment is performed from right to left, so c, b, and a will all be assigned the value 11.

It's important to note that associativity also plays a role. The assignment operators in C are right-associative, which means that when multiple assignments appear in a row, they are evaluated from right to left. In the example above, c = 5 + 3 * 2 is evaluated first, followed by b = c, and finally a = b.

Now that you know all about assignment operators in C, check out some other interesting topics:

  • Increment And Decrement Operators In C With Precedence (+Examples)
  • Keywords In C Language | Properties, Usage, Examples & Detailed Explanation
  • Pointers In C | Ultimate Guide With Easy Explanations (+Examples)
  • Ternary (Conditional) Operator In C Explained With Code Examples
  • For Loop In C | Structure, Working & Variations With Code Examples

I am an economics graduate using my qualifications and life skills to observe & absorb what life has to offer. A strong believer in 'Don't die before you are dead' philosophy, at Unstop I am producing content that resonates and enables you to be #Unstoppable. When I don't have to be presentable for the job, I'd be elbow deep in paint/ pencil residue, immersed in a good read or socializing in the flesh.

No comments Image

Login to continue reading

And access exclusive content, personalized recommendations, and career-boosting opportunities.

NEST – Nurturing Excellence, Strengthening Talent

to our newsletter

Blogs you need to hog!

Porter's 5 Forces: Comprehensive Guide To This Analytical Tool

Porter's 5 Forces: Comprehensive Guide To This Analytical Tool

How To Organize Hackathons & Coding Competitions

How To Organize Hackathons & Coding Competitions

Social Media: Boon Or Bane? Delving Deep Into The Debate

Social Media: Boon Or Bane? Delving Deep Into The Debate

What Is Coding | Role, Working, How To Learn & More Simplified

What Is Coding | Role, Working, How To Learn & More Simplified

Home » Learn C Programming from Scratch » C Assignment Operators

C Assignment Operators

Summary : in this tutorial, you’ll learn about the C assignment operators and how to use them effectively.

Introduction to the C assignment operators

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:

  • A simple assignment operator assigns the value of the left operand to the right operand.
  • A compound assignment operator performs the operation specified by the additional operator and then assigns the result to the left operand.

C Programming Tutorial

  • Assignment Operator in C

Last updated on July 27, 2020

We have already used the assignment operator ( = ) several times before. Let's discuss it here in detail. The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows:

The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

The precedence of the assignment operator is lower than all the operators we have discussed so far and it associates from right to left.

We can also assign the same value to multiple variables at once.

here x , y and z are initialized to 100 .

Since the associativity of the assignment operator ( = ) is from right to left. The above expression is equivalent to the following:

Note that expressions like:

are called assignment expression. If we put a semicolon( ; ) at the end of the expression like this:

then the assignment expression becomes assignment statement.

Compound Assignment Operator #

Assignment operations that use the old value of a variable to compute its new value are called Compound Assignment.

Consider the following two statements:

Here the second statement adds 5 to the existing value of x . This value is then assigned back to x . Now, the new value of x is 105 .

To handle such operations more succinctly, C provides a special operator called Compound Assignment operator.

The general format of compound assignment operator is as follows:

where op can be any of the arithmetic operators ( + , - , * , / , % ). The above statement is functionally equivalent to the following:

Note : In addition to arithmetic operators, op can also be >> (right shift), << (left shift), | (Bitwise OR), & (Bitwise AND), ^ (Bitwise XOR). We haven't discussed these operators yet.

After evaluating the expression, the op operator is then applied to the result of the expression and the current value of the variable (on the RHS). The result of this operation is then assigned back to the variable (on the LHS). Let's take some examples: The statement:

is equivalent to x = x + 5; or x = x + (5); .

Similarly, the statement:

is equivalent to x = x * 2; or x = x * (2); .

Since, expression on the right side of op operator is evaluated first, the statement:

is equivalent to x = x * (y + 1) .

The precedence of compound assignment operators are same and they associate from right to left (see the precedence table ).

The following table lists some Compound assignment operators:

The following program demonstrates Compound assignment operators in action:

Expected Output:

Load Comments

  • Intro to C Programming
  • Installing Code Blocks
  • Creating and Running The First C Program
  • Basic Elements of a C Program
  • Keywords and Identifiers
  • Data Types in C
  • Constants in C
  • Variables in C
  • Input and Output in C
  • Formatted Input and Output in C
  • Arithmetic Operators in C
  • Operator Precedence and Associativity in C
  • Increment and Decrement Operators in C
  • Relational Operators in C
  • Logical Operators in C
  • Conditional Operator, Comma operator and sizeof() operator in C
  • Implicit Type Conversion in C
  • Explicit Type Conversion in C
  • if-else statements in C
  • The while loop in C
  • The do while loop in C
  • The for loop in C
  • The Infinite Loop in C
  • The break and continue statement in C
  • The Switch statement in C
  • Function basics in C
  • The return statement in C
  • Actual and Formal arguments in C
  • Local, Global and Static variables in C
  • Recursive Function in C
  • One dimensional Array in C
  • One Dimensional Array and Function in C
  • Two Dimensional Array in C
  • Pointer Basics in C
  • Pointer Arithmetic in C
  • Pointers and 1-D arrays
  • Pointers and 2-D arrays
  • Call by Value and Call by Reference in C
  • Returning more than one value from function in C
  • Returning a Pointer from a Function in C
  • Passing 1-D Array to a Function in C
  • Passing 2-D Array to a Function in C
  • Array of Pointers in C
  • Void Pointers in C
  • The malloc() Function in C
  • The calloc() Function in C
  • The realloc() Function in C
  • String Basics in C
  • The strlen() Function in C
  • The strcmp() Function in C
  • The strcpy() Function in C
  • The strcat() Function in C
  • Character Array and Character Pointer in C
  • Array of Strings in C
  • Array of Pointers to Strings in C
  • The sprintf() Function in C
  • The sscanf() Function in C
  • Structure Basics in C
  • Array of Structures in C
  • Array as Member of Structure in C
  • Nested Structures in C
  • Pointer to a Structure in C
  • Pointers as Structure Member in C
  • Structures and Functions in C
  • Union Basics in C
  • typedef statement in C
  • Basics of File Handling in C
  • fputc() Function in C
  • fgetc() Function in C
  • fputs() Function in C
  • fgets() Function in C
  • fprintf() Function in C
  • fscanf() Function in C
  • fwrite() Function in C
  • fread() Function in C

Recent Posts

  • Machine Learning Experts You Should Be Following Online
  • 4 Ways to Prepare for the AP Computer Science A Exam
  • Finance Assignment Online Help for the Busy and Tired Students: Get Help from Experts
  • Top 9 Machine Learning Algorithms for Data Scientists
  • Data Science Learning Path or Steps to become a data scientist Final
  • Enable Edit Button in Shutter In Linux Mint 19 and Ubuntu 18.04
  • Python 3 time module
  • Pygments Tutorial
  • How to use Virtualenv?
  • Installing MySQL (Windows, Linux and Mac)
  • What is if __name__ == '__main__' in Python ?
  • Installing GoAccess (A Real-time web log analyzer)
  • Installing Isso

IMAGES

  1. Operators in C

    when is assignment operator called in c

  2. Assignment operator

    when is assignment operator called in c

  3. Explain Different Type of Arithmetic Operators in C

    when is assignment operator called in c

  4. What is assignment operator in C with example?

    when is assignment operator called in c

  5. Assignment Operators In C Explained With Proper Code Examples // Unstop

    when is assignment operator called in c

  6. Operators In C Logicmojo

    when is assignment operator called in c

VIDEO

  1. Augmented assignment operators in C

  2. ASSIGNMENT OPERATOR IN C

  3. Assignment Operator in C Programming

  4. Operators in C language

  5. C++ Assignment operator using quincy 2005

  6. 8. Conditional, Equality and Assignment operators in C

COMMENTS

  1. Assignment Operators in C - GeeksforGeeks

    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 ...

  2. C Programming Assignment Operators - ScholarHat

    What is an Assignment Operator in C? Assignment Operators in C are used to assign values to the variables. They come under the category of binary operators as they require two operands to operate upon. The left side operand is called a variable and the right side operand is the value.

  3. Operators in C - GeeksforGeeks

    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 ...

  4. C Programming: Assignment Operators with Examples - w3resource

    In C programming, assignment operators are used to assign values to variables. The simple assignment operator is =. C also supports shorthand assignment operators that combine an operation with assignment, making the code more concise.

  5. C Operators - W3Schools

    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:

  6. Assignment and shorthand assignment operator in C - Codeforwin

    Assignment operator is used to assign value to a variable (memory location). There is a single assignment operator = in C. It evaluates expression on right side of = symbol and assigns evaluated value to left side the variable.

  7. Assignment Operators In C Explained With Proper Code ... - Unstop

    In simple terms, assignment operators in C programming are used to assign values to variables. This includes assigning initial value as well as updating the value of a variable with a new value based on a certain operation or calculation.

  8. C Assignment Operators

    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.

  9. Assignment Operators in C with Examples - BeginnersBook

    The /= assignment operator is a combination of / and = operators. It divides the left side operand by the right side operand and assign the quotient value of this division to the left side variable. For example, x /= y; is equivalent to x = x / y; .

  10. Assignment Operator in C - C Programming Tutorial - OverIQ.com

    The general format of compound assignment operator is as follows: variable op = expression where op can be any of the arithmetic operators ( + , - , * , / , % ).