Operators in C/C++ language,you must know

Operator in c

Operators are the backbone of any programming language. So C/C++ programming language is incomplete without the operators. The C/C++ programming language support mainly three types of operators unary operators, binary operators, and ternary operator (Conditional-expression operator).

Operators in C programming

 

 

Operators in C/C++ programming gives the direction to the compiler to perform the specific task. C language has a lot of operators to perform mathematical or logical operations. For example, (+) is an operator that is used for adding two numbers.

Let’s take an example to understand the working

Suppose I have written a statement a = a-b;

Then what would happen?

The compiler understands the ‘-’ instruction and subtract the value of ‘b’ from ‘a’ and store the result into ‘a’.

 

In “C” language, operators are divided into the following categories:

Arithmetic operator:

Arithmetic operators are used to performs a mathematical operation like addition multiplication etc. Let’s assume, ‘a’ is 8 and ‘b’ is 4.

There are following Arithmetic operators in C/C++.

+ Adds operands a+b=12
Subtracts second operand from the first a-b=4
* Multiplies both operands a*b=32
/ Divides numerator by denominator. a/b=2
% Modulus Operator returns the remainder after an integer division. a%b=0

 

Generally, we are familiar with all arithmetic operators but sometimes we get confused with division and modulo division operator. So before going into depth, I want to clear this confusion.

Let see a program where I am using division and modulo division operator. This program also describes the difference between division and modulo division operator.

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int data= 0;

    // Perform Division
    data =7/3;
    printf("\n\n\nDivision Result= %d\n\n\n",data);

    // Perform Modulo Division
    data =7%3;
    printf("Modulo Division Result= %d\n",data);

    return 0;
}

Output:

Division Result= 2
Modulo Division Result= 1

Explanation: Actually, the division operator gives the quotient but the modulo division operator gives the Remainder.

 

Relational Operators

A relational operator is used to comparing the mathematical, character and logical expression. The relational operator compares the right-hand expression with left-hand expression, if these expressions are true then return 1(True Value) and if it’s false then return 0 (False Value).

Note: Relation operator generally used with the conditional expression like if, while and do-while, etc.

You can check Articles,

Let’s assume, ‘a’ is 5 and ‘b’ is 3.

Operator Meaning of Operator Example
== Equal to a == b is evaluated to 0 (false)
> Greater than a > b is evaluated to 1 (true)
< Less than a < b is evaluated to 0 (false)
!= Not equal to a != b is evaluated to 1 (true)
>= Greater than or equal to a >= b is evaluated to 1 (true)
<= Less than or equal to a <= b is evaluated to 0 (false)

 

Let see an example to understand the use of relational operators. Suppose there is a scenario where you want to display “Welcome” and “Bye” as corresponding to the input value of ‘a’. If the value of a less than 100 then displays “Welcome” otherwise display “Bye”.

#include <stdio.h>

int main()
{
    int a=0;

    printf("Enter the value\n");
    scanf("%d",&a);

    if(a<100)
    {
        printf("Welcome\n");
    }
    else
    {
        printf("Bye\n");
    }

    return 0;
}

OutPut 1:

Enter the value = 20
Welcome

 

OutPut 2:

Enter the value = 200
Bye

 

 

Logical operator

If any event depends on more than one condition then we have to use logical operator. Logical operators are used to combine two or more conditions. The result of the operation of a logical operator is a boolean value either true or false.

Let see an example, suppose you are washing clothe in a digital washing machine and you want your washing machine stop only in two situations when the clothe is completely clean or after 5 minutes of the start.

To manage this scenario programmer have used the logical operator, which would be like this.

if (time > 5_minute || cloth == clean)
Stop;
else
Run;

 

There are three type logical operators in c.

Operator           Meaning
|| (Logical OR) Work like digital OR Gate.
&&(Logical AND) Work like digital AND Gate.
! ( Logical NOT) Work like digital NOT Gate.

Logical OR

It works like digital OR gate if any condition true then it returns a true value.

Example code,

#include <stdio.h>

int main()
{
    int a=0, b =0;

    printf("Enter the value a = ");
    scanf("%d",&a);

    printf("Enter the value b = ");
    scanf("%d",&b);

    if (a < 10 || b > 20)
    {
        printf("Hi aticleworld\n");
    }
    else
    {
        printf("Bye aticleworld \n");
    }
    return 0;
}

 

In the above example, if the value of “a” less than 10 or value of “b” greater than 20, then print “Hi aticleworld” otherwise in any scenario print “Bye aticleworld”.

Note: Return true if any one of them expression true in logical OR operator.

 

Logical AND

It works like digital AND gate if both condition true then it returns the true value.

Example code,

#include <stdio.h>

int main()
{
    int a=0, b =0;

    printf("Enter the value a = ");
    scanf("%d",&a);

    printf("Enter the value b = ");
    scanf("%d",&b);

    if ((a < 10) && (b > 20))
    {
        printf("Hi aticleworld\n");
    }
    else
    {
        printf("Bye aticleworld \n");
    }
    return 0;
}

 

In the above example, if the value of “a” less then 10 and value of “b” greater then 20, then print “Hi aticleworld” otherwise in any scenario print “Bye aticleworld“.

Note: Return true only if both expressions will true in logical AND operator.

 

Logical NOT

It works like digital NOT gate if condition false then it returns the true value. It works on a single expression, if the expression is true then it evaluates false and if the expression is false then it evaluates true.

Example code,

if(!expression)
{
    printf("Hi aticleworld\n");
}
else
{
    printf("Bye aticleworld \n");
}

If expression false then print “Hi aticleworld “either print “Bye aticleworld“.

 

Bitwise operator

These operators mainly use in low-level programming. Bitwise operator operates on each bits. It’s mainly useful for integer, not useful for float and double. If we need to check a single bit or we want to make reset or set a single bit then we have to use bit-wise operator.

There are following bit-wise operators

Operator           Meaning
| (Bitwise OR) Use to Set a Bit of a Register.
& (Bitwise AND) Use to check a Bit of Register.
^ (Bitwise EX-OR) Use to toggle a Bit of a Register.
~( Bitwise complement) Use for the complement.
<< (Shift left) Use to shift a sequence of Bit toward left.
>> (Shift right) Use to shift a sequence of Bit toward Right

 

You can check below articles,

 

Ternary operator

It’s a conditional operator, consist two symbol question mark (?) and colon (:). It’s similar to if-else but somehow execution time of ternary operator is less as compared to if-else.

Syntax:

(expression)  ?  statement1  :   statement2;

 

To understand ternary operator we take an example.

(a<10) ? printf (“Hi !”) : printf(“Bye !”);

In the above example, if the value of “a” less than 10 then print “Hi” otherwise print “Bye“.

 

Assignment operator

Assignment operator assigns the value from right to left. The left side operand of the assignment operator is a varriave (lvalue) and the right side operand of the assignment operator is a variale or constant (lvalue or rvalue). The value on the right side must be of the same data-type of variable on the left side otherwise the compiler will raise an error.

See below example,

a= 5; (valid)
5=y; (not valid)

 

There are different types of assignment operators.

 “=” :  This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.

Example,

int data1 = 10;
float data2 = 20.0;
char data3 = 'y';

 

 “+=”  : This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on right and then assigns the result to the variable on the left.

Example,

(data += 27) is equivalent to (data = data + 27)

 

“-=”: This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the value on right from the current value of the variable on left and then assigns the result to the variable on the left.

Example,

(data -= 27) is equivalent to (data = data - 27)

 

“*=”: This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on right and then assigns the result to the variable on the left.

Example,

(date *= 27) is equivalent to (data = data * 27)

 

“/=”: This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on right and then assigns the result to the variable on the left.

Example,

(date /= 27) is equivalent to (data = data/27)

 

“%=”: This operator is combination of ‘%’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on right and then assigns the result to the variable on the left.

Example,

(date %= 27) is equivalent to (data = data%27)

 

sizeof operator:

It is a compile-time unary operator which can be used to compute the size of its operand.  It returns the size of the operand in bytes. Let see an example program,

#include <stdio.h>

int main()
{
    int  arr[] = {10, 20, 30, 40, 50, 60};

    printf("Array Size in bytes = %d\n",sizeof(arr));

    return 0;
}

Output: 24 (Assuming the size of int is 4 bytes)

 

You can read below Articles,

 

Operator Precedence And Associativity In C

The operator precedence defines the priority of the operators that means precedence indicates which operator applied first on the given expression. The higher precedence operator is evaluated before the low precedence operator.

You can check this Article, Operator Precedence And Associativity.

In the below table Precedence of operators decreases from top to bottom.

Operator Precedence And Associativity In C

Recommended Articles for you,



3 comments

Leave a Reply

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