Assignment Operator in C



 assignment-operator-in-c

Learn C - C tutorial - Assignment operator in c - C examples - C programs

Assignment Operator in C - Definition and Usage

  • In C-program the assignment operator is used for assigning a value to a variable.
  • The most common assignment operator is = (equals to).
  • The assignment operators tend to be classified into two categories such as,
    • Simple assignment operator.
    • Compound assignment operator.
C Assignment operator

Simple assignment Operator :

  • In C-Program the common assignment operator as equals to (=)..

Compound assignment Operator :

  • In C-Program the compound assignment operator works in left to right order format that is first the operation will be performed after that the output value will be assigned for the variable.
  • Some of the compound assignment operators are listed below,
Operator Example Same as
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

Sample - C Code

#include <stdio.h>
#include <conio.h>
void main()
{
    int a = 7, c;
    c = a;
    printf("c = %d \n", c);
    c += a; // c = c+a
    printf("c = %d \n", c);
    c -= a; // c = c-a
    printf("c = %d \n", c);
    c *= a; // c = c*a
    printf("c = %d \n", c);
    c /= a; // c = c/a
    printf("c = %d \n", c);
    c %= a; // c = c%a
    printf("c = %d \n", c);
    getch();
}

C Code - Explanation

code-explanation-assignment
  1. In this statement we declare and initialize the value for the variable “a= 7”and “c”.
  2. In this statement we are assigning the “a” variable value 7 to “c” Variable.
  3. In this statement we are performing an addition operation that c+a (7+7=14) and the value will be assigned for the variable “c”=14.
  4. In this statement we are performing the subtraction operation that is c-a(14-7=7) and the value will be assigned for the variable “c”=7.
  5. In this statement we are performing the Multiplication operation that is c*a(7*7=49) and the value will be assigned for the variable “c”=49.
  6. In this statement we are performing the division operation that c/a (49/7) and the value will be assigned for the variable “c”=7.
  7. In this statement we are performing the mod operation that c%a(7%7=0) and the value will be assigned for the variable “c”=0.

Output :

code-explanation-assignment-output
  1. Here in this output we have shown the assignment operation of variable “c” (c=7) where its output value of the variable “c” =7 will be displayed.
  2. Here in this output we have shown the addition operation of variable “c” & “a” (7+7=14) where its output value of the variable “c” =14 will be displayed.
  3. Here in this output we have shown the subtraction operation of variable “c” & “a” (14-7=7) where its output value of the variable “c” =7 will be displayed.
  4. Here in this output we have shown the subtraction operation of variable “c” & “a” (7*7=49) where its output value of the variable “c” =49 will be displayed.
  5. Here in this output we have shown the
  6. Here in this output we have shown the division operation of variable “c” & “a” (49/7=7) where its output value of the variable “c” =7 will be displayed.
  7. Here in this output we have shown the mod operation of variable “c” & “a” (7%7=0) where its output value of the variable “c” =0 will be displayed.

View More Quick Examples


Related Searches to c assignment operator