Assignment operators in C

C programming assignment operators; In this tutorial, i am going to show you assignment operators in c programming with examples.

C Assignment Operators

An assignment operator is used for assigning a value to a variable in c programming language.

Type of Assignment Operators in C

Note that:- There are 2 categories of assignment operators in C language. Shown below:

  • 1. Simple assignment operator ( Example: = )
  • 2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )
OperatorExampleSame as
=a = ba = b
+=a += ba = a+b
-=a -= ba = a-b
*=a *= ba = a*b
/=a /= ba = a/b
%=a %= ba = a%b

Example 1 – Assignment Operators

// Working of assignment operators
#include <stdio.h>
int main()
{
    int a = 5, c;
    c = a;      // c is 5
    printf("c = %d\n", c);
    c += a;     // c is 10 
    printf("c = %d\n", c);
    c -= a;     // c is 5
    printf("c = %d\n", c);
    c *= a;     // c is 25
    printf("c = %d\n", c);
    c /= a;     // c is 5
    printf("c = %d\n", c);
    c %= a;     // c = 0
    printf("c = %d\n", c);
    return 0;
}

Output

c = 5 
c = 10 
c = 5 
c = 25 
c = 5 
c = 0

Note that:- 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.

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *