C Assignment Operators

C assignment operators are used to assigned the value of a variable or expression to a variable. The syntax of assignment operators is:
1.var = expression;
2.var = var;
Beside = operator, C programming language supports other short hand format which acts the same assignment operator with additional operator such as +=, -=, *=, /=, %=.
1.var +=expression; //means
2.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:
01.#include <stdio.h>
02./* a program demonstrates C assignment operator */
03.void main(){
04.int x = 10;
05. 
06./* demonstrate = operator */
07.int y = x;
08.printf("y = %d\n",y);
09. 
10./* demonstrate += operator */
11.y += 10;
12.printf("y += 10;y = %d\n",y);
13./* demonstrate -= operator */
14.y -=5;
15.printf("y -=5;y = %d\n",y);
16. 
17./* demonstrate *= operator */
18.y *=4;
19.printf("y *=4;y = %d\n",y);
20. 
21./* demonstrate /= operator */
22.y /=2;
23.printf("y /=2;y = %d\n",y);
24. 
25.}
Here is the output:
y = 10
y += 10;y = 20
y -=5;y = 15
y *=4;y = 60
y /=2;y = 30

source:cprogramlanguage.net

No comments:

Post a Comment