Monday, August 14, 2017

Different Type Operators in C


C is the most powerful programming language for its powerful built-in operators and functions. The purpose of using operators in program is manipulating variables. Without manipulating what can we do to build up program or big project ! Again without calculation we can’t solve the complex problem and to calculate we know what is the necessary of operators.

At least one operator we can make expression in C program. The variable works with operator to make an expression is called operand.

Example: x + y = 10;

Here has three two operators: + , = and two operands: x and y.

There are many operators like this in C. Those can be classified as below:
  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Increment and Decrement Operators
  • Bitwise Operators
Different Type Operators in C
We have already used ‘+’, ‘-’

Arithmetic Operators in C 

The arithmetic operators in C are +, -, /, * and %.

Operator Name Operator Sign Refers to Example
Plus + Add A + B
Minus - Subtract A - B
Division / Divide A / B
Multiplication * Multiply A * B
Modulus * % Remain of division A % B
Remember: The data type of operands must be same, otherwise operation will return error in result. Example, if we want to add two variables value1, value2 and value1 be integer type, then value2 must be integer type. This operation will return the result in integer type also.

If you want to operate in different data type in C, you can use casting. Casting in C is very easy. Just put required data type in braces before variable.

Example: The given two variables are int var1 and float var2. We want to divide var2 by var1. So we have to convert the data type of var1 into float. Then our program will be
#include <stdio.h>
int main()
{
    int var1;
    float var2, var3;

    var1 =3 , var2 = 22.5;
    var3 = var2 / (int)var1;

    printf("%f\n", var3);
    return 0;
}
***Modulus operator is an exclusive arithmetic operator in C. The operation by this operator return the remainder of two variables.

Example:
#include <stdio.h>
int main()
{
    int a, b, c;
    a = 20, b = 3;

    c = a % b;
    printf("%d\n",c);
    return 0;
}

Assignment Operators in C

Assignment operators refer to assign the value of operation to a declared variable.

We use this type operators in Linear equation.
Example: x = 1+ y

Here = is the assignment operator. The value of addition of 1 and y will assign to x by using an assignment (=) operator.

Without this usual operator, there are more assignment operator in C. This is called shorthand operator. We can two operation with this operators.

Example: x = x + 10 can be written in x +=10.

Now look at the chart.

Statement of Shorthand Operators Statement of Simple Assignment Operators
x+= y x = x + y
x-= y x = x - y
x*= y x = x * y
X/= y x = x / y
X%= y x = x % y

Relational Operators in C 

Relational operators return the result by comparing multiple variables. If we want to compare of values of two variables that what is the greater or smaller? Are those equal or not? Etc, what can we do in C then?

Relational Operators Meaning Expressions
> Greater than X > Y
>= Greater than or equal to X >= Y
< Less than X < Y
<= Less than or equal to X >= Y
==Equal to X == Y
!= Not equal to X != Y
You can compare variable with constant like integer number.

Example: a > 10, a < x = 10 etc.

Now run and observe the following program.
#include <stdio.h>
int main()
{
    int x , y;
    x = 10, y = 10;
    printf("%d\n", x>y);
    printf("%d\n", x==y);
    return 0;
}
Output:
0
1

The first output is 0 because the result of x>y is false and the second output is 1 because the result of x ==y is true. In binary 0 for false and 1 for true.

Logical Operators in C 

Logical operators are the advance operators to reduce the complexity of the expression with relational operator.

If we want to compare and test about more than two variables with relational operator, then it may be complex. To reduce the complexity and make accuracy the logical operators are built in C.

Example:
The given values of variable a, b & c are 10, 11, 12 and we want to compare : is a greater than b and is the previous condition and c is equal to 10 is true? If we want to test in one expression then it is difficult to define by only relational operators. But it would be easy to define with logical operator and relational operator.

Example: a>b && c==10;

This expression will return 0 (false result). Here && is a logical operator and >, == are two relational operators. The logical operator && is called AND. It adds two compare. It returns 1 (true) if and only two operands be true, otherwise false. The another two logical operators are OR, NOT.

Logical Operator Sign Returns Example
AND && True if and only if two operands be true, otherwise false. A && B
OR || False if and only if two operands be false, otherwise true. A || B
NOT ! The opposite of operand. !A
Now run and observe the following program.
#include <stdio.h>
int main()
{
    int x , y, z;
    x = 10, y = 5, z = 5;

    printf("%d\n", x && (y+z));
    printf("%d\n", x || z);
    printf("%d\n", !(x && (y+z)));
    printf("%d\n", (x<y)== (z<x));

    return 0;
}
Output:
1
1
0
0

Increment and Decrement Operators in C 

Increment and decrement operators are two major operators in C programming as well as other programming languages. Increment operator increase 1 with the current value of variable after once operation and decrement is opposite of increment.

Suppose the current value of variable value is 2, if we use increment operator in value then the result after operation will be 3 and if the current value of variable value2 be 4 and we use decrement operator in value2 then the result after once operation will be 3.

Increment operator is defined by ++ before or after variable to increase one and decrement operator is defined by – before or after variable to decrease one.

So the required statements will be:

++value or value++ and --value2 or value2--

Here operators before are called prefix operators and operators after are called postfix operators.
So prefix operators are used in ++value and --value2 and postfix operators are used in value++ and value2--.
Question: What is different between postfix and prefix operators in C?

Ans: In which variable use prefix operator, the value of this increased or decreased variable is returned to variable and then follow the next operation or statement but in postfix, he value of this increased or decreased variable is not returned to variable rather return it the next operation or statement.

Run and observe the following program.
#include <stdio.h>
int main()
{
    int m;
    m = 5;
    printf("%d\n",m);
    printf("%d\n",m++);
    printf("%d\n",m);
    printf("%d\n",++m);
    printf("%d\n",m);

    return 0;
}
Output:
5
5
6
7
7

Bitwise operators in C for manipulating of value of variables in bit level 0 and 1. Operation by bitwise operators return the value in default data type but the function of this operator works the converted binary digit.

Bitwise Operators Name Expressions
& Bitwise AND A & B
| Bitwise OR A | B
^ Bitwise Exclusive OR A ^ B
<< Bitwise Left Shift A << B
>> Bitwise Right Shift A >> B
Now run and observe the following program.
#include <stdio.h>
int main()
{
    int a, b;
    a = 2, b = 3;

    printf("%d\n", a & b);
    printf("%d\n", a | b);
    printf("%d\n", a ^ b);
    printf("%d\n", a << b);
    printf("%d\n", a >> b);
}
Output:
2
3
1
16
0

Looking for more effecive Course?

C Programming For Beginners by Udemy icon
$15 (Original $145)
30-Day Money-Back Guarantee
Buy
Includes:
  • 6 hours on-demand video
  • 1 Article
  • 2 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion