C Operators
Do know that we can make the programs perform the basic arithmetic operations as per our wish. This topic Operators in C will provide us with an exciting privilege to manipulate variables and expression.C Operators are the symbols which instruct the compiler to perform a certain mathematical or logical operation. They are the foundation of any programming language. Operators manipulate variables and expressions. As C Language supports a rich set of built-in datatypes, there are various operators used to perform different operations. C Language provides the following types of operators –
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment operators
- Conditional operators
- Bitwise operators
Contents
What is the Arithmetic Operators?
Arithmetic operators are used for performing arithmetic operations on constant and variables. In other words, they are used to perform operations such as addition, subtraction, multiplication on numerical values such as variables, constants etc. The table given below illustrates all the basic operators with their respective uses. Let us assume that A holds value 2 and B holds value 4 which is used an evaluated in each of the operations below.
Operators | Meaning of Operators | Example |
% | Evaluates remainder after division | B%A=0 |
/ | Divides the numerator by denominator | B/A=2 |
* | Multiplies two numbers | A*B=8 |
+ | Adds two numbers | A+B=6 |
– | Subtracts the second operand from the first operand | A-B=2 |
Now let’s move on to our first example using an operator in C.
Example of the Arithmetic operator
#include <stdio.h> int main() { int r1, r2,mul,sum, div, rem, sub; printf("Enter the First number:"); scanf("%d", &r1); printf("Enter the secod number:"); scanf("%d", &r2); sum=r1+r2; printf("The sum of given two numbers is:\n",sum); mul=r1*r2; printf("The multiplication of two numbers is:\n", mul); div = r1/r2; printf("The division of two numbers are:\n", div); rem=r1%r2; printf("The remainder when a=%d divided by b=%d is:\n", rem); sub=r1-r2; printf("The subtration of rwo numbers are:\n", sub); }
The program above performs basic operations that can be done on C language. The program above takes two input from the user and displays its result by the use of different operators. Another key point here is that the operators we use can perform their respective functions explicitly. In the above program, the operators +, -, * , / , % perform their respectib=ve functions.
The above figure is the output in the console of the program above. Here the program asks for two inputs from the user and displays the output. Therefore, Arithmetic operator helps to manipulate and change the given expressions or variables as per our desire.
For exploring more examples of Arithmetic Operators visit following links:
- Illustrating the use of arithmetic operators in floating point number.
- To input 4 integer number and calculate the sum of digits in the given number.
- Printing the reverse number of a given four-digit number.
What are the Relational Operators?
Relational operators provide a very exciting feature to check the relationship between two operands. Furthermore, they are used to compare the two values of the same data type. They are also known as comparison operators. The result of any relational operator is equal and associativity is left to right.
Following is a table including all the operators and their respective functions.
Operators | Description |
< | Tests if operand at left is Less than operand at right |
<= | Tests if operand at Left is Less than and equals to operand at right |
> | Tests if operand at left is Greater than operand at right |
>= | Tests if operand at left is Greater than and equals to operand at right |
== | Tests if two operands are equal |
!= | Tests if two operands are not equal |
A very simple program is illustrated below. This program takes in an input from the user and checks whether the number consists of either positive or negative or zero value in it.
Example of Relational operator
#include<stdio.h> int main(){ int n; printf("Enter a number: "); scanf("%d", &n); if(n>0){ printf("%d is positive.", n); } else if (n< 0) { printf("%d is negative.", n); } else if(n==0){ printf("%d is zero."); } }
When the code above is compiled the output of the program is displayed in the console as below.
What are Logical Operators?
To begin with the Logical Operator, you have to understand the concept of true and false and the relationship between them. If you have two conditions and if you want to combine those two conditions then you can make use of the Logical Operators. Logical operators are used for combining two or more relational expression and to evaluate single value as true or false. Logical operators are mostly used in the process of decision making in C in C Language supports three different logical operators. They are represented in the table below. let us suppose that a=0 and b=1,
Operator | Meaning of Operator | Example |
&& | Logical AND | (a && b)==false |
|| | Logical OR | (a||b)==true |
! | Logical NOT | (!a)==true |
The table above explicitly defines the Logical operators and their respective functions. With these things in mind, it makes us easy to implement these operators in our program efficiently.
Now, let’s move onto our first program using logical operators.
Example of Logical operators:
#include<stdio.h> int main(){ int x,y, dif; printf("Enter the first number:"); scanf("%d", &x); printf("Enter the second numebr:"); scanf("%d", &y); if(x<y || x==0){ dif=y-x; printf("The difference between two numbers is: %d", dif); } else if(x>y || y==0){ dif=x-y; printf("The difference between two numbers is: %d", dif); } else if(x ==y || (x!=0 && y!=0)){ printf("Both numbers are equal but not 0"); } else if(x!=0 && y!=0){ printf("Both numbers are zero"); } }
In the program above, the example demonstrates the use of logical operators in our program. This will give us a clear understanding of the logical operators for combining two conditions. Here we see the use of operators in our program according to the nature of our program.
The output of the program above is displayed as in the figure below.
What is the Assignment Operator?
Do you know about the fact that some of our expressions on C can be minimized? We can perform such an act by using Assignment Operators. For example, (p+=q) can be written as (p=p+q). This helps to minimize the length of our program and makes it more readable. Assignment operators are the only type of operator which can be overloaded but cannot be inherited.
Furthermore, we can use the assignment operator to simply assign a value to a variable. For example, (x=10) indicates that we have assigned the value 10 to the variable x. This operator is used to assign the value in right to the variable in left.
Now let’s see further examples illustrating the use of the Assignment Operator to gain a much deeper understanding of the assignment operator.
Operator | Example | Meaning |
= | X=10; | X=10 |
+= | X+=10; | X=x+10 |
-= | x-=5; | X=x+5 |
*= | X*=2; | X=x-5 |
/= | x/=2; | X=x/2 |
%= | X%=5 | X=x%5 |
Example of Assignment Operator
//Demonstration of the use of assignment operator #include<stdio.h> int main(){ //assigning a random value to x by using '=' operator int x=40; printf("The value of x is: %d\n", x); //using operator '+=' to modify the value of x x += 10; printf("The new value of x is: %d\n", x); //using an operator '-=' to modify the value of x x-=5; printf("The new value of x: is %d\n", x); //using an operator '*=' to modify the value of x x*=2; printf("The new value of x: is %d\n", x); //using an operator '/=' to modify the value of x x/=5; printf("The new value of x: is %d\n", x); //using an operator '%=' to modify the value of x x%=2; printf("The new value of x: is %d\n", x); }
The above program illustrates the use of the assignment operator to change and modify the value of the variable in our program. Here we see that with the help of the assignment operator we can make our codes simple and easily debuggable.
When the above source code is compiled the result will be displayed as below:
What is the Increment and Decrement Operator?
In this section, we are going to learn what are Increment and Decrement Operators in C. Increment operator increases the value of the operand by 1 and Decrement operator decreases the value of operand by 1.
By the virtue of the topics, we have gone through before will provide us with an idea to perform increment and decrement of operators. The common approach to implement increment and decrement that switches on to our mind will be using arithmetic operators. for example, for increasing the value of x by 1 we might write the code as below.
x=x+1;//simple assignment
The above code can be replaced by using an increment operator to obtain the same result. we use ++ operator to increase the value of an operator by 1. i.e. following code works the same.
++x;
Similarly, we can use the following line of code to decrement the value of x by 1.
--x;
To make you more clear about the concept and implementation of increment and decrement operators an example is presented below.
Example of Increment and Decrement Operator
#include<stdio.h> int main(){ int a=15, b=20; float x=3, y=34; printf("The value of the variable after incrementin is: %d\n", ++a); printf("The value of the variable after incrementing is: %f\n", ++x); printf("The value of the variable after decrementin is: %d\n", --b); printf("The value of the variable after decrementin is: %f\n", --y); }
The above program demonstrates the use of increment and decrement operators in an integer and float datatypes. When the above code has compiled the result obtained is displayed as below.
What is Conditional Operator?
Conditional Operators are the most powerful operators in Programming Languages like C, C++, Java, Python, PHP, and javascript. They are also known as Ternary operator as they have 3 operands in it. The ternary operator is used in C Language to construct the conditional expressions of the form:
exp1 ? exp2 : exp3
The working principle of ternary operators are as follows:
- First, exp1 is evaluated. If it is true then exp2 is evaluated and becomes the result of the expression.
- If exp1 is false then exp3 is evaluated and becomes the result of the expression i.e. only one expression either exp2 or exp3 is evaluated.
In a simple way, The above points can be summarised as “If exp1 is true then exp2 is executed else exp3 is executed.”
Example of Conditional Operator
#include<stdio.h> int main(){ int x; printf("Enter your country code: "); scanf("%d", &x); x==+91?printf("You are Indian"):printf("You are not Indian"); }
The above program illustrates the use of the Ternary Operator in a very practical manner. In the above program, If the user enters a valid number then the first expression in executed else another statement is executed.
The output of the above program when you enter the right country code then the output is displayed as below:
The output of the above program when you enter the wrong country code then the output is displayed as below:
Here, we can now say that the use of the Ternary operator truly makes our program efficient. If the same program was written without the Ternary operator then it could have consumed much spaces and time to write. Hence, the use of the Ternary operator makes the program much concise and understandable. So, In a programming practice ternary operator’s use is highly encouraged.
What are the Bitwise Operators?
Have you ever wondered how a computer takes input and processes them? Every operation that is performed at the back end of the program is performed in its corresponding binary forms. Similarly, Bitwise operators are the operators that operate on a binary form of value. Bitwise operators are used in programming the processor. The Bitwise operators first convert a decimal value into a binary value and then applies the operators in it. In processor, Mathematical operations like addition, subtraction, multiplication, and division are done using bitwise operators. This activity makes processing faster and saves power.
Following is a list of bitwise operators and which are used to perform bit-level operations. Their description and functionalities will give you a clear overview of Bitwise operators.
Operators | Description | Meaning |
& | Bitwise And | The output is 1 if both the corresponding bits of the operator is 1, otherwise 0. |
| | Bitwise OR | The output is 1 if any one of the bit is 1 or both the bit is 1, otherwise 0. |
^ | Bitwise XOR | The output is 1 if the corresponding bits of the operand are opposite. Otherwise 0. |
~ | Complement | It is a unary operator. It changes the bit of the operator to its opposite bit. |
>> | Bitwise Right Shift | It moves all bits towards the right by a certain number of bits which can be specified. |
<< | Bitwise Left Shift | It moves all bits towards the left by a certain number of bits which can be specified. |
Now let’s move onto our first example implementing the Bitwise Operators.
Example of Bitwise Operator
#include<stdio.h> int main(){ int a=5, b=20, c; c=a&b; printf("Bitwise AND= %d\n",c); c=a|b; printf("Bitwise OR= %d\n",c); c=a^b; printf("Bitwise XOR= %d\n",c); c=~a; printf("Bitwise complement= %d\n",c); c=a>>2; printf("Bitwise right shift= %d\n",c); c=a<<1; printf("Bitwise left shift= %d\n",c); }
The above program demonstrates the use of Bitwise Operators. The picture below displays the output obtained by executing the program above.
By all means, it is evident from all topic that the use of operator will benefit the programmer in so many ways. It helps to increase efficiency as well as the readability of the program.