C Language Operators

The C language provides a rich set of operators for performing various operations.1. Arithmetic Operators

Operator Description Example
+ Addition a + b
Subtraction a – b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b
++ Increment a++ or ++a
Decrement a– or –a
#include <stdio.h>
int main(int argc, char *argv[]) {    int a = 11;    int b = 3;    int c = 2;    int e = 0;    int f = 4;    int g = 4;
    printf("a + b = %d\n", (a + b));    printf("a - b = %d\n", (a - b));    printf("a * b = %d\n", (a * b));    printf("a / b = %d\n", (a / b));    printf("a %% b = %d\n", (a % b));
    // c++ means use first, then c increments by 1    // here c's original value is 2, the meaning of use first is to set e=c,    // then c increments by 1, so the first print outputs    // c=3, e=2    // this complicated usage is generally avoided, usually only needed in exams/interviews    e = c++;
    printf("1. c=%d, e=%d\n", c, e);
    // now c has become 3    // ++c means c increments by 1 first, then use    // so here ++c, c becomes 4, then e=c, e also becomes 4.    // this complicated usage is generally avoided, usually only needed in exams/interviews    e = ++c;
    printf("2. c=%d, e=%d\n", c, e);
    // to avoid confusion between c++ and ++c, it can be used separately as below    // through f++, ++g, the following printf outputs are all 5, without considering whether ++ is in front or behind    // generally f++ is more common.    f++;    ++g;    printf("1. f=%d, g=%d\n", f, g);
    // -- and ++ have the same concept    f--;    --g;    printf("2. f=%d, g=%d\n", f, g);    return 0;}

Output:

a + b = 14

a – b = 8

a * b = 33

a / b = 3

a % b = 2

1. c=3, e=2

2. c=4, e=4

1. f=5, g=5

2. f=4, g=4

2. Relational OperatorsGenerally used for comparisons

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
#include <stdio.h>
int main(int argc, char *argv[]) {    int a = 10;    int b = 20;
    if (a == b) {        printf("a==b is true\n");    } else {        printf("a==b is false\n");    }
    if (a != b) {        printf("a!=b is true\n");    } else {        printf("a!=b is false\n");    }
    if (a > b) {        printf("a>b is true\n");    } else {        printf("a>b is false\n");    }
    if (a < b) {        printf("a<b is true\n");    } else {        printf("a<b is false\n");    }
    if (a >= b) {        printf("a>=b is true\n");    } else {        printf("a>=b is false\n");    }
    if (a <= b) {        printf("a<=b is true\n");    } else {        printf("a<=b is false\n");    }    return 0;}

Output:

a==b is false

a!=b is true

a>b is false

a<b is true

a>=b is false

a<=b is true

3. Logical Operators

Operator Description Example
&& Logical AND a && b
|| Logical OR a || b
! Logical NOT !a
#include <stdio.h>
int main(int argc, char *argv[]) {    int a = 5;    int b = 6;        // can also follow with &amp;&amp; (b > 0), can have multiple conditions    if ( (a > 0) &amp;&amp; (b > a) ) {        printf("If a is greater than 0 and b is greater than a, both conditions must be true, then it is true\n");    }        // can also follow with || (b > 0), can have multiple conditions    if ( (a > 2) || (a > b)) {        printf("If a is greater than 2 or a is greater than b, as long as one condition is true, then it is true\n");    }        if (!(a<0)) {        printf("If a is less than 0 is false, then the opposite of false is true\n");    }    return 0;}

Output:

If a is greater than 0 and b is greater than a, both conditions must be true, then it is true

If a is greater than 2 or a is greater than b, as long as one condition is true, then it is true

If a is less than 0 is false, then the opposite of false is true

4. Bitwise OperatorsGenerally used less frequently

Operator Description Example
& Bitwise AND a & b
| Bitwise OR a | b
^ Bitwise XOR a ^ b
~ Bitwise NOT ~b
<< Left Shift a << 2
>> Right Shift a >> 2
#include <stdio.h>
int main() {    unsigned int a = 5;    // Binary: 0101     unsigned int b = 3;    // Binary: 0011
    printf("a &b = %u\n", a & b);  // 0101 & 0011 = 0001 (1)    printf("a | b = %u\n", a | b);  // 0101 | 0011 = 0111 (7)    printf("a ^ b = %u\n", a ^ b);  // 0101 ^ 0011 = 0110 (6)    printf("~a = 0x%x\n", ~a);        // 0x00000005 inverted 0xfffffffa    printf("a << 1 = %u\n", a << 1); // 0101 << 1 = 1010 (10)    printf("a >> 1 = %u\n", a >> 1); // 0101 >> 1 = 0010 (2)
    return 0;}

Output:

a & b = 1

a | b = 7

a ^ b = 6

~a = 0xfffffffa

a << 1 = 10

a >> 1 = 2

5. Assignment Operators

Operator Description Example Equivalent to
= Simple assignment a = b
+= Add and assign a += b a = a + b
-= Subtract and assign a -= b a = a – b
*= Multiply and assign a *= b a = a * b
/= Divide and assign a /= b a = a / b
%= Modulus and assign a %= b a = a % b
#include <stdio.h>
int main(int argc, char *argv[]) {    int a = 10;
    a += 5;  // a = 15    printf("a+=5: %d\n", a);        a -= 3;  // a = 12    printf("a-=3: %d\n", a);        a *= 2;  // a = 24    printf("a*=2: %d\n", a);        a /= 4;  // a = 6    printf("a/=4: %d\n", a);        a %= 4;  // a = 2    printf("a%%=4: %d\n", a);        return 0;}

Output:

a+=5: 15

a-=3: 12

a*=2: 24

a/=4: 6

a%=4: 2

Leave a Comment