C Program Using Assignment Operators

Assignment Operator :

An Assignment operator is used to form an assignment expression, which assigns the value to an identifier. The most commonly used assignment operator is = . Assignment expressions that make use of this operator are written in the form<
Identifier=expression;
Example:
i=100;
j=200;

Other forms of assignment operators exist that are obtaining various operators such as +,-,*,/,% etc with the = sign.

 operatorEquationOutput
+=i=i+1i+=1
-=i=i-10i-=10
*=i=i*11i*=11
/=i=i/12i/=12
%=i=i%5i%=5

C – ASSIGNMENT OPERATORS EXAMPLE PROGRAM :

Output:

Value of a = 123

Value of b = 125

PrevNext

#include<stdio.h>

#include<conio.h>

intmain()

{

inta=123,b;

b=a+2;

clrscr();

printf(“Value ofa=%d\n”,a);

printf(“Value ofb=%d”,b);

getch();

return0;

}


In this tutorial we will learn about assignment operators in C programming language.

We use the assignment operators to assign the result of an expression to a variable.

In the following example we are assigning integer value 10 to a variable score of data type integer.

Shorthand Assignment Operators

Lets say we have an integer variable and we have initially assigned 10 to it. Then lets say we increase the value by 5 and assign the new value to it.

Another way to write the code is by using the shorthand assignment as shown below.

Following is a list of shorthand assignment operators.

Simple assignmentShorthand assignment
x = x + 1x += 1
x = x - 1x -= 1
x = x * (n + 1)x *= (n + 1)
x = x / (n + 1)x /= (n + 1)
x = x % yx %= y

In the following example we are taking a value x from user then we are taking a new value y from user. Then we are adding y to x and assigning the result to x. Finally we are printing the new value stored in variable x.

Output

Another way of adding y to x and re-assigning the new value to x is by using the shorthand assignment operator .

0 thoughts on “C Program Using Assignment Operators

Leave a Reply

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