Skip to main content

C Operators

Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C is rich in built-in operators and provides the following types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Miscellaneous Operators

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

OperatorDescriptionExample
+AdditionA + B
-SubtractionA - B
*MultiplicationA * B
/DivisionA / B
%Modulus (remainder after division)A % B
++Increment (increases value by 1)A++ or ++A
--Decrement (decreases value by 1)A-- or --A

Examples of Arithmetic Operators

c
#include <stdio.h>

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

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

int c = a++; // Post-increment
printf("After a++, a = %d, c = %d\n", a, c);

int d = ++b; // Pre-increment
printf("After ++b, b = %d, d = %d\n", b, d);

return 0;
}

Output:

a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
After a++, a = 11, c = 10
After ++b, b = 6, d = 6
note

When dividing integers, the result is also an integer. For example, 5 / 2 gives 2, not 2.5. To get a decimal result, at least one operand must be a floating-point number: 5.0 / 2 gives 2.5.

Relational Operators

Relational operators are used to compare two values:

OperatorDescriptionExample
==Equal toA == B
!=Not equal toA != B
>Greater thanA > B
<Less thanA < B
>=Greater than or equal toA >= B
<=Less than or equal toA <= B

Examples of Relational Operators

c
#include <stdio.h>

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

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 >= c: %d\n", a >= c); // 1 (true)
printf("a <= c: %d\n", a <= c); // 1 (true)

return 0;
}

Output:

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

Logical Operators

Logical operators are used to combine conditional statements:

OperatorDescriptionExample
&&Logical ANDA && B
||Logical ORA || B
!Logical NOT!A

Examples of Logical Operators

c
#include <stdio.h>

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

// Logical AND: both operands must be true (non-zero)
printf("a > 0 && b > 0: %d\n", a > 0 && b > 0); // 1 (true)
printf("a > 0 && c > 0: %d\n", a > 0 && c > 0); // 0 (false)

// Logical OR: at least one operand must be true (non-zero)
printf("a > 0 || c > 0: %d\n", a > 0 || c > 0); // 1 (true)
printf("b < 0 || c > 0: %d\n", b < 0 || c > 0); // 0 (false)

// Logical NOT: reverses the logical state
printf("!a: %d\n", !a); // 0 (false, because a is non-zero)
printf("!c: %d\n", !c); // 1 (true, because c is zero)

return 0;
}

Output:

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

Bitwise Operators

Bitwise operators operate on the individual bits of their operands:

OperatorDescriptionExample
&Bitwise ANDA & B
|Bitwise ORA | B
^Bitwise XOR (exclusive OR)A ^ B
~Bitwise complement (one's complement)~A
<<Left shiftA << n
>>Right shiftA >> n

Examples of Bitwise Operators

c
#include <stdio.h>

int main() {
unsigned int a = 60; // 00111100 in binary
unsigned int b = 13; // 00001101 in binary

printf("a & b = %u\n", a & b); // Bitwise AND: 00001100 (12)
printf("a | b = %u\n", a | b); // Bitwise OR: 00111101 (61)
printf("a ^ b = %u\n", a ^ b); // Bitwise XOR: 00110001 (49)
printf("~a = %u\n", ~a); // Bitwise complement: 11000011... (4294967235 for 32-bit int)
printf("a << 2 = %u\n", a << 2); // Left shift: 11110000 (240)
printf("a >> 2 = %u\n", a >> 2); // Right shift: 00001111 (15)

return 0;
}

Output:

a & b = 12
a | b = 61
a ^ b = 49
~a = 4294967235
a << 2 = 240
a >> 2 = 15

Assignment Operators

Assignment operators are used to assign values to variables:

OperatorDescriptionExampleEquivalent to
=Simple assignmenta = ba = b
+=Add and assigna += ba = a + b
-=Subtract and assigna -= ba = a - b
*=Multiply and assigna *= ba = a * b
/=Divide and assigna /= ba = a / b
//=Floor divide and assigna //= ba = a // b
%=Modulus and assigna %= ba = a % b
**=Exponent and assigna **= ba = a ** b
&=Bitwise AND and assigna &= ba = a & b
`=`Bitwise OR and assigna |= b
^=Bitwise XOR and assigna ^= ba = a ^ b
<<=Left shift and assigna <<= ba = a << b
>>=Right shift and assigna >>= ba = a >> b

Examples of Assignment Operators

c
#include <stdio.h>

int main() {
int a = 10;

printf("Initial value: a = %d\n", a);

a += 5; // a = a + 5
printf("After a += 5: a = %d\n", a);

a -= 3; // a = a - 3
printf("After a -= 3: a = %d\n", a);

a *= 2; // a = a * 2
printf("After a *= 2: a = %d\n", a);

a /= 4; // a = a / 4
printf("After a /= 4: a = %d\n", a);

a %= 2; // a = a % 2
printf("After a %%= 2: a = %d\n", a);

return 0;
}

Output:

Initial value: a = 10
After a += 5: a = 15
After a -= 3: a = 12
After a *= 2: a = 24
After a /= 4: a = 6
After a %= 2: a = 0

Miscellaneous Operators

OperatorDescriptionExample
sizeofReturns the size of a variable or data typesizeof(a)
&Returns the address of a variable&a
*Pointer to a variable*a
? :Conditional expression (ternary operator)condition ? expr1 : expr2
,Comma operatorexpr1, expr2

Examples of Miscellaneous Operators

c
#include <stdio.h>

int main() {
int a = 10;
int *ptr;

// sizeof operator
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of variable a: %zu bytes\n", sizeof(a));

// & operator (address-of)
ptr = &a;
printf("Address of a: %p\n", &a);

// * operator (dereferencing)
printf("Value at address stored in ptr: %d\n", *ptr);

// Ternary operator (? :)
int max = (a > 5) ? a : 5;
printf("Maximum value: %d\n", max);

// Comma operator
int b = (a = 5, a + 2); // Assigns 5 to a, then assigns (a + 2) to b
printf("a = %d, b = %d\n", a, b);

return 0;
}

Output (the memory address will vary):

Size of int: 4 bytes
Size of variable a: 4 bytes
Address of a: 0x7ffd382a57a4
Value at address stored in ptr: 10
Maximum value: 10
a = 5, b = 7

Operator Precedence

Operators have different precedence levels, which determine the order in which they are evaluated in an expression.

PrecedenceOperatorDescriptionAssociativity
1() [] -> .Function call, array subscript, member accessLeft to right
2! ~ ++ -- + - * & (type) sizeofUnary operatorsRight to left
3* / %MultiplicativeLeft to right
4+ -AdditiveLeft to right
5<< >>Bitwise shiftLeft to right
6< <= > >=RelationalLeft to right
7== !=EqualityLeft to right
8&Bitwise ANDLeft to right
9^Bitwise XORLeft to right
10|Bitwise ORLeft to right
11&&Logical ANDLeft to right
12||Logical ORLeft to right
13?:Conditional (ternary)Right to left
14= += -= *= /= %= &= ^= |= <<= >>=AssignmentRight to left
15,CommaLeft to right

Example of Operator Precedence

c
#include <stdio.h>

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

// Operator precedence in action
result = a + b * c; // Multiplication has higher precedence than addition
printf("a + b * c = %d\n", result); // Equivalent to a + (b * c)

result = (a + b) * c; // Parentheses change precedence
printf("(a + b) * c = %d\n", result);

// Complex example
result = a + b * c / (a - 2) + b;
printf("a + b * c / (a - 2) + b = %d\n", result);
// Evaluated as: a + ((b * c) / (a - 2)) + b

return 0;
}

Output:

a + b * c = 35
(a + b) * c = 80
a + b * c / (a - 2) + b = 18

Summary

  • Arithmetic operators perform mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
  • Relational operators compare values and return either true (1) or false (0).
  • Logical operators combine conditional statements using AND (&&), OR (||), and NOT (!).
  • Bitwise operators perform operations on individual bits of integers.
  • Assignment operators assign values to variables, sometimes combining assignment with another operation.
  • Miscellaneous operators include sizeof, address operator (&), dereference operator (*), and the ternary conditional operator (?:).
  • Operator precedence determines the order in which operations are performed in an expression.

Understanding operators is essential for writing efficient and effective C programs. They form the basic building blocks for manipulating data and controlling program flow.



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)