Basics of C Programming for Beginners- Operators in C (Part-IV)
Operators are symbols in the C programming language that stand for operations that can be executed on one or more operands. These are the fundamental elements of C programming. We will learn about all of C’s built-in operations with in this post.
Before moving forward with Operators in C language, we recommend you to read the three parts of the preceding post if you haven’t already.
- C Programming language Introduction
- Variables in C Programming language
- Data Types in C Programming Language
Introduction to Operators in C Language
- The C language supports a rich set of built-in operators.
- An operator is a symbol that tells the compiler to perform a certain operation (arithmetic, comparison, etc.) using the values provided along with the operator.
Type of C Operators
C operators fall into the following categories:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
- Special operators
Before going into the details of each of these operator types, it is important to understand these categories of operators as well.
- Unary operators : Unary operators operate on a single input (or single value)
- Binary operators: Binary operators operate on two input values
- Ternary operators : The Ternary operator (
?:
) operates on three input values
Let’s explore each of these operator types individually, identifying if they are Ternary, Binary, or Unary operators with samples code.
1. Arithmetic Operations in C
The arithmetic operators in C language help a user perform the mathematical operations as well as the arithmetic operations in a program, such as subtraction (-), addition (+), division (/), multiplication (*), the remainder of division (%), decrement (–), increment (++).
Binary Arithmetic Operators
Example:
#include <stdio.h>
int main() {
int a = 10;
int b = 5;
// Addition
int sum = a + b;
// Subtraction
int difference = a - b;
// Multiplication
int product = a * b;
// Division
int quotient = a / b;
// Modulo
int remainder = a % b;
// Print the results
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
Output:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Unary Arithmetic Operators
Prefix and Postfix Increment/ Decrement Operators
Prefix
— When we use the operator before the available variable in a program as a prefix, it is known as a prefix operator.
In a prefix increment(++) operator, the program first adds 1 and then assigns the resultant value to that variable.
While in a prefix decrement( — ) operator, the program first subtracts 1 and then assigns the resultant value to the variable. For example, ++a and — a
Example
#include <stdio.h>
int main() {
int a = 10, b = 20, c, d;
/*
Using prefix increment operator
*/
printf("Incrementing value of a = %d \n", ++a);
/*
Using prefix decrement operator
*/
printf("Decrementing value of b = %d \n", --b);
return 0;
}
Output:
Incrementing value of a = 11
Decrementing value of b = 19
Postfix
— When we use the operator after the available variable in a program as a postfix, it is known as a postfix operator.
In a postfix increment (++) operator, the program first assigns the value to the variable, then adds 1, and then assigns the resultant value.
While in a postfix decrement( — ) operator, the program first assigns the value to the variable, then subtracts 1, and then assigns the resultant value. For example, a++ and a — .
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20, c, d;
// first print value of a, then decrement a
printf("Decrementing value of a = %d \n", a--);
printf("Value of a = %d \n", a);
// first print value of b, then increment b
printf("Incrementing value of b = %d \n", b++);
printf("Value of b = %d \n", b);
return 0;
}
output:
Decrementing value of a = 10
Value of a = 9
Incrementing value of b = 20
Value of b = 21
2. Relational operators in C
The relational operators in C are used to compare the values available for two given operands(inputs). All these operators are binary operators that return true or false values as the result of comparison
Examples:
#include <stdio.h>
int main() {
int a = 5, b = 3;
int examScore = 75;
int passingScore = 60;
if (a > b) {
printf("a is greater than b \n");
}
if (a < b) {
printf("a is not greater than b \n");
}
if (a == b) {
printf("a is equal to b \n");
}
if (a != b) {
printf("a is not equal to b \n");
}
if (examScore >= passingScore) {
printf("Congratulations! You have passed the exam.\n");
} else {
printf("Sorry, you have not passed the exam.\n");
}
return 0;
}
output:
a is greater than b
a is not equal to b
Congratulations! You have passed the exam.
3. Logical Operators
Logical Operators in C are essential for making decisions based on multiple conditions. They allow us to combine and test these conditions to decide the flow of a program
- With the AND operator, only if both operands are true, the result is true.
- With the OR operator, if a single operand is true, then the result will be true.
- The NOT operator changes true to false, and false to true.
Example:
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
if (a == 10 && b == 20) {
printf("Both conditions are true.\n");
}
if (a == 10 || b == 30) {
printf("At least one condition is true.\n");
}
if (!(a == 20)) {
printf("The condition is false.\n");
}
return 0;
}
output:
Both conditions are true.
At least one condition is true.
The condition is false.
4. Bitwise Operators in C
Bitwise operators are used to manipulate bits in various different ways. They are equivalent to how we use mathmatical operations like (+, -, /, *) among numbers, similarly we use bitwise operators like (|, &, ^, <<, >>, ~) among bits.
6 Bitwise operators in C
There are 6 bitwise operators in C language. They are
- AND (&)
- OR (|)
- XOR (^)
- COMPLEMENT (~)
- Left Shift (<<)
- Right Shift (>>)
Example:
#include <stdio.h>
main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}
output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
5. Assignment Operators in C
We use the assignment operators in c for assigning any value to the given variable.
In C, there are 11 assignment operators :
Example:
// C program to demonstrate
// working of Assignment operators
#include <stdio.h>
int main()
{
// Assigning value 10 to a
// using "=" operator
int a = 10;
printf("Value of a is %d\n", a);
// Assigning value by adding 10 to a
// using "+=" operator
a += 10;
printf("Value of a is %d\n", a);
// Assigning value by subtracting 10 from a
// using "-=" operator
a -= 10;
printf("Value of a is %d\n", a);
// Assigning value by multiplying 10 to a
// using "*=" operator
a *= 10;
printf("Value of a is %d\n", a);
// Assigning value by dividing 10 from a
// using "/=" operator
a /= 10;
printf("Value of a is %d\n", a);
return 0;
}
Output:
Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10
6. Conditional Operators (or Ternary Operator)
The conditional operator is the only ternary operator in C++.
The basic syntax for using ternary operator is:
(Expression1)? Expression2 : Expression3;
Here is how it works:
- The question mark
?
in the syntax represents the if part. - The first expression (expression 1) returns either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3)
- If (expression 1) returns true then the (expression 2) is executed.
- If (expression 1) returns false then the expression on the right side of
:
i.e (expression 3) is execute
#include <stdio.h>
int main() {
int a = 20, b = 20, result;
/* Using ternary operator
- If a == b then store a+b in result
- otherwise store a-b in result
*/
result = (a==b)?(a+b):(a-b);
printf("result = %d",result);
return 0;
}
output:
result = 40
7. Other C Operators
Apart from arithmetic, relational, logical, assignment, etc. operators, C language uses some other operators such as:
sizeof
operator&
operator*
operator- The
.
(dot) and->
(arrow) operators []
operator, etc.
Example of Other C Operators
// C Program to demonstrate the use of Misc operators
#include <stdio.h>
int main()
{
// integer variable
int num = 10;
int* add_of_num = #
printf("sizeof(num) = %d bytes\n", sizeof(num));
printf("&num = %p\n", &num);
printf("*add_of_num = %d\n", *add_of_num);
printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
printf("(float)num = %f\n", (float)num);
return 0;
}
Output
sizeof(num) = 4 bytes
&num = 0x7ffe2b7bdf8c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000
Conclusion
In this article, the points we learned about the operator are as follows:
- Different types of operation in C.
- There are six types of operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators.
- Operators can also be of type Unary, Binary, and Ternary according to the number of operand/value they are using.