The Programming Gurus

Assignment operators in C


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;”
  • Other assignment operators in C language are given below.

Operators Example Explanation
Simple assignment operator = sum = 10 10 is assigned to variable sum
Compound assignment operators += 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.

Code:
# include <stdio.h>

int main()
 {
 int Total=0,i;
 for(i=0;i<10;i++)
 {
 Total+=i; // This is same as Total = Toatal+i
 }
 printf("Total = %d", Total);
 }
Output:
Total = 45

Continue on types of C operators:
Click on each operators name below for detail description and example programs.

S.no Types of Operators Description
1 Arithmetic operators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus
2 Assignment operators These are used to assign the values for the variables in C programs.
3 Relational operators These operators are used to compare the value of two variables.
4 Logical operators These operators are used to perform logical operations on the given two variables
5 Bit wise operators These operators are used to perform bit operations on given two variables.
6 Conditional (ternary) operators Conditional operators return one value if condition is true and returns another value is condition is false.
7 Increment/decrement operators These operators are used to either increase or decrease the value of the variable by one.
8 Special operators &, *, sizeof( ) and ternary operators.
0 Comments For "Assignment operators in C"

Back To Top