C Language Assignment Help Way 1 : Assignment Operator used to Assign Value
int main() {
int value;
value = 55;
return (0);
} In the above example, we have assigned 55 to a variable ‘value’.
Way 2 : Assignment Operator used To Type Cast. int value;
value = 55.450;
printf("%d",value); Assignment operator can type cast higher values to lower values. It can also cast lower values to higher values
Way 3 : Assignment Operator in If Statement if(value = 10)
printf("True");
else
printf("False"); It would be interesting to understand above program. Above Program will always execute True Condition because Assignment Operator is used inside if statement not comparison operator. Lets see what will happen in above example – Constant number 10 will be assigned to variable ‘value’. Variable ‘value’ will be treated as positive non-zero number, which is considered as true condition. if(value = 0) printf("True"); else printf("False"); In below case else block will be executed
Shorthand assignment operator Shorthand assignment operator is used express the syntax that are already available in shorter way Operator symbol
Name operator
of
the Exampl e
+=
Addition assignment
x += 4;
x = x + 4;
-=
Subtraction assignment
x -= 4;
x = x – 4;
*=
Multiplication assignment
x *= 4;
x = x * 4;
/=
Division assignment
x /= 4;
x = x / 4;
%=
Remainder assignment x %= 4;
Example : Shorthand assignment operator #include<stdio.h> int main() { int res; res = 11; res += 4; printf("\nResult of Ex1 = %d", res); res = 11; res -= 4; printf("\nResult of Ex2 = %d", res); res = 11; res *= 4; printf("\nResult of Ex3 = %d", res); res = 11; res /= 4; printf("\nResult of Ex4 = %d", res);
Equivalent construct
x = x % 4;
res = 11; res %= 4; printf("\nResult of Ex5 = %d", res); return 0; } Output : Result of Ex1 = 15 Result of Ex2 = 7 Result of Ex3 = 44 Result of Ex4 = 2 Result of Ex5 = 3