CS Engineering Gyan

Operators in C

Once you know how to store data in variables, the next natural question is how to actually do something with that data. This is exactly the role operators play in C. Operators are special symbols that instruct the compiler to perform specific operations on one or more values, whether that means adding two numbers, comparing them, or combining logical conditions to make a decision.

C offers a particularly rich set of operators compared to many other languages, partly because of its close relationship with low-level hardware operations. While this variety can feel overwhelming at first glance, most operators follow patterns that quickly become intuitive once you see them used in a few practical examples.

In this tutorial, you will learn about arithmetic operators, relational operators, logical operators, assignment operators, bitwise operators, unary operators, and the special ternary operator, along with the order in which operators are evaluated when several appear together in a single expression.


What is an Operator in C?

An operator is a symbol that tells the compiler to perform a mathematical, relational, or logical operation between one or more operands, which are simply the values or variables the operator acts upon. For example, in the expression a + b, the plus sign is the operator, while a and b are the operands.

Example

#include <stdio.h>

int main() {

    int mondayViews = 1500;

    int tuesdayViews = 1800;

    int totalViews = mondayViews + tuesdayViews;

    printf("Total views: %d", totalViews);

    return 0;

}

Output

Total views: 3300

Arithmetic Operators

Arithmetic operators perform standard mathematical calculations and are usually the first type of operator beginners encounter, since they closely resemble the mathematics learned outside of programming.

Operator Description
+ Adds two operands together.
- Subtracts the right operand from the left operand.
* Multiplies two operands.
/ Divides the left operand by the right operand.
% Returns the remainder after dividing the left operand by the right operand.

Example

#include <stdio.h>

int main() {

    int totalSubscribers = 25000;

    int newThisWeek = 350;

    int remainder = totalSubscribers % 1000;

    printf("Remainder: %d", remainder);

    return 0;

}

Output

Remainder: 0

The modulus operator, represented by the percent symbol, is especially useful for tasks such as checking whether a number is even or odd, or for extracting specific digits from a larger number.


Relational Operators

Relational operators compare two values and produce a result indicating whether the comparison is true or false. These operators are essential whenever a program needs to make decisions based on comparing data.

Operator Description
== Checks whether two operands are equal.
!= Checks whether two operands are not equal.
> Checks whether the left operand is greater than the right operand.
< Checks whether the left operand is less than the right operand.
>= Checks whether the left operand is greater than or equal to the right operand.
<= Checks whether the left operand is less than or equal to the right operand.

Example

#include <stdio.h>

int main() {

    int views = 5000;

    int target = 4000;

    printf("Views greater than target: %d", views > target);

    return 0;

}

Output

Views greater than target: 1

In C, relational expressions evaluate to either 1, representing true, or 0, representing false, which can then be used directly in conditional statements to control program flow.


Logical Operators

Logical operators combine multiple conditions into a single expression, allowing a program to make decisions based on more than one factor at the same time.

Operator Description
&& Returns true only if both conditions on either side are true.
|| Returns true if at least one of the conditions on either side is true.
! Reverses the result of a condition, turning true into false and false into true.

Example

#include <stdio.h>

int main() {

    int subscribers = 25000;

    int uploadsThisMonth = 6;

    if (subscribers > 10000 && uploadsThisMonth >= 4) {

        printf("Channel meets monetization activity requirements.");

    }

    return 0;

}

Output

Channel meets monetization activity requirements.

Here, both conditions must be true simultaneously for the message to be displayed, demonstrating how logical operators allow multiple requirements to be checked together within a single condition.


Assignment Operators

Assignment operators are used to store values into variables. While the basic equals sign is the most familiar, C also provides several shorthand assignment operators that combine an arithmetic operation with assignment in a single step.

Operator Equivalent Expression
= Assigns the value on the right to the variable on the left.
+= a += b is equivalent to a = a + b.
-= a -= b is equivalent to a = a - b.
*= a *= b is equivalent to a = a * b.
/= a /= b is equivalent to a = a / b.

Example

#include <stdio.h>

int main() {

    int totalViews = 4000;

    totalViews += 500;

    printf("Updated total views: %d", totalViews);

    return 0;

}

Output

Updated total views: 4500

Unary Operators

Unary operators act on a single operand, unlike most other operators that require two. These are commonly used for incrementing, decrementing, or changing the sign of a value.

Operator Description
++ Increases the value of a variable by one.
-- Decreases the value of a variable by one.
- Reverses the sign of a value, turning positive into negative or vice versa.

Example

#include <stdio.h>

int main() {

    int uploadCount = 9;

    uploadCount++;

    printf("Upload count after increment: %d", uploadCount);

    return 0;

}

Output

Upload count after increment: 10

It is worth noting that placing the increment operator before or after a variable can behave differently in more complex expressions, a distinction commonly referred to as pre-increment versus post-increment.


Bitwise Operators

Bitwise operators work directly on the individual bits that make up a value's binary representation. These are less commonly used in everyday beginner programs but become important in system-level programming, embedded development, and performance-critical code.

Operator Description
& Performs a bitwise AND between the corresponding bits of two values.
| Performs a bitwise OR between the corresponding bits of two values.
^ Performs a bitwise XOR, setting a bit only when the corresponding bits differ.
~ Inverts all the bits of a value, commonly known as a bitwise complement.
<< Shifts bits to the left, effectively multiplying the value by powers of two.
>> Shifts bits to the right, effectively dividing the value by powers of two.

Example

#include <stdio.h>

int main() {

    int value = 4;

    int shifted = value << 1;

    printf("Shifted value: %d", shifted);

    return 0;

}

Output

Shifted value: 8

The Ternary Operator

The ternary operator provides a compact way to write a simple if-else decision within a single expression. It is the only operator in C that requires exactly three operands, which is where its name comes from.

Syntax

condition ? valueIfTrue : valueIfFalse

Example

#include <stdio.h>

int main() {

    int views = 8000;

    char *status = (views > 5000) ? "Trending" : "Growing";

    printf("Video status: %s", status);

    return 0;

}

Output

Video status: Trending

This single line achieves the same result as a longer if-else block, making the ternary operator useful for short, straightforward decisions that would otherwise require several extra lines of code.


Operator Precedence and Associativity

When an expression contains multiple operators, C follows a specific order to decide which operations are performed first, known as operator precedence. When operators share the same precedence level, associativity determines whether evaluation proceeds from left to right or right to left.

Concept Explanation
Precedence Determines which operator is evaluated first when multiple different operators appear in the same expression.
Associativity Determines the evaluation order when operators of equal precedence appear together, typically left to right for most operators.
Parentheses Can be used to override default precedence and make the intended order of evaluation explicit.

Example

#include <stdio.h>

int main() {

    int result = 10 + 5 * 2;

    int resultWithParentheses = (10 + 5) * 2;

    printf("Without parentheses: %d", result);

    printf("\nWith parentheses: %d", resultWithParentheses);

    return 0;

}

Output

Without parentheses: 20

With parentheses: 30

Because multiplication has higher precedence than addition, the first expression evaluates the multiplication before the addition, while adding parentheses around the addition forces it to be evaluated first instead.


Best Practices When Using Operators


Common Mistakes Beginners Make

Mistake Correct Practice
Confusing the assignment operator with the equality operator. Use a single equals sign only for assignment and a double equals sign only for comparison.
Expecting decimal results from dividing two integers. Cast at least one operand to a floating-point type when a decimal result is required.
Misusing bitwise operators in place of logical operators. Use logical AND and OR for combining boolean-style conditions, reserving bitwise operators for bit manipulation.
Ignoring operator precedence in complex expressions. Use parentheses to explicitly control the order of evaluation whenever there is any doubt.

Frequently Asked Interview Questions

  1. What is an operator in C?
    An operator is a symbol that instructs the compiler to perform a specific operation, such as arithmetic, comparison, or logical evaluation, on one or more operands.
  2. What is the difference between the equals sign and the double equals sign in C?
    A single equals sign assigns a value to a variable, while a double equals sign compares two values for equality.
  3. What does the modulus operator do?
    The modulus operator returns the remainder that results from dividing one number by another.
  4. What is the difference between logical AND and bitwise AND in C?
    Logical AND evaluates whether two entire conditions are both true, while bitwise AND operates on the individual bits of two values.
  5. What is the ternary operator used for?
    The ternary operator provides a compact way to write a simple conditional expression that selects one of two values based on a condition.
  6. What is the difference between pre-increment and post-increment?
    Pre-increment increases a variable's value before it is used in an expression, while post-increment uses the current value first and increases it afterward.
  7. What is operator precedence?
    Operator precedence determines which operator is evaluated first when an expression contains multiple different operators.
  8. Why might a program produce an unexpected integer result during division?
    Dividing two integers in C performs integer division, which discards any decimal remainder unless one of the operands is explicitly cast to a floating-point type.
  9. What do the left shift and right shift operators do?
    The left shift operator shifts bits toward higher positions, while the right shift operator shifts bits toward lower positions, effectively multiplying or dividing by powers of two.
  10. Can parentheses change the result of an expression in C?
    Yes, parentheses override the default operator precedence, forcing a specific part of the expression to be evaluated first.
  11. What values does a relational expression evaluate to in C?
    A relational expression evaluates to 1 if the comparison is true, or 0 if the comparison is false.
  12. Are shorthand assignment operators like += useful in real programs?
    Yes, they provide a more concise way to update a variable's value based on an existing arithmetic operation, improving readability for common patterns.

Summary

Operators are the building blocks that let a C program actually manipulate the data stored in its variables, from simple arithmetic calculations to complex conditional logic involving multiple comparisons. By learning arithmetic, relational, logical, assignment, unary, bitwise, and ternary operators, you gain the tools needed to express almost any calculation or decision a program might require.

In this tutorial, you explored each major category of operator in C, saw practical examples of how they behave, and learned how operator precedence determines the order of evaluation in more complex expressions. With operators firmly understood, you are now ready to explore how input and output work in C, allowing your programs to interact directly with users.


← Previous: Variables & Data Types Next: Input & Output →

Home Visit Our YouTube Channel