top of page
Search

Fully Detailed Explanation of Different Operators of C Programming



An operator is a symbol that is used to perform specific mathematical operations or logical functions. C language provides different types of operators. Let us have a look at the following operators:

  1. Arithmetic Operators

  2. Relational Operators

  3. Assignment Operators

  4. Increment/Decrement Operators


Arithmetic Operators

These are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.

The following table shows all the arithmetic operators supported by the C language.

  • + Addition Adds two operands.

  • Subtraction Subtracts the second operand from the first.

  • * Multiplication Multiplies both operands.

  • / Division Divides numerator by denominator.

  • % Modulus Gives the remainder after an integer division.


Relational Operators

Relational operators are used to comparing the values of two variables in a C program.

The following table shows all the relational operators supported by C.

  • == Is equal to x==y; Checks if the values of two operands are equal or not. If yes, then the condition becomes true.

  • != Is not equal to x!=y; Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.

  • > Is greater than x>y; Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true.

  • < Is less than x<y; Checks if the value of left operand is less than the value of the right operand. If yes, then the condition becomes true.

  • >= Is greater than or equal to x>=y; Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.

  • <= Is less than or equal to x<=y; Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.


Logical Operators

The following table shows all the logical operators supported by C.

  • && Called Logical AND operator. If both the expressions evaluate to True, the result is True. If either expression is False, the result is False.

  • || Called Logical OR Operator. If anyone of the two expressions or both expression is True, then the result is True.

  • ! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.

Logical AND (&&)

false && false = False

false && true = False

true && false = False

true && true = True

Logical OR (||)

false || false = false

false || true = true

true || false = true

true || true = true

Logical NOT (!)

!false = True

!true = False


Assignment Operators

Assignment Operators are used to assigning values for the variables in C programs. The following table shows all the assignment operators supported by C.

  • = x = y; Simple assignment operator. Assigns values from right side operands to left side operand

  • += x += y; {meaning x = x + y} Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand.

  • -= x -= y; {meaning x = x - y} Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.

  • *= x *= y; {meaning x = x * y} Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.

  • /= x /= y; {meaning x = x / y} Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.

  • %= x %= y; {meaning x = x % y} Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.


Increment/Decrement Operators

C supports two unique operators: ++ and --.Both are unary operators

  • ++ Increment operator x++; Increment operator adds 1 to the value of the operand.

  • -- Decrement operator x--; Decrement operator subtracts 1from the value of the operand.

Note: Meaning needs only one operand.


E.g.: int m=5;

m++; {meaning m=m+1}

printf(“%d”, m);

Output: 6


E.g.: int m=5;

m--; {meaning m=m-1}

printf(“%d”, m);

Output: 4


Increment and decrement operators are further classified as prefix and postfix.

The difference between prefix and postfix is observable when the result is stored in a variable and then printed.

++var or --var are prefix operators. {Prefix operators do the operation first and then passes the value}

var ++ or var -- postfix operators. {Postfix operators passes the value first and then does the operation}


Example 1:

#include <stdio.h>

int main()

{

int x=10, y=10, ans;

ans=++x;

printf("Prefix answer:%d",ans);

ans=y++;

printf("\nPostfix answer:%d",ans);

printf("\nValue of x:%d",x);

printf("\nValue of y:%d",y);

return (0);

}

Output:

Prefix answer: 11

Postfix answer: 10

Value of x: 11

Value of y: 11


Example 2:

#include <stdio.h>

int main ()

{

int x=4, y=5, ans;

ans=++x + y++;

printf("%d",ans);

return (0);

}

Output: 11




12 views
bottom of page