Operators in C Programming: A
Comprehensive Guide
Arithmetic Operators
Arithmetic operators perform mathematical calculations. Here's a breakdown:
Arithmetic Operators in C Programming
Modulus Increment
Addition Subtraction
Arithmetic
Operators
Division Multiplication
Decrement
Key Interview Points:
• Integer Division: In C, dividing two integers results in an integer quotient. The
fractional part is truncated (not rounded). For example, 5 / 2 results in 2.
• Modulus Operator: The modulus operator (%) gives the remainder of a division. It can
only be used with integer operands. 7 % 3 results in 1.
• Increment/Decrement Operators: Understand the difference between prefix (++a, --a
) and postfix (a++, a--) increment/decrement.
• Prefix: The value of the variable is incremented/decremented before it is used
in the expression.
• Postfix: The value of the variable is incremented/decremented after it is used in
the expression.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 2;
float c = 5.0, d = 2.0;
printf("a + b = %d\n", a + b); // Output: a + b = 7
printf("a - b = %d\n", a - b); // Output: a - b = 3
printf("a * b = %d\n", a * b); // Output: a * b = 10
printf("a / b = %d\n", a / b); // Output: a / b = 2 (Integer division)
printf("c / d = %f\n", c / d); // Output: c / d = 2.500000 (Floating-point
division)
printf("a %% b = %d\n", a % b); // Output: a % b = 1
int x = 5;
printf("x++ = %d\n", x++); // Output: x++ = 5 (x is incremented after
printing)
printf("x = %d\n", x); // Output: x = 6
int y = 5;
printf("++y = %d\n", ++y); // Output: ++y = 6 (y is incremented before
printing)
return 0;
}
Relational Operators
Relational operators compare two values and return a boolean result (true or false). In C, true
is represented by any non-zero value (typically 1), and false is represented by 0.
Comparison operators
Less or equal Equal
Checks if one value is Checks if two values are
less than or equal to equal. |
another. |
Greater or equal Not equal
Checks if one value is Checks if two values are
greater than or equal to not equal. |
another. |
Less than Greater than
Checks if one value is Checks if one value is
less than another. | greater than another. |
Key Interview Points:
• Equality vs. Assignment: A common mistake is using the assignment operator (=)
instead of the equality operator (==) in conditional statements. if (a = 5) will always
evaluate to true (because a is assigned the value 5, which is non-zero), whereas if (a
== 5) will only be true if a is equal to 5.
• Floating-Point Comparisons: Directly comparing floating-point numbers for equality
using == can be problematic due to potential rounding errors. Instead, check if the
absolute difference between the two numbers is less than a small tolerance value
(epsilon).
Example:
#include <stdio.h>
#include <stdbool.h> // Include for using bool data type
int main() {
int a = 5, b = 10;
float x = 2.000001, y = 2.000002;
float epsilon = 0.00001;
printf("a == b: %d\n", a == b); // Output: a == b: 0 (false)
printf("a != b: %d\n", a != b); // Output: a != b: 1 (true)
printf("a > b: %d\n", a > b); // Output: a > b: 0 (false)
printf("a < b: %d\n", a < b); // Output: a < b: 1 (true)
if (x == y) {
printf("x and y are equal\n");
} else {
printf("x and y are not equal\n"); // This might be printed due to floating
point precision
}
if (fabs(x - y) < epsilon) {
printf("x and y are approximately equal\n"); // More reliable comparison
} else {
printf("x and y are not approximately equal\n");
}
return 0;
}
Logical Operators
Logical operators are fundamental tools in programming for creating complex boolean
expressions. They allow you to combine multiple conditions and control the flow of your
program based on the truthiness or falsiness of these combined conditions.
Logical AND (&&)
The && operator, also known as the logical AND operator, returns true if and only if both
operands are true. If either operand is false, the entire expression evaluates to false.
| Operand A | Operand B | A && B |
| :-------- | :-------- | :----- |
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
Example:
int age = 25;
boolean hasLicense = true;
if (age >= 16 && hasLicense) {
[Link]("Eligible to drive.");
} else {
[Link]("Not eligible to drive.");
}
In this example, the code checks if the age is greater than or equal to 16 and if the person
hasLicense. Only if both conditions are true will the message "Eligible to drive." be printed.
Logical OR (||)
The || operator, also known as the logical OR operator, returns true if at least one of the
operands is true. The entire expression evaluates to false only if both operands are false.
| Operand A | Operand B | A || B |
| :-------- | :-------- | :------- |
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Example:
int temperature = 15;
boolean isRaining = true;
if (temperature < 20 || isRaining) {
[Link]("Wear a jacket.");
} else {
[Link]("No jacket needed.");
}
In this example, the code checks if the temperature is less than 20 or if it isRaining. If either
condition is true, the message "Wear a jacket." will be printed.
These logical operators are essential for creating complex conditional statements and
controlling the flow of your programs based on multiple factors. Understanding their
behavior is crucial for writing effective and reliable code.
For more content: Click Here