Learn Various Operators in C (with video)

operators in c

Operators are the basic infrastructure of any programming language. The functionality of the C programming language. Learn Various Operators in C is incomplete without the use of operators. Operators are the base of any operation. They are the symbols that perform mathematical and logical operations on operands. Operators in C instruct the compiler to perform the specific task.

Let’s understand it with an example,
Suppose, c= a+b;

Then, what happens?

The compiler understands the ‘+’ instruction and adds the value of ‘a’ with ‘b’ and store the result in ‘c.’

See Also: How to avoid the creation of Dangling Pointer in C?

Here, ‘+’ is the addition operator and ‘a’ and ‘b’ are operands. The C programming language has a lot of built-in, and they are:

Learn Operator precedence and associativity in C

1) Arithmetic Operators in C

These operators are used to perform arithmetic/mathematical operations (addition, subtraction, etc.) on operands. Arithmetic operators in C are of two types:



A) Unary Operators

Those operators which operate on single operand are known as unary operators. Like, (++, --)

operators in c

a) Increment Operator
This (++) operator increases the value by one. They are of two types:

i) Post-increment Operator: When the increment operator (++) is placed after the variable name is known as Post-increment operator. For instance, z++

This operator first prints the value of z then increases the value of z by one.

Following C program helps you to understand this concept,
#include <stdio.h>
int main()
{
int z=10, out;
out=z++;
printf ("Value of z %d and out\n",z, out);
return 0;
}

Output
Value of z 10 and out 11

ii) Pre-increment Operator: When the increment operator (++) is placed before the variable name is known as Pre-increment operator. Example: ++z. This operator increases the value of z by one, then prints the output.

You can understand the working of Pre-increment operator by C Program
#include <stdio.h>
int main()
{
int z=5;
out=++z;
printf ("Value of z and out is %d%d\n",z,out);
return 0;
}

Output
Value of z and out is 6 6

b) Decrement Operator

This (--) operator decreases the integer value by one. This operator is categorized as a pre-decrement operator and post-decrement operator,

i) Pre-decrement Operator: When the decrement operator (--) placed before the variable name is called Pre-decrement operator. Example: --z

ii) Post-decrement Operator: When the decrement operator (--) placed after the variable name is called Post-decrement operator.

Following C Program helps you to understand the working of Post and Pre-decrement operator

#include <stdio.h>
int main()
{
int z=10,out;
out=--z;
printf("Value of out %d and z %d\n",out,z);
out = z--;
printf ("Value of out %d and z %d\n", out,z);
return 0;
}

Output
Value of out 9 and z 9
Value of out 9 and z 8

B) Binary Operators

Those operators which operate on two operands are known as binary operators. For example: (+, -, /, *, %)

operators in c

Addition operator: This (+) operator add two operands. Example: a+b
Subtraction operator: This (-) operator subtract two operands. Example: a-b
Multiplication operator: This (*) operator multiplies two operands like a*b
Division operator (/): The First operand is divided by the second operand. Example: a/b
Modulus: When the first operand is divided by the second operand, then this (%) operator returns the remainder. Example: a%b


//Following C Program helps you to understand the working of the C program.

#include <stdio.h>
int main()
{
int w=19, x=5, out;
//Addition
out=w+x;
printf("Output of w+x is %d\n",out);
//Subtraction
out=w-x;
printf("Output of w-x is %d\n",out);
//Multiplication
out=w*x;
printf("Output of w*x is %d\n",out);
//Division
out=w/x;
printf("Output of w/x is %d\n",out);
//Modulus
out=w%x;
printf("Output of w%x is %d\n",out);
return 0;
}

Output
Output of w+x is 24
Output of w-x is 14
Output of w*x is 95
Output of w/x is 3
Output of w%x is 4

2) Relational Operators in C

This operator is used for the comparison of two values of two operands. It compares the right-hand value with left-hand value, if these values are true, then return 1 (True value); otherwise, it returns 0 (False value).

operators in c

A) Equal to Operator

This (==) operator checks the value of left operand with a right operand, if it is true then return 1 else return 0 (False). Let’s try to understand it using C program,


#include <stdio.h>
int main()
{
int a=10, b=10;
printf ("a==b %d\n",a==b); // Vlaue of a is equal to b, returns 1 (True).
int x=25, y=5;
printf ("x==y %d\n",x==y); //Value of x is not equal to y, returns 0 (False).
return 0;
}

Output
a==b 1
x==y 0

B) Not equal to operator

This (!=) operator returns the true value, if the value of left operand is not equal to right operand’s value. See below,


#include <stdio.h>
int main()
{
int a=10, b=10;
printf ("a!=b %d\n",a!=b);
int x=30, y=10;
printf ("x!=y %d\n", x!=y);
return 0;
}

Output
a!=b 0
x!=y 1

C) Less than Operator

If the value of the left operand is less than the value of right operand then returns 1 i.e. True else return false value (0).

#include <stdio.h>
int main()
{
int a=10, b=5;
printf ("a<b %d\n",a<b);
int w=3, x=5;
printf ("w<x %d\n",w<x);
return 0;
}

Output
a<b 0
W<x 1

D) Less than or equal to operator

This (<=) operator returns the true value if the left operand is less than or equal to the value of the right operand. Below mention, C program helps you to understand the functioning of this operator,

#include <stdio.h>
int main()
{
int a=10, b=20;
printf ("a<=b %d\n",a<=b);
int x=10, y=10;
printf ("x<=y %d\n", x<=y);
int w=15, z=5;
printf ("w<=z %d\n", w<=z);
return 0;
}

Output
a<=b 1
x<=y 1
w<=z 0

E) Greater than Operator

This (>) operator returns the true value if left-hand operand is greater than right-hand operand. See an C Program,

#include <stdio.h>
int main()
{
int a=10, b=1;
printf ("a>b %d\n",a>b);
int x=12, y=13;
printf ("x>y %d\n",x>y);
return 0;
}

Output
a>b 1
x>y 0

F) Greater than equal to

This (>=) operator returns the true value if the value of the left-hand operand is greater than the value of right-hand operand; otherwise, it returns a false value, i.e., 0.

#include <stdio.h>
int main()
{
int a=1, b=3;
printf ("a>=b %d\n",a>=b);
int w=5, x=4;
printf ("w>=x %d\n", w>=x);
int y=7, z=7;
printf ("y>=z %d\n", y>=z);
return 0;
}

Output
a>=b 0
w>=x 1
y>=z 1

3) Logical Operators in C

If any event depends on more than one conditions, then we have to use a logical operator.

operators in c

A) Logical AND Operator

This (&&) operator works like digital AND gate. When both conditions are true then it returns a true value (1) else returns false value (0).

#include <stdio.h>
int main()
{
int x=10, y=15;
if (x<20 && y>11)
{
printf ("Hello\n");
}
else
printf ("Bye\n");
return 0;
}

In the above C program, the value of ‘x’ is less than 20, and the value of ‘y’ operand is greater than 11. Both the conditions are true. So, it will print Hello otherwise it prints Bye.

B) Logical OR Operator

This (||) operator works like a digital OR gate. Any one condition is true, and then it returns a true value.

#include <stdio.h>
int main()
{
int x =5, y=200;
if (x<20 || y>500)
printf ("Hello\n");
else
printf ("Bye");
return 0;
}

Output
Hello

In the above C program, the value of x is less than 20 which is True, so, it returns 1 (True Value), and value of b is less than 500 which is false, it returns 0 (False Value).

C) Logical NOT Operator

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

#include <stdio.h>
int main()
{
int a=10;
if (!(a<5))
printf ("True\n");
else
printf ("False\n");
return 0;
}

Output
True

4) Assignment operators in C

This (=) operator assign the value of right-hand operand to the left-hand operand or right-hand side value to the left operand. The value on the right-hand side must be of the same data type of left-hand side otherwise compiler produces an error. Below you can see assignment operator is combined with other operators to make a shorter version,


A) “+=”: The operator first adds the right-hand operand’s value to the left-hand operand and then assign the value to the left-hand operand.
For instance: (x+=y) can be written as (x=x+y).
If x=5 & y=5. Then, (x+=y) =10. OR
If x=5. Then, (x+=10) =15.

B) “-=”: This operator first subtracts the right-hand operand’s value to the left-hand operand and then assign the value to the left operand.
(x -= y) can be written as (x = x-y)
If x = 10 & y = 6, then, (x -= y) = 4, For instance OR
If x =10, then, (x -=6) =4.

C) “*=”: Multiplies the right-hand operand’s value with the left-hand operand and then assign the output to the left-hand operand.
Example: (x *= y) can be written as (x = x*y)
If x = 10 & y=5, then, (x *= y) = 50. OR
If x = 10 then, (x*=3) = 30.

D) “/=”: Value of the left-hand operand’s is divided by the right-hand operand, Therefore, the result is assigned on the left operand.
For instance: (x /= y) can be written as (x = x/y)
If x = 10 and y=2, then, (x /=y) =5. OR
If x = 10, then, then, (x /=5) =2.

5) Bitwise Operators in C

These operators in C are used to perform bit-level operations. These operators are used in low-level programming. This operator first converted to the bit-level, and perform the operation.


operators in c

A) Bitwise AND Operator (&)

The output of this operator is one if a bit of two operands is 1. Any bit of an operand is zero then the result of the corresponding bit will be 0.
For instance, Bitwise AND operation of two integers numbers i.e., 10 and 12.
10 = 00001010 (Binary Form)
12 = 00001100 (Binary Form)

Bitwise AND Operation of 10 and 12.
    0000 1010
& 0000 1100

0000 1000 = 08 (Decimal Form)


C Program Demonstrates working of Bitwise AND Operator

#include <stdio.h>
int main()
{
int x =10, y= 12;
printf ("Output of Bitwise AND Operation = %d\n",x&y);
return 0;
}

Output
Output of Bitwise AND Operation = 8

B) Bitwise OR Operator (|)

The output of this operator is one if any bit of two operands is 1. Therefore, the output is zero, when both the bits of operands is zero.

For instance, Bitwise OR operation of two integers numbers i.e., 10 and 12
10 = 00001010 (Binary Form)
12 = 00001100 (Binary Form)

Bitwise OR Operation of 10 and 12.
  0000 1010
| 0000 1100

0000 1110 = 14 (Decimal Form)

C Program helps you to understand concept of Bitwise OR Operator
#include <stdio.h>
int main()
{
int x =10, y= 12;
printf ("Output of Bitwise OR Operation = %d\n",x|y);
return 0;
}

Output
Output of Bitwise OR Operation = 14

C) Bitwise XOR Operator (^)

Resultant is one if the corresponding bit of two operands differs.
For instance: Bitwise XOR operation of two integers numbers i.e., 10 and 12.
10 = 00001010 (Binary Form)
12 = 00001100 (Binary Form)

Bitwise XOR Operation of 10 and 12.
  0000 1010
^0000 1100

0000 0110 = 06 (Decimal Form)

C Program shows the operation of Bitwise XOR Operator
#include <stdio.h>
int main()
{
int x =10, y= 12;
printf ("Output of Bitwise OR Operation = %d\n",x^y);
return 0;
}

Output
Output of Bitwise XOR Operation = 06

D) Bitwise NOT Operator (~)

It converts bit-1 to bit-0 or vice-versa.
Example: Bitwise NOT Operator or complement operator change the bit from 0 to 1 or 1 to 0.
10 = 0000 1010
Bitwise NOT Operation of 10
~ 0000 1010
   1111 0101 = 245 (In Decimal Form)

See the working of Bitwise NOT Operator
#include <stdio.h>
int main()
{
int x =10;
printf ("Output of Bitwise NOT Operation = %d\n",~x);
return 0;
}


Output
Output of Bitwise NOT Operation = -11

Explanation
The Bitwise complement of 10 is 245 (in decimal form). The 2’s complement of 10 is -11. Therefore, the output is -11 instead of 245.

E) Right-shift Operator (>>)

This operator shifts the bits towards right.
196 = 1100 0100 (Binary Form)
196 >> 2 = 0011 0001 (Right shift by two bits)
196 >> 7 = 0000 0001
196>>8 = 0000 0000
196>>0 = 1100 0100 (No Shift)

F) Left-shift Operator (<<)

This operator shifts the bits towards left.
196 = 1100 0001
196 << 2 = 1100 0000
196 << 0 = 1100 0001 (No Shift)

C Program demonstrates the working of shift operators in C
#include <stdio.h>
int main()
{
int num=196,i;
for (i=0; i<=2;i++)
{
printf ("Right shift %d = %d\n",i,num>>i);
}
printf ("\n");
for (i=0; i<=2;i++)
{
printf ("left shift %d = %d\n",i,num<<i);
}
return 0;
}

Output
Right shift 0 =196
Right shift 1 = 98
Right shift 2 = 49


Left shift 0 = 196
Left shift 1 = 392
Left shift 2 = 784
Learn Various Operators in C (with video) Learn Various Operators in C (with video) Reviewed by Glory on 16:26 Rating: 5

No comments:

Powered by Blogger.