Operators in C

Operators in C are used to perform some operations on variables and constant values.
See the example below

#include <stdio.h>
int main(){
    int a = 4, b = 5, c;
    c = a + b;
    return 0;
}

In the above example, we are using the addition operator(+) to add the values of a and b and store the result in variable c.

In C, operators are divided into the following categories:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • size of operator

Arithmetic operators

Arithmetic operators are used to performing mathematical operations on variables or values. Given below is a table of all the arithmetic operators in C.

Operator Name Description Example
+ Addition Adds together two values a + b
Subtraction Subtracts one value from another a – b
* Multiplication Multiplies two values a * b
/ Division Divides one value by another a / b
% Modulus Returns the remainder after division a % b
++ Increment Increases the value of a variable by 1 a++
Decrement Decreases the value of a variable by 1 a–

Let us see an example of an arithmetic operator:

#include <stdio.h>
int main(){
    int a = 1;
    int b = 4;

    int c = a + b;
    printf("%d", c);
    return 0;
}

Output: 5

Assignment Operators

Assignment operators are used to assigning values to variables. The most common assignment operator is =.

For example.

#include <stdio.h>
int main(){
    int a;
    a = 1;
    return 0;
}

The variable ‘a’ is now containing the value 1.

Addition Assignment Operator: This operator add the value to the pre stored value in a variable.

For example:

#include <stdio.h>
int main(){
    int a = 4;
    a += 1;
    printf("%d", a);
    return 0;
}

Output: 5

Similarly the operators -=, *=, /=, and %= works for subtracting multiplying, or dividing the new value to the pre stored value.

Comparison Operators

To compare two or more values, we use comparison operators. The result of comparison is either true(1) or false(0).

For example

#include <stdio.h>
    int main(){
    int a = 4;
    int b = 2;

    printf("%d", a > b);
    return 0;
}

Output: 1

Let us see different comparison operators.

Double equal to(==)

This operator is used to check whether the values are equal.

For example:

#include <stdio.h>
int main(){
    int a = 1; 
    int b = 2;
    printf("%d", a == b);
    return 0;
}

Output: 0

Triple equal to(===)

This operator is also used to check the equality of two or more values, however it checks in a more strict format. What this means is that it checks for the datatype also.

For example:

#include <stdio.h>
int main(){
    int a = 4;
    char b = '4';

    printf("%d", a===b);
    return 0;
}

Output: 0

Here the values of a and b are the same but their datatypes are different. Therefore the output is false(0);

Not equal to (!=)

This operator is used to check if two values are not equal to each other.

For example:

#include <stdio.h>
int main(){

    int a = 4;
    int b = 4;

    printf("%d", a!=b);
    return 0;
}

Output: 0

Greater than(>)

This operator is used to check if a value is greater than the other.

For example:

#include <stdio.h>
    int main(){
    int a = 4;
    int b = 5;

    printf("%", b > a);
    return 0;
}

Output: 1

Less than(<)

This operator is used to check if a value is lesser than the other.

For example:

#include <stdio.h>
int main(){
    int a = 4;
    int b = 5;

    printf("%", b < a);
    return 0;
}

Output: 0

Greater than or equal to (>=)

This operator is used to check if one value is either greater than or equal to another value.

For example:

#include <stdio.h>
int main(){

    int a = 4;
    int b = 4;

    printf("%", b >= a);
    return 0;
}

Output: 1

Lesser than or equal to(<=)

This operator is used to check if one value is either less than or equal to another value.

For example:

#include <stdio.h>
int main(){
    int a = 4;
    int b = 5;

    printf("%", b <= a);
    return 0;
}

Output: 0

Logical Operators

The logical operators are used to check multiple comparison statements.

There are three logical operators in C. They are &&, || and !.

Logical and(&&)

If the returned value of all the comparison statements is true, it will return true, otherwise return false.

For example:

#include <stdio.h>
int main(){
    int a = 7;
    int b = 8;
    int x = 4;
    int y = 5;

    printf("%d", (a < b) && (x > y));
    return 0;
}

Output: 0

In the above example, the statement a < b will return 1, since 7 is less than 8, but the statement x > y will return 0, since 4 is not greater than 5. And, we have used the && operator between these two statements. Therefore, the overall output will be 0.

Logical or(||)

This operator will return true if the returned value of any one of the comparison statements is true, otherwise it return false.

For example:

#include <stdio.h>
int main(){
    int a = 7;
    int b = 8;
    int x = 4;
    int y = 5;

    printf("%d", (a < b) && (x > y));

    return 0;
}

Output: 1

Logical not(!)

The logical not operator reverse the output of comparing individual comparison statements.

For example:

#include <stdio.h>
int main(){

    int a = 7;
    int b = 8;

    printf("%d", !(a < b));
    return 0;
}

Output: 0

sizeof operator

This operator is used to find out the memory size(in bytes) of a datatype.

For example:

#include <stdio.h>
int main(){
    int age;
    char section;

    printf("%lu\n", sizeof(age));
    printf("%lu\n", sizeof(section));
    return 0;
}

Output:

4

1