Lesson 14 of 60 – Operators in C
23%

Operators in C

Operators are special symbols that tell the C compiler to perform an operation on one or more values. Operators are used for calculations, comparisons, assignments, logical decisions, and many other programming tasks.

Note: C provides several categories of operators, including arithmetic, relational, logical, assignment, increment/decrement, bitwise, conditional, and other specialized operators.

1. What is an Operator?

An operator is a symbol or combination of symbols that performs an operation on one or more operands.

int result = 10 + 20;

Here, + is an operator and 10 and 20 are operands.

2. What is an Operand?

An operand is a value, variable, or expression on which an operator performs an operation.

int sum = a + b;

Here, a and b are operands, while + is the operator.

3. Categories of Operators

Important operator categories in C include:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Increment and decrement operators
  • Bitwise operators
  • Conditional operator
  • Other operators such as sizeof and comma

4. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder

5. Addition Operator +

The + operator adds two operands.

int a = 10;
int b = 20;

int sum = a + b;

printf("%d", sum);

Output:

30

6. Subtraction Operator -

The - operator subtracts the second operand from the first.

int a = 30;
int b = 10;

int result = a - b;

printf("%d", result);

Output:

20

7. Multiplication Operator *

The * operator multiplies two operands.

int a = 10;
int b = 5;

int result = a * b;

printf("%d", result);

Output:

50

8. Division Operator /

The / operator performs division. When both operands are integers, integer division is performed.

int a = 20;
int b = 5;

int result = a / b;

printf("%d", result);

Output:

4

9. Remainder Operator %

The % operator gives the remainder of integer division.

int a = 17;
int b = 5;

int remainder = a % b;

printf("%d", remainder);

Output:

2

10. Relational Operators

Relational operators compare two values.

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

11. Equal to ==

The == operator checks whether two values are equal.

int a = 10;
int b = 10;

printf("%d", a == b);

The expression produces 1 when the values are equal and 0 when they are not.

12. Not Equal to !=

The != operator checks whether two values are different.

int a = 10;
int b = 20;

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

The result is 1 because the values are different.

13. Greater Than >

int age = 20;

printf("%d", age > 18);

The expression evaluates to 1 because 20 is greater than 18.

14. Less Than <

int age = 15;

printf("%d", age < 18);

The expression evaluates to 1 because 15 is less than 18.

15. Assignment Operator =

The = operator assigns the value of the expression on its right to the object on its left.

int age;

age = 20;

The variable age receives the value 20.

16. Compound Assignment Operators

C provides shorthand assignment operators.

Operator Example Meaning
+= a += 5; a = a + 5;
-= a -= 5; a = a - 5;
*= a *= 5; a = a * 5;
/= a /= 5; a = a / 5;
%= a %= 5; a = a % 5;

17. Increment Operator ++

The ++ operator increases an integer or compatible arithmetic object by one.

int count = 5;

count++;

printf("%d", count);

Output:

6

18. Decrement Operator --

The -- operator decreases an integer or compatible arithmetic object by one.

int count = 5;

count--;

printf("%d", count);

Output:

4

19. Logical AND Operator &&

The logical AND operator && produces true when both operands are nonzero.

int age = 20;
int marks = 80;

printf("%d", age >= 18 && marks >= 40);

Both conditions are true, so the result is 1.

20. Logical OR Operator ||

The logical OR operator || produces true when at least one operand is nonzero.

int age = 16;

printf("%d", age < 18 || age == 20);

At least one condition is true, so the result is 1.

21. Logical NOT Operator !

The logical NOT operator ! reverses the logical truth value of its operand.

int value = 0;

printf("%d", !value);

Output:

1

22. Bitwise Operators

Bitwise operators work on the individual bits of integer operands.

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift

23. Conditional Operator ?:

The conditional operator is a shorthand way to choose between two expressions based on a condition.

int age = 20;

char *result = (age >= 18) ? "Adult" : "Minor";

printf("%s", result);

If the condition is true, the first expression is selected; otherwise the second expression is selected.

24. sizeof Operator

The sizeof operator gives the size in bytes of a type or object.

int age;

printf("%zu", sizeof(age));

The result type of sizeof is size_t.

25. Address-of Operator &

The unary & operator obtains the address of an object.

int age = 20;

printf("%p", (void *)&age);

The address can be stored in a suitable pointer variable.

26. Pointer Dereference Operator *

The unary * operator can be used to access the value stored at the address held by a pointer.

int age = 20;
int *ptr = &age;

printf("%d", *ptr);

The expression *ptr accesses the value of age.

27. Comma Operator

The comma operator evaluates its left operand and then its right operand, with the result of the overall expression being the value of the right operand.

int a, b;

a = 10, b = 20;

After this statement, a is 10 and b is 20.

28. Operator Example

#include <stdio.h>

int main()
{
    int a = 10;
    int b = 20;

    printf("Addition = %d\n", a + b);
    printf("Subtraction = %d\n", b - a);
    printf("Multiplication = %d\n", a * b);
    printf("Division = %d\n", b / a);
    printf("Remainder = %d\n", b % a);

    return 0;
}

29. Common Operator Mistakes

  • Confusing = with ==.
  • Using & when logical AND && is intended.
  • Using | when logical OR || is intended.
  • Forgetting that integer division discards the fractional part.
  • Using the remainder operator with inappropriate operands.
  • Ignoring operator precedence in complex expressions.

30. Complete Operators Example

#include <stdio.h>

int main()
{
    int a = 10;
    int b = 5;

    int sum = a + b;
    int difference = a - b;
    int product = a * b;
    int quotient = a / b;
    int remainder = a % b;

    printf("Sum = %d\n", sum);
    printf("Difference = %d\n", difference);
    printf("Product = %d\n", product);
    printf("Quotient = %d\n", quotient);
    printf("Remainder = %d\n", remainder);

    if (a > b && b > 0)
    {
        printf("Both conditions are true");
    }

    return 0;
}

This program demonstrates arithmetic, relational, and logical operators together.

📌 Key Points

  • Operators perform operations on operands.
  • Arithmetic operators are used for mathematical calculations.
  • Relational operators compare values.
  • Assignment operators assign or update values.
  • ++ increases a value by one and -- decreases it by one.
  • Logical operators include &&, ||, and !.
  • Bitwise operators work on individual bits of integer operands.
  • The conditional operator uses the form condition ? value1 : value2.
  • sizeof determines the size in bytes of a type or object.
  • & can obtain an object's address and unary * can dereference a pointer.

🧠 Quick Quiz

Question: Which operator is used for addition in C?