C assignment operators are used to assigned the value of a variable or expression to a variable. The syntax of assignment operators is:
var = expression;
var = var;
Beside = operator, C programming language supports other short hand format which acts the same assignment operator with additional operator such as +=, -=, *=, /=, %=.
var +=expression; //means
var = var + expression;
Each assignment operator has a priority and they are evaluated from right to left based on its priority. Here is assignment operator and its priority: =, +=, -=, *=, /=, %=.
A simple C program to demonstrate assignment operators:
#include <stdio.h> /* a program demonstrates C assignment operator */ void main(){ int x = 10; /* demonstrate = operator */ int y = x; printf("y = %d\n",y); /* demonstrate += operator */ y += 10; printf("y += 10;y = %d\n",y); /* demonstrate -= operator */ y -=5; printf("y -=5;y = %d\n",y); /* demonstrate *= operator */ y *=4; printf("y *=4;y = %d\n",y); /* demonstrate /= operator */ y /=2; printf("y /=2;y = %d\n",y); }
Write the above program in your C compiler and run the code you will get below result.
Output:
y = 10
y += 10;y = 20
y -=5;y = 15
y *=4;y = 60
y /=2;y = 30
0 Comments