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

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 OperatorsExample
%Evaluates remainder after divisionB%A=0
/Divides the numerator by denominatorB/A=2
*Multiplies two numbersA*B=8
+Adds two numbersA+B=6
Subtracts the second operand from the first operandA-B=2

Now let’s move on to our first example using an operator in C.

Example of the Arithmetic operator

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:

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.

OperatorsDescription
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

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,

OperatorMeaning of OperatorExample
&&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:

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.

OperatorExampleMeaning
=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%=5X=x%5

 

Example of Assignment Operator

 

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.

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.

Similarly, we can use the following line of code to decrement the value of x by 1.

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

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:

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.”

Now, Its time to move onto our first example using Conditional Operator. Following example demonstrates the use of the ternary operator.

Example of Conditional Operator

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.

OperatorsDescriptionMeaning
&Bitwise AndThe output is 1 if both the corresponding bits of the operator is 1, otherwise 0.
|Bitwise ORThe output is 1 if any one of the bit is 1 or both the bit is 1, otherwise 0.
^Bitwise XORThe output is 1 if the corresponding bits of the operand are opposite. Otherwise 0.
~ComplementIt is a unary operator. It changes the bit of the operator to its opposite bit.
>> Bitwise Right ShiftIt moves all bits towards the right by a certain number of bits which can be specified.
<< Bitwise Left ShiftIt 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

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.

SHARE C Operators

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Share