Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:
#include<stdio.h>
int main() {
int x = 10;
printf("%d", x);
return 0;
}
The addition assignment operator (+=) adds a value to a variable:
#include<stdio.h>
int main() {
int x = 10;
x += 5;
printf("%d", x);
return 0;
}
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
#include<stdio.h>
int main() {
int x = 5;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x += 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x -= 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x *= 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
float x = 5;
x /= 3;
printf("%f", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x %= 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x &= 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x |= 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x ^= 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x >>= 3;
printf("%d", x);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
x <<= 3;
printf("%d", x);
return 0;
}