Operators in C

In C programming, operators are symbols that perform actions on numbers and values. They help you do math, compare numbers, and control how a program runs. C has many types of operators, such as arithmetic, logical, and assignment operators. These are important because they help in making calculations and decisions in a program. In this guide, we will explain these operators with simple examples.

Contents:

  1. What are Operators in C?
  2. Types of Operators in C
  3. Arithmetic Operators in C
  4. Relational Operators in C
  5. Logical Operators in C
  6. Bitwise Operators in C
  7. Assignment Operators in C
  8. Increment and Decrement Operators in C
  9. Ternary Operator in C
  10. Other Operators in C
  11. FAQs on Operators in C

What are Operators in C?

Operators in C are symbols that perform operations on variables and values. They help in performing calculations, comparing values, and controlling the flow of a program.

Syntax:

result = operand1 operator operand2;

Example:

advertisement
sum = a + b;  // Here, `+` is an operator that adds `a` and `b`

Types of Operators in C

Operators in C are classified into different types based on their functionality. Below are the main types of operators in C:

  • Arithmetic Operators – Perform basic math operations (+, -, *, /, %).
  • Relational Operators – Compare values (==, !=, >, <, >=, <=).
  • Logical Operators – Combine conditions (&&, ||, !).
  • Bitwise Operators – Work on bits (&, |, ^, <<, >>).
  • Assignment Operators – Assign values (=, +=, -=, *=, /=).
  • Increment and Decrement Operators – Increase or decrease values (++, –).
  • Ternary Operator – A shorthand for if-else (condition ? true_value : false_value).
  • Comma Operator – Evaluates multiple expressions (a = (b, c);).
  • Sizeof Operator – Finds the size of a variable (sizeof(int)).
  • Type Casting Operator – Converts data types ((int) 3.5).

Free 30-Day Python Certification Bootcamp is Live. Join Now!

Arithmetic Operators in C

Arithmetic operators in C help perform basic math, like adding, subtracting, multiplying, dividing, and finding the remainder. They work with both whole numbers (integers) and decimal numbers (floating-point).

Syntax:

result = operand1 operator operand2;

where operator can be +, -, *, /, or %.

List of Arithmetic Operators:

Operator Description Example (a = 10, b = 5)
+ Addition a + b = 15
Subtraction a – b = 5
* Multiplication a * b = 50
/ Division a / b = 2
% Modulus (Remainder) a % b = 0

Example of Arithmetic Operators in C

#include <stdio.h>
 
int main()
{
    int a = 15, b = 4; 
 
    printf("Addition: %d\n", a + b);
    printf("Subtraction: %d\n", a - b);
    printf("Multiplication: %d\n", a * b);
    printf("Division: %d\n", a / b);
    printf("Modulus: %d\n", a % b);
 
    return 0;
}

Output:

Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Modulus: 3

advertisement

Relational Operators in C

Relational operators in C are used to compare two values or variables. They return either true (1) or false (0) based on the comparison result.

Syntax:

variable1 operator variable2;

where operator is one of the relational operators.

Types of Relational Operators in C:

Operator Description Example (a = 10, b = 5) Result
== Equal to a == b 0 (false)
!= Not equal to a != b 1 (true)
> Greater than a > b 1 (true)
< Less than a < b 0 (false)
>= Greater than or equal to a >= b 1 (true)
<= Less than or equal to a <= b 0 (false)

Example:

#include <stdio.h>
 
int main()
{
    int a = 10, b = 5;
 
    printf("a == b: %d\n", a == b);  // 0 (false)
    printf("a != b: %d\n", a != b);  // 1 (true)
    printf("a > b: %d\n", a > b);    // 1 (true)
    printf("a < b: %d\n", a < b);    // 0 (false)
    printf("a >= b: %d\n", a >= b);  // 1 (true)
    printf("a <= b: %d\n", a <= b);  // 0 (false)
 
    return 0;
}

Output:

a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0

Logical Operators in C

Logical operators in C are used to combine multiple conditions to make logical decisions. They return true (1) or false (0).

Syntax:

condition1 operator condition2;

where operator is &&, ||, or !.

Types of Logical Operators in C:

Operator Description Example (a = 1, b = 0) Result
&& Logical AND a && b 0 (false)
|| Logical OR a || b 1 (true)
! Logical NOT !a 0 (false)

Example:

#include <stdio.h>
 
int main()
{
    int marks = 85;
    int attendance = 75;
 
    // Logical AND
    if (marks >= 50 && attendance >= 70) {
        printf("Student is eligible for the exam.\n");
    } else {
        printf("Student is not eligible for the exam.\n");
    }
 
    // Logical OR
    if (marks >= 90 || attendance >= 80) {
        printf("Student gets a special award.\n");
    } else {
        printf("No special award.\n");
    }
 
    // Logical NOT
    int pass = 0; 
    if (!pass) {
        printf("Student failed the test.\n");
    }
 
    return 0;
}

Output:

Student is eligible for the exam.
No special award.
Student failed the test.

Bitwise Operators in C

Bitwise operators in C perform operations at the bit level and manipulate individual bits of data.

Syntax:

result = operand1 operator operand2;

where operator is a bitwise operator such as &, |, ^, ~, <<, or >>.

Types of Bitwise Operators in C

Operator Symbol Description
AND & Performs bitwise AND operation
OR | Performs bitwise OR operation
XOR ^ Performs bitwise XOR operation
NOT ~ Performs bitwise NOT (one’s complement)
Left Shift << Shifts bits to the left (multiplication by 2)
Right Shift >> Shifts bits to the right (division by 2)

Examples of Bitwise Operators in C

1. Bitwise AND (&)

#include <stdio.h>
 
int main()
{
    int a = 5, b = 3; // 5 = 0101, 3 = 0011
    printf("Bitwise AND: %d\n", a & b); // 0101 & 0011 = 0001 (1)
    return 0;
}

Output:

Bitwise AND: 1

2. Bitwise OR (|)

#include <stdio.h>
 
int main()
{
    int a = 5, b = 3;
    printf("Bitwise OR: %d\n", a | b); // 0101 | 0011 = 0111 (7)
    return 0;
}

Output:

Bitwise OR: 7

3. Bitwise XOR (^)

#include <stdio.h>
 
int main()
{
    int a = 5, b = 3;
    printf("Bitwise XOR: %d\n", a ^ b); // 0101 ^ 0011 = 0110 (6)
    return 0;
}

Output:

Bitwise XOR: 6

4. Bitwise Complement (~)

#include <stdio.h>
 
int main()
{
    int a = 5;
    printf("Bitwise Complement: %d\n", ~a); // ~0101 = 1010 (-6 in 2's complement)
    return 0;
}

Output:

Bitwise Complement: -6

5. Left Shift (<<)

#include <stdio.h>
 
int main()
{
    int a = 5;
    printf("Left Shift: %d\n", a << 1); // 0101 << 1 = 1010 (10)
    return 0;
}

Output:

Left Shift: 10

6. Right Shift (>>)

#include <stdio.h>
 
int main()
{
    int a = 5;
    printf("Right Shift: %d\n", a >> 1); // 0101 >> 1 = 0010 (2)
    return 0;
}

Output:

Right Shift: 2

Assignment Operators in C

Assignment operators in C are used to assign values to variables. The most common assignment operator is =. However, C provides compound assignment operators that combine an arithmetic or bitwise operation with assignment.

Syntax:

variable operator= value;

where operator can be =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=.

Types of Assignment Operators in C

Operator Example Equivalent To Description
= a = b a = b Assigns value of b to a
+= a += b a = a + b Adds b to a and assigns the result to a
-= a -= b a = a – b Subtracts b from a and assigns the result to a
*= a *= b a = a * b Multiplies a by b and assigns the result to a
/= a /= b a = a / b Divides a by b and assigns the result to a
%= a %= b a = a % b Computes remainder of a divided by b and assigns it to a
<<= a <<= b a = a << b Left shifts a by b bits and assigns the result to a
>>= a >>= b a = a >> b Right shifts a by b bits and assigns the result to a
&= a &= b a = a & b Performs bitwise AND on a and b, then assigns the result to a
^= a ^= b a = a ^ b Performs bitwise XOR on a and b, then assigns the result to a

Example: Using Different Assignment Operators

#include <stdio.h>
 
int main()
{
    int score = 50;
 
    score += 20;  // score = score + 20
    printf("Score after += : %d\n", score);
 
    score -= 10;  // score = score - 10
    printf("Score after -= : %d\n", score);
 
    score *= 3;   // score = score * 3
    printf("Score after *= : %d\n", score);
 
    score /= 2;   // score = score / 2
    printf("Score after /= : %d\n", score);
 
    score %= 7;   // score = score % 7
    printf("Score after %%= : %d\n", score);
 
    return 0;
}

Output:

Score after += : 70  
Score after -= : 60  
Score after *= : 180  
Score after /= : 90  
Score after %= : 6

Increment and Decrement Operators in C

Increment (++) and decrement (–) operators are used to increase or decrease the value of a variable by 1.

Syntax:

variable++;  // Post-increment
++variable;  // Pre-increment
variable--;  // Post-decrement
--variable;  // Pre-decrement

Example: Using Increment and Decrement Operators

#include <stdio.h>
 
int main()
{
    int num = 10;
 
    printf("Initial Value: %d\n", num);
 
    // Prints 10, then num becomes 11
    printf("Post-increment: %d\n", num++);  
    printf("After Post-increment: %d\n", num);
 
    // Increments first, then prints 12
    printf("Pre-increment: %d\n", ++num);  
 
    // Prints 12, then num becomes 11
    printf("Post-decrement: %d\n", num--);
    printf("After Post-decrement: %d\n", num);
 
    // Decrements first, then prints 10
    printf("Pre-decrement: %d\n", --num);
 
    return 0;
}

Output:

Initial Value: 10  
Post-increment: 10  
After Post-increment: 11  
Pre-increment: 12  
Post-decrement: 12  
After Post-decrement: 11  
Pre-decrement: 10

Ternary Operator in C

The ternary operator (? :) is a shorthand for the if-else statement, used for making quick decisions in a single line of code.

Syntax:

condition ? expression1 : expression2;
  • If the condition is true, expression1 is executed.
  • If the condition is false, expression2 is executed.

Example: Using the Ternary Operator

#include <stdio.h>
 
int main()
{
    int num = 15;
 
    // Using ternary operator to check if the number is even or odd
    (num % 2 == 0) ? printf("Even\n") : printf("Odd\n");
 
    return 0;
}

Output:

Odd

Here, since 15 % 2 != 0, the condition is false, so “Odd” is printed.

Other Operators in C

Apart from arithmetic, relational, logical, bitwise, assignment, and ternary operators, C provides some additional operators:

1. Sizeof Operator (sizeof)

Used to determine the size (in bytes) of a data type or variable.

Example:

#include <stdio.h>
 
int main()
{
    int a;
    printf("Size of int: %lu bytes\n", sizeof(a));
    return 0;
}

Output:

Size of int: 4 bytes (may vary based on system)

2. Comma Operator (,)

The comma operator allows multiple expressions to be evaluated in a single statement, with only the last expression’s value being returned.

Example:

#include <stdio.h>
 
int main()
{
    int a, b;
    a = (b = 5, b + 10); // b is assigned 5, then a is assigned b + 10
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Output:

a = 15, b = 5

3. Typecast Operator ((type))

Used to explicitly convert one data type to another.

Example:

#include <stdio.h>
 
int main()
{
    int a = 10, b = 3;
    float result = (float)a / b; // Typecasting 'a' to float
    printf("Result: %.2f\n", result);
    return 0;
}

Output:

Result: 3.33

4. Pointer Operators (* and &)

Used for working with memory addresses and pointers.

Example:

#include <stdio.h>
 
int main()
{
    int num = 20;
    int *ptr = &num; // Pointer stores address of num
    printf("Value of num: %d\n", *ptr);
    return 0;
}

Output:

Value of num: 20

FAQs on Operators in C

1. What are operators in C?
Operators in C are symbols that perform operations on variables and values. For example, + is used for addition, – for subtraction, and * for multiplication.

2. What is the difference between = and == in C?
The = operator is used for assignment, meaning it assigns a value to a variable. The == operator is used for comparison to check if two values are equal.

3. What is the modulus operator % used for?
The modulus operator % gives the remainder when one number is divided by another. For example, 10 % 3 gives 1.

4. Can the ternary operator replace if-else statements?
Yes, the ternary operator ? : can replace simple if-else conditions. However, for complex logic, if-else is preferable for readability.

5. What is the difference between & and &&?
& is the bitwise AND operator, which works at the binary level. && is the logical AND operator, which is used to check conditions in a boolean expression.

6. What happens when dividing by zero using the modulus operator?
Dividing by zero using % results in a runtime error since division by zero is undefined.

7. Can we use logical operators on non-boolean values?
Yes, in C, 0 is treated as false, and any non-zero value is treated as true.

Key Points to Remember

Here is the list of key points we need to remember about “Operators in C”.

  • Operators in C are symbols that perform operations on variables and values, such as arithmetic, comparison, and logical operations.
  • Arithmetic operators (+, -, *, /, %) are used for basic mathematical calculations.
  • Relational operators (==, !=, >, <, >=, <=) compare values and return true (1) or false (0).
  • Logical operators (&&, ||, !) combine conditions to control program flow based on true/false evaluations.
  • Bitwise operators (&, |, ^, ~, <<, >>) perform operations at the binary level, manipulating individual bits.
  • Assignment operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=) assign values to variables and perform compound operations.
  • The ternary operator (condition ? true_value : false_value) acts as a shorthand for if-else conditions.
  • Other operators include sizeof (determines memory size), comma (evaluates multiple expressions), typecasting ((type) converts data types), and pointer operators (*, &) for memory address manipulation.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.