Operators in C Programming

In C programming, operators are special symbols used to perform operations on variables and values. They form the backbone of expressions and are essential for writing logical, arithmetic, and decision-making programs.

Understanding operators in C language is crucial for beginners, exam preparation, and competitive programming.

What Are Operators in C?

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation.

Example:

int sum = a + b;

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

Types of Operators in C Programming

C provides several types of operators, categorized based on their functionality.

Operators in C Programming

Let’s explain all types one by one

1. Arithmetic Operators in C

Used to perform mathematical calculations.

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

Example:

C Programming Code
#include <stdio.h> 
int main() {
int a = 10, b = 3;
printf("Addition (a + b): %d\n", a + b);
printf("Subtraction (a - b): %d\n", a - b);
printf("Multiplication (a * b): %d\n", a * b);
printf("Division (a / b): %d\n", a / b);
printf("Modulus (a %% b): %d\n", a % b);
}
Output:
Addition (a + b): 13
Subtraction (a - b): 7
Multiplication (a * b): 30
Division (a / b): 3
Modulus (a % b): 1

2. Unary Operators in C

Unary operators operate on only one operand.

Unary Operators Table

Operator Name Description Example
+ Unary Plus Represents positive value (no change) +a
- Unary Minus Negates the value -a
++ Increment Increases value by 1 (pre / post) ++a, a++
-- Decrement Decreases value by 1 (pre / post) --a, a--
! Logical NOT Reverses logical value !a
~ Bitwise NOT (1’s comp.) Inverts all bits ~a
sizeof Sizeof Operator Returns size of variable/data type (bytes) sizeof(a)
* Dereference Access value pointed by pointer *ptr
& Address-of Returns address of variable &a
(type) Type Cast Converts one data type to another (float)a

Program: Unary Operators in C (with Output)

#include <stdio.h>

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

printf("Unary plus (+a): %d\n", +a);
printf("Unary minus (-a): %d\n", -a);
printf("Pre-increment (++a): %d\n", ++a);
printf("Post-increment (a++): %d\n", a++);
printf("After post-increment a: %d\n", a);
printf("Pre-decrement (--a): %d\n", --a);
printf("Post-decrement (a--): %d\n", a--);
printf("After post-decrement a: %d\n", a);
printf("Logical NOT (!b): %d\n", !b);
printf("Bitwise NOT (~a): %d\n", ~a);
printf("Size of int: %lu bytes\n", sizeof(int));

float f;
int *p;
p = &a;
printf("Address of a (&a): %p\n", &a);
printf("Value using pointer (*p): %d\n", *p);
f = (float)a;
printf("Type casting int to float: %.2f\n", f);

return 0;
}

Output:

Unary plus (+a): 5
Unary minus (-a): -5
Pre-increment (++a): 6
Post-increment (a++): 6
After post-increment a: 7
Pre-decrement (--a): 6
Post-decrement (a--): 6
After post-decrement a: 5
Logical NOT (!b): 1
Bitwise NOT (~a): -6
Size of int: 4 bytes
Address of a (&a): 0x7ffee3b9a8ac
Value using pointer (*p): 5
Type casting int to float: 5.00

3. Relational Operators in C

Relational operators are used to compare two operands. They return 1 (true) if the condition is true and 0 (false) if the condition is false.

Relational Operators Table

Operator Name Description Example
== Equal to True if both operands are equal a == b
!= Not equal to True if operands are not equal a != b
> Greater than True if left operand is greater a > b
< Less than True if left operand is smaller a < b
>= Greater than or equal to True if left is greater or equal a >= b
<= Less than or equal to True if left is less or equal a <= b

Program: Relational Operators in C (with Output)

#include <stdio.h>
int main() {
int a = 10, b = 5;

printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
printf("a > b: %d\n", a > b);
printf("a < b: %d\n", a < b);
printf("a >= b: %d\n", a >= b);
printf("a <= b: %d\n", a <= b);

return 0;
}

Output

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

4. Logical Operators in C

Logical operators are used to combine or invert relational expressions. They return 1 (true) or 0 (false).

Logical Operators Table

Operator Name Description Example
&& Logical AND True if both conditions are true a > b && b > 0
|| Logical OR True if any one condition is true Logical OR
! Logical NOT Reverses the logical result !(a > b)

Program: Logical Operators in C (with Output)

#include <stdio.h>

int main() {
int a = 10, b = 5, c = 0;

printf("(a > b && b > c): %d\n", (a > b && b > c));
printf("(a > b || b < c): %d\n", (a > b || b < c));
printf("!(a > b): %d\n", !(a > b));
printf("!(c): %d\n", !c);

return 0;
}

Output

(a > b && b > c): 1
(a > b || b < c): 1
!(a > b): 0
!(c): 1

5. Assignment Operators in C

 

6. Increment and Decrement Operators in C

Used to increase or decrease a variable’s value by 1.

Operator Name
++ Increment
-- Decrement

Prefix vs Postfix Example:

int x = 5;
printf("%d", ++x); // 6 (prefix)
printf("%d", x++); // 6 then x becomes 7

7. Bitwise Operators in C

Operate at the bit level.

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

Example:

int a = 5, b = 3;
printf("%d", a & b); // Output: 1

8. Conditional (Ternary) Operator in C

A shorthand for if-else.

Syntax:

(condition) ? true_value : false_value;

Example:

int max = (a > b) ? a : b;

9. Special Operators in C

Operator Use
sizeof Size of data type
, Comma operator
& Address of variable
* Pointer dereference
. Structure member
-> Structure pointer member

Example:

printf("%d", sizeof(int));

Operator Precedence in C Programming

Operator precedence determines the order of evaluation.

Priority Operators
Highest (), ++, --
High *, /, %
Medium +, -
Low ==, <, >
Lowest =, +=, -=

Tip: Use parentheses to avoid confusion.

Why Operators Are Important in C?

 Build expressions
 Control program flow
 Perform calculations
 Essential for algorithms and data structures

Conclusion

Operators in C programming are fundamental tools that allow programmers to perform computations, make decisions, and manipulate data efficiently. Mastering operator types and precedence helps write error-free and optimized C programs.