Operator precedence
Operator precedence determines the order in which operations are performed in an expression. In C, operators have different levels of precedence, & operations with higher precedence are performed before those with lower precedence. If operators have the same precedence, they are evaluated from left to right.
Let’s look at the order of precedence for commonly used operators in C (from highest to lowest):
1. Parentheses ()
2. Unary operators (!, ++, --, +, -, *, &, sizeof)
3. Multiplicative operators (*, /, %)
4. Additive operators (+, -)
5. Relational operators (<, <=, >, >=)
6. Equality operators (==, !=)
7. Logical AND (&&)
8. Logical OR (||)
9. Assignment operators (=, +=, -=, *=, /=, %=)
For example:
#include <stdio.h>
int main() {
int result = 5 + 3 * 4 - 2; // Equivalent to 5 + (3 * 4) - 2
printf("Result: %d\n", result);
result = (5 + 3) * (4 - 2); // Parentheses override precedence
printf("Result with parentheses: %d\n", result);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In the first expression, the multiplication (3 * 4) is performed first due to its higher precedence, resulting in 12. Then, the addition (5 + 12) & subtraction (17 - 2) are performed from left to right, giving the final result of 15.
In the second expression, the parentheses override the default precedence. The additions & subtractions inside the parentheses are performed first, resulting in (8) * (2). Finally, the multiplication is performed, giving the result of 16.
The output of this program will be:
Result: 15
Result with parentheses: 16
Assignment Operator (=)
The assignment operator (=) is used to assign a value to a variable. It takes the value on the right side of the operator & assigns it to the variable on the left side.
For example:
#include <stdio.h>
int main() {
int x;
x = 10; // Assigning the value 10 to variable x
printf("Value of x: %d\n", x);
int y = 5; // Declaring and initializing variable y in a single line
printf("Value of y: %d\n", y);
x = y; // Assigning the value of y to x
printf("New value of x: %d\n", x);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we first declare an integer variable 'x'. We then use the assignment operator to assign the value 10 to 'x'. Next, we declare & initialize another integer variable 'y' with the value 5 in a single line.
We print the values of 'x' & 'y' using printf(). Finally, we use the assignment operator again to assign the value of 'y' to 'x', effectively changing the value of 'x' to 5.
The output of this program will be:
Value of x: 10
Value of y: 5
New value of x: 5
It's important to note that the assignment operator (=) is different from the equality operator (==), which is used for comparison.
Logical Operators (AND, OR)
Logical operators are used to combine or modify logical expressions. In C, the two main logical operators are AND (&&) & OR (||).
The AND operator (&&) returns true if both operands are true. If either or both operands are false, it returns false.
The OR operator (||) returns true if at least one of the operands is true. It returns false only if both operands are false.
Here's an example that demonstrates the usage of logical operators:
#include <stdio.h>
int main() {
int x = 5;
int y = 10;
if (x > 0 && y < 20) {
printf("Both conditions are true.\n");
}
if (x > 0 || y > 20) {
printf("At least one condition is true.\n");
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we have two integer variables 'x' & 'y'. In the first if statement, we use the AND operator (&&) to check if both conditions (x > 0 and y < 20) are true. Since both conditions are indeed true, the message "Both conditions are true." will be printed.
In the second if statement, we use the OR operator (||) to check if at least one of the conditions (x > 0 or y > 20) is true. In this case, the first condition (x > 0) is true, so the message "At least one condition is true." will be printed.
The output of this program will be:
Both conditions are true.
At least one condition is true.
Note: Logical operators are very useful in conditional statements and loops to control the flow of the program based on multiple conditions.
Relational Operators
Relational operators are used to compare two values and determine the relationship between them. The result of a relational operation is always a Boolean value (true or false). In C, the relational operators are:
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
- Equal to (==)
- Not equal to (!=)
For example:
#include <stdio.h>
int main() {
int a = 10;
int b = 5;
if (a > b) {
printf("%d is greater than %d\n", a, b);
}
if (b < a) {
printf("%d is less than %d\n", b, a);
}
if (a >= 10) {
printf("%d is greater than or equal to 10\n", a);
}
if (b <= 5) {
printf("%d is less than or equal to 5\n", b);
}
if (a == 10) {
printf("%d is equal to 10\n", a);
}
if (b != 10) {
printf("%d is not equal to 10\n", b);
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we have two integer variables 'a' and 'b'. We use relational operators to compare these variables in different scenarios.
The output of this program will be:
10 is greater than 5
5 is less than 10
10 is greater than or equal to 10
5 is less than or equal to 5
10 is equal to 10
5 is not equal to 10
String comparisons
In C, you can compare strings using the strcmp() function from the string.h library. The strcmp() function compares two strings lexicographically & returns an integer value indicating the relationship between them.
For example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
char str3[] = "Hello";
int result1 = strcmp(str1, str2);
int result2 = strcmp(str1, str3);
if (result1 == 0) {
printf("%s is equal to %s\n", str1, str2);
} else if (result1 < 0) {
printf("%s is less than %s\n", str1, str2);
} else {
printf("%s is greater than %s\n", str1, str2);
}
if (result2 == 0) {
printf("%s is equal to %s\n", str1, str3);
} else if (result2 < 0) {
printf("%s is less than %s\n", str1, str3);
} else {
printf("%s is greater than %s\n", str1, str3);
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we have three character arrays (strings) str1, str2, & str3. We use the strcmp() function to compare str1 with str2 & str1 with str3.
The strcmp() function returns:
- 0 if the strings are equal
- A negative value if the first string is less than the second string
- A positive value if the first string is greater than the second string
Based on the return value of strcmp(), we use if-else statements to print the appropriate message indicating the relationship between the strings.
The output of this program will be:
Hello is less than World
Hello is equal to Hello
Note: String comparison is useful when you need to make decisions based on the lexicographical order of strings or check for equality between strings.
Left Shift
The left shift operator (<<) shifts the bits of a number to the left by a specified number of positions. It effectively multiplies the number by 2 raised to the power of the shift amount.
For example:
#include <stdio.h>
int main() {
int num = 10; // Binary representation: 1010
int result1 = num << 1; // Left shift by 1 position
int result2 = num << 2; // Left shift by 2 positions
printf("Original number: %d\n", num);
printf("Left shift by 1: %d\n", result1);
printf("Left shift by 2: %d\n", result2);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we have an integer variable 'num' with a value of 10. In binary representation, 10 is 1010.
We use the left shift operator (<<) to shift the bits of 'num' to the left. When we left shift by 1 position, each bit moves one position to the left, and a 0 is added as the rightmost bit. So, 1010 becomes 10100, which is equivalent to 20 in decimal.
Similarly, when we left shift by 2 positions, each bit moves two positions to the left, and two 0s are added as the rightmost bits. So, 1010 becomes 101000, which is equivalent to 40 in decimal.
The output of this program will be:
Original number: 10
Left shift by 1: 20
Left shift by 2: 40
Note: The left shift operator is useful when you need to multiply a number by powers of 2 efficiently. It can also be used for bit manipulation tasks.
Right Shift
The right shift operator (>>) shifts the bits of a number to the right by a specified number of positions. It effectively divides the number by 2 raised to the power of the shift amount.
For example:
#include <stdio.h>
int main() {
int num = 20; // Binary representation: 10100
int result1 = num >> 1; // Right shift by 1 position
int result2 = num >> 2; // Right shift by 2 positions
printf("Original number: %d\n", num);
printf("Right shift by 1: %d\n", result1);
printf("Right shift by 2: %d\n", result2);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we have an integer variable 'num' with a value of 20. In binary representation, 20 is 10100.
We use the right shift operator (>>) to shift the bits of 'num' to the right. When we right shift by 1 position, each bit moves one position to the right, and the leftmost bit is discarded. So, 10100 becomes 1010, which is equivalent to 10 in decimal.
Similarly, when we right shift by 2 positions, each bit moves two positions to the right, and the two leftmost bits are discarded. So, 10100 becomes 101, which is equivalent to 5 in decimal.
The output of this program will be:
Original number: 20
Right shift by 1: 10
Right shift by 2: 5
Note: The right shift operator is useful when you need to divide a number by powers of 2 efficiently. It can also be used for bit manipulation tasks.
Ones complement
The ones' complement operator (~), also known as the bitwise NOT operator, inverts all the bits of a number. It changes every 0 to a 1 and every 1 to a 0.
For example:
#include <stdio.h>
int main() {
int num = 10; // Binary representation: 0000 1010
int result = ~num; // Ones' complement of num
printf("Original number: %d\n", num);
printf("Ones' complement: %d\n", result);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we have an integer variable 'num' with a value of 10. In binary representation, 10 is 0000 1010 (assuming 8-bit representation for simplicity).
We use the ones' complement operator (~) to invert all the bits of 'num'. So, 0000 1010 becomes 1111 0101.
The output of this program will be:
Original number: 10
Ones' complement: -11
The reason the ones' complement of 10 is -11 is due to the way integers are represented in C using two's complement. In two's complement, the leftmost bit is used as the sign bit (0 for positive numbers and 1 for negative numbers), and the remaining bits represent the magnitude of the number.
When we apply the ones' complement to a positive number, it results in its negative counterpart minus 1. In this case, the ones' complement of 10 (0000 1010) is -11 (1111 0101) in two's complement representation.
Note: The ones' complement operator is used in bitwise operations and certain arithmetic calculations.
Conditional operator (? :)
The conditional operator, also known as the ternary operator, is a shorthand way of writing an if-else statement. The syntax is:
(condition) ? (expression1) : (expression2)
If the condition evaluates to true, expression1 is executed. If the condition evaluates to false, expression2 is executed.
For example:
#include <stdio.h>
int main() {
int num = 5;
char* result = (num % 2 == 0) ? "even" : "odd";
printf("%d is an %s number.\n", num, result);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example, we have an integer variable 'num' with a value of 5. We use the conditional operator to determine whether 'num' is even or odd.
The condition (num % 2 == 0) checks if 'num' is divisible by 2 (i.e., if it's even). If the condition is true, the string "even" is assigned to the 'result' variable. If the condition is false, the string "odd" is assigned to the 'result' variable.
Finally, we print a message indicating whether 'num' is even or odd using the 'result' variable.
The output of this program will be:
5 is an odd number.
The conditional operator provides a concise way to perform simple conditional assignments or evaluations. It can be used in place of an if-else statement when the expressions are simple and the code can be written in a single line.
Special operations
In addition to the operators we have discussed so far, C provides a few special operations that are commonly used in programming. Let’s take a few examples:
1. sizeof operator
The sizeof operator is used to determine the size (in bytes) of a variable or data type. It is commonly used when working with arrays, pointers, and dynamic memory allocation.
Example:
#include <stdio.h>
int main() {
int num;
float fnum;
char ch;
printf("Size of int: %lu bytes\n", sizeof(num));
printf("Size of float: %lu bytes\n", sizeof(fnum));
printf("Size of char: %lu byte\n", sizeof(ch));
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 byte
2. Comma operator
The comma operator (,) is used to separate multiple expressions in a single statement. The expressions are evaluated from left to right, and the value of the last expression is the result of the entire expression.
Example:
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3;
int result = (a++, b *= 2, c += 3);
printf("a = %d, b = %d, c = %d\n", a, b, c);
printf("result = %d\n", result);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
a = 2, b = 4, c = 6
result = 6
In this example, the comma operator is used to separate multiple expressions in a single statement. The expressions are evaluated from left to right, and the value of the last expression (c += 3) is assigned to the 'result' variable.
Note: These are just a couple of examples of special operations in C. There are other operations like the pointer arithmetic operators, the address-of operator (&), and the dereference operator (*) that are used when working with pointers and memory manipulation.
Frequently Asked Questions
What is the difference between the assignment operator (=) & the equality operator (==)?
The assignment operator (=) is used to assign a value to a variable, while the equality operator (==) is used to compare two values for equality.
How do logical operators (&&, ||) differ from bitwise operators (&, |)?
Logical operators (&&, ||) operate on Boolean values & return a Boolean result, while bitwise operators (&, |) operate on individual bits of integer values & return an integer result.
What is the purpose of the sizeof operator in C?
The sizeof operator is used to determine the size (in bytes) of a variable or data type. It is commonly used when working with arrays, pointers, & dynamic memory allocation.
Conclusion
In this article, we discussed the various types of operators & expressions in C. We explained arithmetic operators, assignment operators, logical operators, relational operators, bitwise operators, & special operations like sizeof & the comma operator.
You can also check out our other blogs on Code360.