Curriculum
In C programming, operators are symbols or special tokens used to perform operations on data or manipulate values. Operators allow you to carry out mathematical, logical, and other operations, making it possible to perform a wide range of tasks in your programs.
In this lesson, we’ll explore the various types of operators in C and how to use them effectively.
Arithmetic operators perform basic mathematical operations on numeric values. Here are the common arithmetic operators in C:
+
: Adds two operands.
Example:
int sum = 5 + 3; // sum is 8
Subtraction -
: Subtracts the right operand from the left operand.
Example:
int difference = 10 - 4; // difference is 6
Multiplication *
: Multiplies two operands.
Example:
int product = 6 * 7; // product is 42
Division /
: Divides the left operand by the right operand.
Example:
float quotient = 15.0 / 3; // quotient is 5.0
Modulus %
: Computes the remainder when the left operand is divided by the right operand.
Example:
int remainder = 17 % 4; // remainder is 1
Relational operators compare two values and return a Boolean result (true
or false
) based on the relationship between them. Here are the common relational operators in C:
==
: Checks if two values are equal.
Example:
int a = 5, b = 5; _Bool isEqual = (a == b); // isEqual is true
Not equal to !=
: Checks if two values are not equal.
Example:
int x = 10, y = 20; _Bool isNotEqual = (x != y); // isNotEqual is true
Greater than >
: Checks if the left operand is greater than the right operand.
Example:
int m = 15, n = 8; _Bool isGreater = (m > n); // isGreater is true
Less than <
: Checks if the left operand is less than the right operand.
Example:
int p = 7, q = 12; _Bool isLess = (p < q); // isLess is true
Less than or equal to <=
: Checks if the left operand is less than or equal to the right operand.
Example:
int val1 = 5, val2 = 8; _Bool isLessOrEqual = (val1 <= val2); // isLessOrEqual is true
Logical operators are used to combine and manipulate Boolean values. They are typically used in conditional statements and loops. Here are the common logical operators in C:
&&
: Returns true
if both operands are true
.
Example:
_Bool condition1 = true, condition2 = false; _Bool result = (condition1 && condition2); // result is false
Logical OR ||
: Returns true
if at least one of the operands is true
.
Example:
_Bool isSunny = true, isRainy = false; _Bool isGoodWeather = (isSunny || isRainy); // isGoodWeather is true
Logical NOT !
: Negates the value of an operand.
Example:
_Bool isCloudy = false; _Bool isNotCloudy = !isCloudy; // isNotCloudy is true
Assignment operators are used to assign values to variables. They also allow for combined operations. Here are some common assignment operators in C:
=
: Assigns the value on the right to the variable on the left.
Example:
int num = 42; // Assigns the value 42 to 'num'
Addition assignment +=
: Adds the value on the right to the variable on the left and assigns the result to the left variable.
Example:
int total = 10; total += 5; // Equivalent to: total = total + 5;
Subtraction assignment -=
: Subtracts the value on the right from the variable on the left and assigns the result to the left variable.
Example:
int balance = 100; balance -= 20; // Equivalent to: balance = balance - 20;
Multiplication assignment *=
: Multiplies the variable on the left by the value on the right and assigns the result to the left variable.
Example:
int quantity = 4; quantity *= 3; // Equivalent to: quantity = quantity * 3;
Division assignment /=
: Divides the variable on the left by the value on the right and assigns the result to the left variable.
Example:
float totalAmount = 100.0; totalAmount /= 2; // Equivalent to: totalAmount = totalAmount / 2;
Increment and decrement operators are used to increase or decrease the value of a variable by one. They come in two forms: postfix (x++
and x--
) and prefix (++x
and --x
). Postfix operators return the current value before the change, while prefix operators return the updated value.
Example of postfix increment:
int count = 5; int result = count++; // result is 5, count is now 6
Example of prefix increment:
int count = 5; int result = ++count; // result is 6, count is now 6
Example of postfix decrement:
int count = 5; int result = count--; // result is 5, count is now 4
Example of prefix decrement:
int count = 5; int result = --count; // result is 4, count is now 4
Bitwise operators manipulate individual bits of data. They are often used for low-level operations, such as working with hardware or optimizing code. Here are the common bitwise operators in C:
&
: Performs a bitwise AND operation between the bits of two integers.
Example:
int num1 = 6; // Binary: 0110 int num2 = 3; // Binary: 0011 int result = num1 & num2; // Binary result: 0010 (decimal 2)
Bitwise OR |
: Performs a bitwise OR operation between the bits of two integers.
Example:
int num1 = 6; // Binary: 0110 int num2 = 3; // Binary: 0011 int result = num1 | num2; // Binary result: 0111 (decimal 7)
Bitwise XOR ^
: Performs a bitwise XOR (exclusive OR) operation between the bits of two integers.
Example:
int num1 = 6; // Binary: 0110 int num2 = 3; // Binary: 0011 int result = num1 ^ num2; // Binary result: 0101 (decimal 5)
Bitwise NOT ~
: Inverts all the bits of an integer, turning 1s into 0s and 0s into 1s.
Example:
int num = 5; // Binary: 0101 int result = ~num; // Binary result: 1010 (decimal -6 in two's complement)
Left Shift <<
: Shifts the bits of an integer to the left by a specified number of positions.
Example:
int num = 8; // Binary: 1000 int result = num << 2; // Binary result: 100000 (decimal 32)
Right Shift >>
: Shifts the bits of an integer to the right by a specified number of positions. The sign bit is preserved for signed integers.
Example:
int num = -16; // Binary: 11111111111111111111111111110000 (32-bit signed integer) int result = num >> 2; // Binary result: 11111111111111111111111111111100 (decimal -4 in two's complement)
You can combine bitwise operators with assignment operators to perform bitwise operations and update variables in a single step.
Example of bitwise AND assignment:
int num1 = 6; // Binary: 0110 int num2 = 3; // Binary: 0011 num1 &= num2; // Bitwise AND assignment: num1 becomes 0010 (decimal 2)
Example of bitwise OR assignment:
int num1 = 6; // Binary: 0110 int num2 = 3; // Binary: 0011 num1 |= num2; // Bitwise OR assignment: num1 becomes 0111 (decimal 7)
Example of bitwise XOR assignment:
int num1 = 6; // Binary: 0110 int num2 = 3; // Binary: 0011 num1 ^= num2; // Bitwise XOR assignment: num1 becomes 0101 (decimal 5)
The comma operator ,
is used to separate expressions in a statement. It evaluates each expression from left to right and returns the value of the rightmost expression.
Example:
int a = 5, b = 10, c; c = (a++, b++); // The value of 'c' is 10
In this example, the comma operator is used to increment both a
and b
, but c
is assigned the value of b
.
Operators in C are powerful tools that enable you to perform a wide range of operations, from basic arithmetic to bitwise manipulations and conditional expressions. By understanding the various types of operators and their usage, you can write more expressive and efficient C programs.