A Comprehensive Guide on Operators in C
C was introduced by Dennis Ritchie in the 1970s. It’s a high-level programming language. Operators in C are special keywords that are essential for writing effective codes and working on operands. They are the substructure of the programming languages where the C language supports the built-in operators. C language assists numerous operators such as bitwise, logical, and arithmetic operators.
In this blog, you will get a comprehensive overview of operators in C, and their types, and will learn about arithmetic, logical, and bitwise operators in C with clear examples to help readers better understand.
What are Operators in C Programming?
It is easy to define an operator in C, an operator is a character that operates on variables and asks the interpreter to run mathematical, relational, and logical operations.
There are multiple operators that carry out an important function in conducting diverse tasks. These operators are crucial for programmers as they allow complicated algorithms and forming decisions, inside their code. The accessibility of a vast array of operators in C immensely helps programmers compose proficient and successful programs.
Programmers can write effective code by focusing on the working and the order of precedence in operators. Since operators allow the manipulation of codes in an understandable way, it becomes easier to write and read code. Check out this C and C++ course if you want to learn more about it.
Arithmetic Operators in C
It is an operator that runs arithmetic operations in a C program and allows the user to construct multiple mathematical equations and formulas. Addition (+), division (/), subtraction (-), and multiplication (*) are some mathematical operations supported in C programming.
The following code helps in illustrating the use of arithmetic operations step by step:
1. Addition (+)
This operator adds two operands in the C programming language.
Example:
#include <stdio.h>
int main() {
100%
Example:
int a = 10, b = 3; int sum = a + b;
// addition operator
printf("Sum = %d\n", sum);
return 0;
}
Output:
Sum = 13
2. Subtraction (-)
This operator subtracts second operands from the first in the C programming language.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
int difference = a - b; // subtraction operator
printf("Difference = %d\n", difference);
return 0;
}
Output:
Difference = 7
3. Multiplication (*)
This operator multiplies two operands in the C programming language.
Example:
#include <stdio.h>
int main() { int a = 5, b = 3;
int product = a * b;
// multiplication operator
printf("Product = %d\n", product);
return 0;
}
Output:
Product = 15
4. Division(/)
This operator divides the numerator by the denominator in the C programming language.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3; float quotient = (float) a / b;
// division operator
printf("Quotient = .2f\n", quotient);
return 0;
}
Output:
Quotient 3.33
Logical Operators in C
It is a character that connects more than two expressions. These operators are employed to compare the logical expression. So, it can return an output of true or false.
Only 3 logical operators are supported by C language, these are:
1. Logical AND (&&)
This operator returns the value as true, only if both conditions are true.
Example:
#include <stdio.h>
int main() { int a = 5, b = 10;
if (a > 0 && b > 0) { printf("Both a and b are positive. \n"); // this statement will be
executed
}
if (a > 0 && b < 0) {
printf("a is positive, but b is negative. \n"); // this statement
will not be executed
}
if (a < 0 && b < 0) { printf("Both a and b are negative.\n"); // this statement will not be executed
}
return 0;
Output:
Both a and b are positive
2. Logical OR (||)
This operator returns the value as true and checks if both two conditions are true. In case, only one condition is true then ‘’Logical OR’’ will still return the value as true.
Example:
#include <stdio.h>
int main()( int a 5, b = -10;
if (a > 0 11 b > 0) {
printf("At least one of a and b is positive. \n"); // this
statement will be executed if (a < 0 || b > 0) {
printf("b is positive, but a is negative. \n"); // this statement
will not be executed
}
if (a < 0 || b < 0) { printf("Both a and b are negative. \n"); // this statement will be
executed
}
return 0:
Output:
At Least one of a and b is positive.
Both a and b are negative
3. Logical NOT (!)
The logical NOT operator (!) refers to a unary operator that executes a logical negation on the given expression, returning True if the expression is False, and False if the expression is True.
Example:
#include <stdio.h>
int main() { int a = 5, b = 0;
if (la) { printf("a is zero. \n"); // this statement will not be executed
if (lb) { printf("b is zero. \n"); // this statement will be executed
if (! (a > 0)) { printf("a is not greater than zero. \n"); // this statement will not be executed
if (!(b < 0)) {
printf("b is not less than zero.\n"); // this statement will be
executed
return 0:
Output:
b is zero.
b is not less than zero
Bitwise Operations in C
In bitwise, the left and the right operands are the same. Bitwise operators also execute the shifting of bits from right to left. The bit value either shifts to left or right by operators in bitwise. The number of positions is specified by the right operand and the left operand defines the value that needs to be shifted.
Bitwise has numerous operators, let’s learn about a few of them:
- Bitwise AND (&): This type of operator always compares the second operand bit to the corresponding bit of the first operand.
- Bitwise OR (|): In this operator, if the corresponding bits of any of the operands is ‘1’ then it returns a Biglnt which has ‘1’ in every bit.
- Bitwise OR – XOR (^): In this operator, if the corresponding bits of any of the operands is ‘1’ then it returns a Biglnt whose has different numbers in every bit.
The AND, OR, and NOT operators in bitwise work as Logical AND, OR operators, but excluding bit-by-bit bitwise operators.
Let’s have a look at the Truth Table of bitwise to see how AND (&), OR (|), and XOR (^) operators work in C.
a | b | a&b | a/b | a^b |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
- The two bits ‘a’ & ‘b’ represents ‘0’ & ‘1’, but those two bits can have only one, either ‘1’ or ‘2.’
- In this table, the ‘&’ operator performs bitwise’ AND’. In simple words, Bitwise ‘AND’ returns the value 1, if a and b are also 1, otherwise it will be 0.
- In this table, A bitwise ‘OR’ performs ‘|’ operators. Simply put, it will return the value 1 if ‘a’ or ‘b’ is ‘1.’ Otherwise, it will be ‘0.’
- The bitwise ‘ XOR’ is performed by the ‘^’ operator. Simply put, it will return the value 1 if ‘a’ or ‘b’ is ‘1.’ Otherwise, it will be ‘0
Assignment Operators in C
Assignment operators are named in a way that reflects their functions as they are focused on assigning one or more values to a variable in a program. The right side operand and the left side operand in the assignment operator are known as the ‘value’. The value must be of the same data type as the variable on the left and the right side, else, an error will be raised by the compiler.
There are multiple types of assignment operators, let’s take a look at them:
- “=”
The “=” operator is the most basic assignment operator, applied to allocate the value from the right side to the variable on the left side.
- ‘+=”
This operator works with the combination of “=” and “+” operators. It adds the value first to the variable on the left to the right operand and then assigns the result or output to the left operand.
- “-=”
This operator works with the combination of “=” and “-” operators. It subtracts the value first to the variable on the left to the right operand and then assigns the result or output to the left operand.
- “*=”
This operator works with the combination of “=” and “*” operators. It multiples the value first to the variable on the left to the right operand and then assigns the result or output to the left operand.
- “/=”
This operator works with the combination of “=” and “/” operators. It divides the value first to the variable on the left side then moves to the right operand and then assigns the result or output to the left operand.
Now, let’s have a look at the examples that demonstrates the different assignment operators:
// C program to illustrate
// Functioning of Assignment operators
#include <stdio.h>
int main()
{
#include <stdio.h>
int main() {
// Assigning value 5 to a
// using "=" operator
int a = 5;
printf("Value of a is %d\n", a);
// Assigning value by adding 5 to a
// using "+=" operator
a += 5;
printf("Value of a is %d\n", a);
// Assigning value by subtracting 5 from a
// using "-=" operator
a -= 5;
printf("Value of a is %d\n", a);
// Assigning value by multiplying 5 to a
// using "*=" operator
a *= 5;
printf("Value of a is %d\n", a);
// Assigning value by dividing 5 from a
// using "/=" operator
a /= 5;
printf("Value of a is %d\n", a);
return 0;
}
Output:
Value of a is 5
Value of a is 10
Value of a is 5
Value of a is 25
Value of a is 5
Increment/Decrement Operator in C
C programming has basically two operators which can increment ++ and decrement — the value of a variable. The increment and decrement operators are used to increase or decrease the value of a variable by one. Increment and decrement operators are very useful operators that are generally used to minimize the calculation.
Operator | Definition |
++ | This operator is called the increment operator, which is used to increase the integer value by 1. |
– – | This operator is called the decrement operator, which is used to decrease the integer value by 1. |
This example demonstrates the working of increment and decrement operators:
Conclusion
Operators in C are very useful because they improve the code’s speed and efficiency. C supports a wide range of operators but the most common are bitwise, logical, and arithmetic operators. Operators in C programming require careful attention to details as the use of C is an important aspect of coding.