C operators are symbols that are used to perform operations on variables and values. C supports several types of operators, including:
- Arithmetic operators
- Relational/Comparison operators
- Logical operators
- Assignment operators
- Bitwise operators
- Conditional operators
1. Arithmetic Operators
In C, arithmetic operators are used to perform basic arithmetic operations on numerical values. The following are the arithmetic operators in C:
- Addition operator (+): Adds two values together.
- Subtraction operator (-): Subtracts one value from another.
- Multiplication operator (*): Multiplies two values together.
- Division operator (/): Divides one value by another.
- Modulus operator (%): Returns the remainder of a division operation.
For example, the following code demonstrates the use of arithmetic operators in C:
int x = 10; int y = 3; int sum = x + y; // Adds x and y, assigns result to sum int difference = x - y; // Subtracts y from x, assigns result to difference int product = x * y; // Multiplies x and y, assigns result to product int quotient = x / y; // Divides x by y, assigns result to quotient int remainder = x % y; // Calculates the remainder of x divided by y, assigns result to remainder
In this example, the value of “sum” would be 13, the value of “difference” would be 7, the value of “product” would be 30, the value of “quotient” would be 3, and the value of “remainder” would be 1.
Arithmetic operators are an essential part of any programming language and are frequently used in mathematical and scientific calculations.
2. Relational/Comparison Operators
Relational operators in C are used to compare two values and return a Boolean value (true or false) indicating whether the comparison is true or false. The following are the relational operators in C:
- Greater than operator (>): Returns true if the left operand is greater than the right operand.
- Less than operator (<): Returns true if the left operand is less than the right operand.
- Greater than or equal to operator (>=): Returns true if the left operand is greater than or equal to the right operand.
- Less than or equal to operator (<=): Returns true if the left operand is less than or equal to the right operand.
- Equal to operator (==): Returns true if the left operand is equal to the right operand.
- Not equal to operator (!=): Returns true if the left operand is not equal to the right operand.
For example, the following code demonstrates the use of relational operators in C:
int x = 10; int y = 3; if (x > y) { printf("x is greater than y\n"); } if (x == y) { printf("x is equal to y\n"); } if (x != y) { printf("x is not equal to y\n"); }
In this example, the first “if” statement would be true because x is greater than y, so the program would output “x is greater than y”. The second “if” statement would be false because x is not equal to y, so the program would not output “x is equal to y”. The third “if” statement would be true because x is not equal to y, so the program would output “x is not equal to y”.
Relational operators are commonly used in conditional statements to make decisions based on the result of a comparison.
3. Logical Operators
Logical operators in C are used to perform logical operations on Boolean values (true or false). The following are the logical operators in C:
- Logical AND operator (&&): Returns true if both the left and right operands are true.
- Logical OR operator (||): Returns true if either the left or right operand is true.
- Logical NOT operator (!): Returns true if the operand is false, and vice versa.
For example, the following code demonstrates the use of logical operators in C:
int x = 10; int y = 3; int z = 5; if (x > y && y > z) { printf("x is greater than y, and y is greater than z\n"); } if (x > y || y > z) { printf("Either x is greater than y, or y is greater than z, or both\n"); } if (!(x > y)) { printf("x is not greater than y\n"); }
In this example, the first “if” statement would be true because x is greater than y and y is greater than z, so the program would output “x is greater than y, and y is greater than z”. The second “if” statement would also be true because x is greater than y, so the program would output “Either x is greater than y, or y is greater than z, or both”. The third “if” statement would be false because x is greater than y, so the program would not output “x is not greater than y”.
Logical operators are commonly used in conditional statements and loops to make decisions based on the truth or falsity of multiple conditions.
4. Assignment Operators
Assignment operators in C are used to assign a value to a variable. The basic assignment operator in C is the equals sign (=), which assigns the value on the right-hand side of the operator to the variable on the left-hand side.
There are also several compound assignment operators in C that combine an arithmetic operation with the basic assignment operation. The following are the compound assignment operators in C:
- Addition assignment operator (+=): Adds the value on the right-hand side of the operator to the variable on the left-hand side and assigns the result to the variable.
- Subtraction assignment operator (-=): Subtracts the value on the right-hand side of the operator from the variable on the left-hand side and assigns the result to the variable.
- Multiplication assignment operator (*=): Multiplies the variable on the left-hand side by the value on the right-hand side of the operator and assigns the result to the variable.
- Division assignment operator (/=): Divides the variable on the left-hand side by the value on the right-hand side of the operator and assigns the result to the variable.
- Modulus assignment operator (%=): Computes the remainder of the division of the variable on the left-hand side by the value on the right-hand side of the operator and assigns the result to the variable.
For example, the following code demonstrates the use of compound assignment operators in C:
int x = 10; int y = 3; x += y; // equivalent to x = x + y; printf("x = %d\n", x); x -= y; // equivalent to x = x - y; printf("x = %d\n", x); x *= y; // equivalent to x = x * y; printf("x = %d\n", x); x /= y; // equivalent to x = x / y; printf("x = %d\n", x); x %= y; // equivalent to x = x % y; printf("x = %d\n", x);
In this example, the value of x starts at 10, and y is 3. The code uses compound assignment operators to add y to x, subtract y from x, multiply x by y, divide x by y, and compute the remainder of x divided by y. Each operation modifies the value of x and then prints the new value of x.
5. Bitwise Operators
Bitwise operators in C are used to manipulate the bits of integer data types such as int, char, and long. These operators perform operations at the bit level, which can be useful in low-level programming and embedded systems.
There are six bitwise operators in C:
- Bitwise AND operator (&): This operator performs the AND operation between each pair of corresponding bits of the two operands. The result is 1 only if both bits are 1.
- Bitwise OR operator (|): This operator performs the OR operation between each pair of corresponding bits of the two operands. The result is 0 only if both bits are 0.
- Bitwise XOR operator (^): This operator performs the XOR (exclusive OR) operation between each pair of corresponding bits of the two operands. The result is 1 if the bits are different and 0 if they are the same.
- Bitwise complement operator (~): This operator performs the one’s complement operation on the operand, which means it changes all 0s to 1s and all 1s to 0s.
- Left shift operator (<<): This operator shifts the bits of the first operand to the left by the number of positions specified by the second operand.
- Right shift operator (>>): This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand.
For example, the following code demonstrates the use of bitwise operators in C:
unsigned int a = 60; // 0011 1100 unsigned int b = 13; // 0000 1101 unsigned int c = 0; c = a & b; // 0000 1100 printf("a & b = %d\n", c); c = a | b; // 0011 1101 printf("a | b = %d\n", c); c = a ^ b; // 0011 0001 printf("a ^ b = %d\n", c); c = ~a; // 1100 0011 printf("~a = %d\n", c); c = a << 2; // 1111 0000 printf("a << 2 = %d\n", c); c = a >> 2; // 0000 1111 printf("a >> 2 = %d\n", c);
In this example, the variables a and b are initialized to 60 and 13, respectively, and are represented in binary notation. The code uses bitwise operators to perform the AND, OR, XOR, complement, left shift, and right shift operations between a and b. The result of each operation is stored in the variable c and then printed.
6. Conditional Operators
Conditional operators in C are used to make decisions based on a condition. They are also called ternary operators because they take three operands. The syntax of the conditional operator is as follows:
condition ? expression1 : expression2;
The condition is evaluated first. If the condition is true, then expression1 is evaluated and becomes the value of the entire expression. If the condition is false, then expression2 is evaluated and becomes the value of the entire expression.
For example, the following code demonstrates the use of the conditional operator in C:
#include <stdio.h> int main() { int x, y, max; printf("Enter two integers: "); scanf("%d%d", &x, &y); max = (x > y) ? x : y; printf("The maximum of %d and %d is %d\n", x, y, max); return 0; }
In this example, the program prompts the user to enter two integers using the scanf
function. The program then uses the conditional operator to determine which integer is larger and assigns the larger integer to the variable max
. Finally, the program prints the value of max
using the printf
function.
Conditional operators can be useful in situations where a decision needs to be made based on a condition. They can also make code more concise and easier to read by avoiding the use of if-else statements in certain situations.
By using operators, you can perform a wide range of operations on variables and values, allowing you to create complex and powerful programs in C.