C – Assignment Operators

Assignment operators in C:

• In C programs, values for the variables are assigned using assignment operators.
• For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
• There are 2 categories of assignment operators in C language. They are,
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )
 Operators Example/Description = sum = 10; 10 is assigned to variable sum += sum += 10; This is same as sum = sum + 10 -= sum -= 10; This is same as sum = sum – 10 *= sum *= 10; This is same as sum = sum * 10 /= sum /= 10; This is same as sum = sum / 10 %= sum %= 10; This is same as sum = sum % 10 &= sum&=10; This is same as sum = sum & 10 ^= sum ^= 10; This is same as sum = sum ^ 10

Example program for C assignment operators:

• In this program, values from 0 – 9 are summed up and total “45” is displayed as output.
• Assignment operators such as “=” and “+=” are used in this program to assign the values and to sum up the values.

Output:

 Total = 45

Continue on types of C operators:

Click on each operator name below for detailed description and example programs.

 Types of Operators Description Arithmetic_operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus Assignment_operators These are used to assign the values for the variables in C programs. Relational operators These operators are used to compare the value of two variables. Logical operators These operators are used to perform logical operations on the given two variables. Bit wise operators These operators are used to perform bit operations on given two variables. Conditional (ternary) operators Conditional operators return one value if condition is true and returns another value is condition is false. Increment/decrement operators These operators are used to either increase or decrease the value of the variable by one. Special operators &, *, sizeof( ) and ternary operators.