Skip to main content

Command Palette

Search for a command to run...

Operators in C

Understanding Operators in C with Simple Examples

Published
5 min read
Operators in C

What are Operators in C?

Operators are fundamental elements of C programming. They are special symbols that instruct the compiler to perform specific operations such as mathematical calculations, relational comparisons, logical decisions, bitwise manipulations, or conditional evaluations on data.
The values or variables on which these operators act are known as operands.

#include <stdio.h>

int main() {

// Expression for getting sum

int sum = 10 + 20;

printf("%d", sum);

return 0;

}

Here, + is an operator, and a and b are operands.

Unary, Binary and Ternary Operators

On the basis of the number of operands they work on, operators can be classified into three types :

  1. Unary Operators: Operators that work on single operand.
    Example: Increment( ++ ) , Decrement( -- )

  2. Binary Operators: Operators that work on two operands.
    Example: Addition ( + ), Subtraction( - ) , Multiplication ( * )

  3. Ternary Operators: Operators that work on three operands.
    Example: Conditional Operator( ? : )

Types of Operators in C

  1. Arithmetic Operators

    Arithmetic operators are used to perform mathematical calculations.

    | Operator | Description | | --- | --- | | + | Addition | | - | Subtraction | | * | Multiplication | | / | Division | | % | Modulus |

  2. Relational Operators

    Relational operators are used to compare two values.

    | Operator | Meaning | | --- | --- | | == | Equal to | | != | Not equal | | > | Greater than | | < | Less than | | >= | Greater than or equal | | <= | Less than or equal |

    3.Logical Operator

    Logical operators are used to combine conditions.

    | Symbol | Operator Name | Description | Syntax | | --- | --- | --- | --- | | && | Logical AND | Returns true if both operands are true. | a && b | | ` | | ` | Logical OR | | ! | Logical NOT | Returns true if the operand is false. | !a |

    4.Bitwise Operators

    Bitwise operators work on binary bits.

    | Symbol | Operator Name | Description | Syntax | | --- | --- | --- | --- | | & | Bitwise AND | Performs bit-by-bit AND operation and returns the result. | a & b | | ` | ` | Bitwise OR | Performs bit-by-bit OR operation and returns the result. | | ^ | Bitwise XOR | Performs bit-by-bit XOR operation and returns the result. | a ^ b | | ~ | Bitwise First Complement | Inverts all the bits (0 becomes 1, and 1 becomes 0). | ~a | | << | Bitwise Left Shift | Shifts bits to the left by a given number of positions; multiplies by 2 each shift. | a << b | | >> | Bitwise Right Shift | Shifts bits to the right by a given number of positions; divides by 2 each shift. | a >> b |

    5.Assignment Operators

    Assignment Operators are Assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.

    The assignment operator can be combine with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.

    | Symbol | Operator Name | Description | Syntax | | --- | --- | --- | --- | | = | Simple Assignment | Assigns the value of the right operand to the left operand. | a = b | | += | Plus and Assign | Adds the right operand to the left operand and assigns the result to the left operand. | a += b | | -= | Minus and Assign | Subtracts the right operand from the left operand and assigns the result to the left operand. | a -= b | | *= | Multiply and Assign | Multiplies the left operand by the right operand and assigns the result to the left operand. | a *= b | | /= | Divide and Assign | Divides the left operand by the right operand and assigns the result to the left operand. | a /= b | | %= | Modulus and Assign | Assigns the remainder of the division of the left operand by the right operand to the left operand. | a %= b | | &= | AND and Assign | Performs bitwise AND on operands and assigns the result to the left operand. | a &= b | | ` | \=` | OR and Assign | Performs bitwise OR on operands and assigns the result to the left operand. | | ^= | XOR and Assign | Performs bitwise XOR on operands and assigns the result to the left operand. | a ^= b | | >>= | Right Shift and Assign | Performs bitwise right shift and assigns the result to the left operand. | a >>= b | | <<= | Left Shift and Assign | Performs bitwise left shift and assigns the result to the left operand. | a <<= b |

Conditional Operator ( ? : )

The conditional operator is the only ternary operator in C. It is a conditional operator that we can use in place of if..else statements.

expression1 ? Expression2 : Expression3;

Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3.