What are Operators in C?

Welcome folks, in this particular module we are going to talk about what are Operators in C, the most important and easy topic in this particular series, Operators is something which we will not see only in programming, but we also tackle this in our daily life too, we are doing the basic calculation from our childhood, i.e., addition, subtraction, multiplication, division, etc. these are all operations only called as mathematical calculation.

Let’s move into this module and see in more detail about the same.

What are Operators in C?

When you want to perform any type of mathematical operation, then you have to use operators, it is a special type of function, which is used with one or more arguments and in return produces a new value as a result. It is a type of symbol that tells the compiler to perform specific calculations or mathematical and logical manipulations. All the operators are available in the C library.

What are Operators in C

Operators are used to performing various different types of operations in different variables and constants. For example: Addition (+), Subtraction (-), Multiplication (*), division (/), etc. operators.

How many different types of Operators are available in C programming?

There are many types of operators available in the C library and that are used for different purposes.

  1. Assignment Operator
  2. Relational Operator
  3. Mathematical Operator
  4. Logical Operator
  5. Bitwise Operator
  6. Unary Operator
  7. Ternary Operator
  8. Shift Operator
  9. Comma Operator

Let’s see all these operators in a bit more detail and understand their uses.

Assignment operator

The assignment operator in C is used to assign the right value to the left variable. It is resembled by =, For example: int num = 30;

Here, 30 is the value that is assigned to the variable num with the help of the assignment operator, and therefore now the variable num is holding the value 30 in it.

Relational Operator

It is used to establish some type of relationship between two or more operands, i.e., it is used for the comparison of values of different variables. Some relational Operators are:

  • Less than (<): It is used to compare whether the particular value is less than another value or not. For example: if (10 < 11)
  • greater than (>): It is used to compare whether the particular value is greater than another value or not. For example: if (11 > 10)
  • less than or equal to (<=): It is used to compare whether the particular value is less than or equal to that other value or not. For example: if (10 <= 11)
  • greater than or equal to (>=): It is used to compare whether the particular value is greater than or equal to that other value or not. For example: if (11 >= 10)
  • equivalent (==): It is used to compare whether the two values are equal or not. For example: if (10 == 10)
  • not equivalent (!=): It used to compare whether the two values are not equal to one another or not. For example: if (10 != 11)

So, these were the relational operators available in C Programming. In all the above examples, we have used if ( condition ), this is the conditional operator which we will cover in the coming modules, as of now you should only know the meaning and their particular uses.

Mathematical Operator

It is also called as arithmetic operator that is used to perform different types of mathematic calculation like Addition ( + ), Subtraction ( – ), Multiplication ( * ), division ( / ), Modulus ( % ) , this modulus operator is used to calculate the remainder.

For example:

int a = 10;
int b = 20;
int add;
add = a + b;

In the above example, we are adding the two values which are stored in variables a and b respectively, and then the addition of both the variables is stored in another variable add. O add will be holding the value of 30 i.e., 10 + 20, similarly you can perform different calculations with different operators.

Logical Operators

These types of operators are used to compare two or more expressions. These are represented by: AND ( && ) called as logical and, OR ( || ) called logical or.

  • AND is valid when both the expressions are true, for example, if ( 10 < 15 && 20 > 11), so this statement results to be true because both the conditions are true.
  • OR is valid when any one of the expressions is true, for example, if ( 10 < 15 || 20 < 11), so this statement also results to be true because one of the conditions is true.

Bitwise Operator

These operators are used to play with the individual bit of a number. It doesn’t work with a floating-point number.

  • Bitwise AND Operator (&),
  • Bitwise OR Operator (|),
  • Bitwise XOR Operator (^),
  • Bitwise NOT Operator (~).

Shift Operator

They are used to shift the bits of a variable. It is of three types:

  • Left Shift (<<),
  • Right Shift (>>),
  • Unsigned Right Shift (>>>).

Unary Operator

These are the operators that work with only one variable.
There are two main unary operators:

  • Increment operator (++)
  • Decrement Operator (–)

Some other unary operators are the address of (&), new, and delete. We use these operators with loops and also in pointers which we will cover in the coping modules.

Ternary Operator

The Ternary, if-else ( ? : ) operator, is the operator that has three operands. For example:

int a=5;
a>2?printf(“true”):printf(“false”);

In the above example, we have declared the variable a as 5, and we are checking the condition and if that condition is true then true will be printed otherwise else will be printed.

Comma Operator

It is used to separate variables and expressions. For example:
int a, b, c;
In this example, we have declared 3 variables a, b, and c of type int and separated by comma operator.

So, this was all about the types of operators and their uses for different purposes.

What is the contrast between Assignment ( = ) and Equivalent ( == ) Operator?

Generally, people make mistakes with these two operators, they consider these operators to be the same, but they have different meanings in programming. Let’s see the differences.

Assignment Operator

= is an Assignment operator in C programming languages, it always operates with two operands. = assigns the value of right-side expressions or variables to the left-side variable. For example:

x = ( a+b+c);
y = x;

Here, when the first expression takes the value of ( a+b+c ) will be assigned into x, and in the second statement, y = x; values of variable x will be assigned into y.

Equivalent Operator

== is an equal to operator in C, It is a binary operator which operates on two operands. == compares value of left and right side expressions, return 1 if they are equal, otherwise it will return 0. For example:

int x, y ;
x = 10 ;
y = 10;
if( x == y )
{
printf("True");
}
else
{
printf("False");
}

When the expression x == y evaluates, it will return 1 ( it means the condition is true ), and “True” will print.

So, it is clear now, both are not the same, = is an assignment operator that is used to assign the value to the variable or expression, while == is equal to the operator and that is a relation operator used for comparison whether both values are equal or not (to compare the value of both left and right side operands).

I hope you all are cleared with this concept and want to implement the same, also must be excited about the upcoming modules.

Until then, Stay connected, Happy Learning, and Keep Coding!