Assignment Operators in C Programming Language
Notes:
Assignment Operators in C Programming Language:
Assignment Operator: is used to assign a value to variable or a constant.
= : Assignment Operator
Short Hand Assignment (SHA) Operators:
- allow us to write condensed or compact form assignment statements
AA (Arithmetic Assignment)
+=
-=
*=
/=
%=
BA (Bitwise Assignment)
&=
|=
^=
<<=
>>=
Example code:
#include <stdio.h>
int main()
{
const float PI = 3.142;
printf("%f\n",PI);
int a = 0;
printf("%d\n",a); // 0
a = 10;
printf("%d\n",a); // 10
a += 10; // a = a + 10;
printf("%d\n",a); // 20
a -= 10; // a = a - 10;
printf("%d\n",a); // 10
a *= 2; // a = a * 2
printf("%d\n",a); // 20
a /= 2; // a = a / 2
printf("%d\n",a); // 10
a %= 2; // a = a % 2
printf("%d\n",a); // 0
a += 10; // a = a + 10;
printf("%d\n",a); // 10
return 0;
}