Assignment operators in C | C Operators and Expressions

Assignment operators in C | C Operators and Expressions

C programming assignment operators; In this tutorial, you will learn about 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.

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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