Operator in C



Operators are foundation any programming language. The functionally C programming language is incomplete without the use of the operator.

Operators are symbols that help us to perform specific mathematical and logical computations on the operator.







Operator Symbol Operator Name Operator Type
++, -- Unary operator unary operator
+, -, *, /, % Arithmetic operator Binary operator
<, <=, >, >=, ==, != Relational operator Binary operator
&&, ||, ! Logical operator Binary operator
&, |, <<, >>, ~, ^ Bitwise operator Binary operator
=, +=, -=, *=, /=, %= Assignment operator Binary operator
? : Ternary operator Ternary operator


  • Unary operator: operator that operator or work with a single operand and unary operator

  • Binary operator: Operator that operates or works with two operands are binary operators



1 ] Arithmetic operator:

The arithmetic operator is used to from the arithmetic expressions





Operators Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division

Example:

If a= 15 and b=6, then the results of the arithmetic operator  are as follows

a + b = 21

a - b = 9

a * b = 90

a / b = 2

a % b = 3 (reminder)

Now take look at this one

13 % 4 =1

-13 % 4= -1

-13 % 4= 1

-13 % -4 = -1

the sign of the reminder is depend upon the left value.

In operation in the above expressions the sign of 13 is considered to give a reminder.

The answer of any operator expression depends upon the data type of both operands if both are an integer Datatype then the answer is always an integer.
Example
Write a program to convert the given temperature in Fahrenheit to Celsius and vice versa.

Formula to convert temperature in Fahrenheit

c=(5/9) * (f-32)


f=(9/5)c + 32


Now (5/9) is 0.55 but as both are integers so its equal to 0, so what can you do, simple 50 convert integer to float by replacing 5 to 5.0 same in case of 9/5
#include<stdio.h>
int main()
{
float c1 ,f1, c2, f2;
printf(" To convert the temperature From Fahrenheit to Celsius");
printf("\n Enter temperature in Fahrenheit ");
scanf("%f",&f1);
c1= (5/9) *(f1-32);
printf("Temperature in Celsius is %f",c1);
printf(" To convert the temperature From Celsius to Fahrenheit");
printf("\n Enter temperature in Celsius ");
scanf("%f",&c2);
f2= (9/5)*c2 + 32;
printf("Temperature in Fahrenheit is %f",f2);
}


Output:

To convert the temperature From Fahrenheit to Celsius
Enter temperature in Fahrenheit  98
Temperature in Celsius is 36.333333

To convert the temperature From Celsius to Fahrenheit
Enter temperature in celsius     25
Temperature in Fahrenheit is 77



2). Relational operator


Relational operators are used to compare the value of two expressions. Relational operators are binary operators because they require two operands to operate. The output of relational expression is always true (non zero value) or false (0), its mostly used to take editions.





Operator Description Example Result
> Greater than 30>29 True
>= Greater than or equal to 3>=2 True
< Less than 18<12 False
<= Less than or equal to 6<=9 True
== equal to 99==99 True
!= Not equal to 10!=9 True



Now important point

The precedence and associativity of relational operators is left to right means when you assist your program then in expression always left one operand is compare with right operand.



Example:

#include<stdio.h>

int main()

{

int x=10, y=12;


printf("x=%d \n y=%d \n", x, y);


printf("x>y = %d\n", x>y);


printf("x>=y = %d\n", x>=y);


printf("x<y = %d\n", x<y);


printf("x<=y = %d\n", x<=y);


printf("x==y = %d\n", x==y);


printf("x!=y = %d\n", x1=y);


return 0;


}

Output:

// 0 means false and 1 means true

x=10

y=12

x>y = 0

x>=y = 0

x< y =1

x<=y =1

x==y =o

x!=y =1





3). Logical operator

These operators are used to perform logical operations on the given expressions there are three logical operators in C language.

  1. Logical And( &&)

  2. Logical Or(||)

  3. Logical Not(!)


Logical operators do not perform the usual arithmetic conversions, instead, they evaluate each operand in terms of its equivalence to 0. The result of a logical operation is either 0 or 1. The type of result is int.







Operator Description
&& The logical AND operator produces the value of 1 if both operands have non zero values if either operand is equal to 0, the result is 0, if the first operand of a logic AND operation is equal to 0, the second operand is not evaluated.
|| The Logical OR operator performs an inclusive OR operation on its operands. The result is 0 if both operands have ) values. If either operand has a non zero value, the result is 1, it the first operand of a logical OR operation has a non zero value, the second operand is not evaluated.
! The logical NOT true only if the operand is 0. it is used to reverse the logical state of its operand.



True table of && and ||




Condition 1 Condition 2 Logical AND (&&) Logical OR (||)
False False False False
False True False True
True False False True
True True True True



Example:


#include<stdio.h>

int main()

{

int a=2, b-5, c=3, ans;


ans= (a==b) && (b>c);   // 0 && 1 =0

printf("( a==b) && (b>c) is %d \n", ans);


ans= (a<b) && (b>c);   // 1 && 1 =1

printf("( a<b) && (b>c) is %d \n", ans);


ans= (a==b) || (b>c);   // 0 || 1 =1

printf("( a==b) || (b>c) is %d \n", ans);


ans= (a>b) && (c>b);   // 0 || 0 =0

printf("( a==b) && (c>b) is %d \n", ans);


ans = !(a==b)   // !0=1

printf("!(a==b) is %d \n", ans);


ans = !(a<b)   // !1=0

printf("!(a<b) is %d \n", ans);


return 0;


}

Output:

( a==b) && (b>c) is  0

( a<b) && (b>c) is 1

( a==b) || (b>c) is 1

( a>b) || (c>b) is 0

!(a==b) is 1

!(a<b) is 0

Note:

&& was higher precedence than || so && will be evaluated first.



4). Assignment operators


An assignment operator assigns the value of the right-hand operand to the left-hand operand. The value on the right side must be of the same data type of the variable on the left side otherwise the compiler will throw an error.




Operator Operation performed
= Simple assignment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Remainder assignment

Expression 1 += Expression 2

can be written as

Expression 1 =Expression 1 + Expression 2

same in other assignment operators



Example:

#include<stdio.h>

int main()

{

int x=3, y=5;


x +=y;    // x=x + y

printf("Value of X is %d", x);


x -=y;    // x=x - y

printf("Value of X is %d", x);


x *=y;    // x=x * y

printf("Value of X is %d", x);


x /=y;    // x=x / y

printf("Value of X is %d", x);


x %=y;    // x=x % y

printf("Value of X is %d", x);


return 0:


}


Output:

Value of X is 8

Value of X is 3

Value of X is 15

Value of X is 3

Value of X is 3



5). Bitwise operator


To perform a bit-level operator in C programming, Bitwise operators are used.




Operator  Meaning
+ Bitwise AND
- Bitwise OR
^ BItwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift

1). Bitwise AND (&)

Takes two numbers as operator and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

Bitwise AND operator of two integers 10 and 15.

10 = 00001010 (In Binary)

15 =00001111 (In Binary)

25 = 00011001 (In Binary)

Bit operation of 10 and 15

   00001010


& 00001111


   00001010  = 10 (Decimal)


Bit operator of 15 and 25

00001111

& 00011001

00001001  = 9 (Decimal)


Example:

#include<stdio.h>

int main()

{

int a=15,  b=25;

printf("output = %d", a&b);

return 0;


}


Output:

output= 9

2). Bitwise OR (|)

Takes two numbers as operand and does OR on every bit of two numbers. The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1.

15 = 00001111   (In Binary)

25 = 00011001   (In Binary)

Bitwise OR operation on 15 and 25

00001111

  |  00011001

      00011111  =  31 (Decimal)


Example:

#include<stdio.h>

int main()

{

int a=15, b=25;

printf("Output = %d", a | b);

return 0;


}


Output:

output= 31

3).Bitwise XOR (^)

Takes two numbers as operand and does XOR on every bit of two numbers. The output of bitwise XOR is 1 If the corresponding bits of two operands are opposite.

15 = 00001111 (In Binary)

25 = 00011001  (In BInary)

Bitwise XOR operation on 15 and 25

00001111

^ 00011001

00010110  = 22 (Decimal)


Example:

#include<stdio.h>

int main()

{

int a=15, b=25;

printf("Output=%d",a ^ b);

return 0;


}


Output:

output = 22

4). Bitwise NOT (~)

Takes one numbers, its unary operator. It change 1 to 0 and 0 to 1. It is denoted by ~.

31  = 00011111   (In Binary)

Bitwise NOT operation on 31

~  00011111

11100000   = 224

21  =  00010101

Bitwise NOT operator on 21

~ 00010101

11101010  =  234 in decimal

But when we executed the program its get output of ~31 as -31  and ~21 as -21

It happens because of a twist in the bitwise complement operator in C.

Generally, there are two types of complement of binary numbers 1's complement and 2's complement to get 1's complement of a binary number, simply invert the bits of the given number. To get 2's complement of the binary number is 1's complement of given number pulse 1 to the 1 least significant bit (LSB)

For example:

In 31 1's binary is 00011111 now its 2's complement of the binary number is (00011111) +1= 00100000 (-32 in decimal)






Decimal Binary 2's complement
224 11100000 -(00011111 + 1) = -32
234 11101010 -(00010101 + 1) = -22


bitwise complement of N= ~N (That acculy be presented in 2's complement)

2's complement of ~N = -(~(~N) +1)

= -(N + 1)

so for any integer n, bitwise complement of n will be -(n+1)


Example:

#include<stdio.h>

int main()

{

printf("output=%d\n",~31);

printf("output=%d\n",~5);

printf("output=%d\n",~10);

return 0;


}


Output:

output= -32

output= -6

output= 9


5) Bitwise left swift(<<)

takes two numbers left side the first operand.the second one is numbers that decides the numbers of places to shift.

Example

binary of 124 is 1111100

now

124<<0  = 1111100 (binary of 248)

124<<1 = 11111000 (binary of 496)

124<<3 = 1111100000 (binary of 992)

[2] binary of  <<  is 10110

22<<0 = 10110 (22)

22<<1 = 101100 (44)

22<<2 = 1011000 (88)

22<<3 = 10110000 ( 176)

if you notic that when we excute left swift on operand it convert value of operand by multiple of 2 by each operation.

6) bitwise right shift (>>)

takes two number, it shift of specified bits to words to right by certain numbers,that we provided by right operand.

Example

binary of 124 is 1111100

now 124>>0  = 1111100 (124)

124>>1  = 111110 (62)

124>>2  =   11111 (51)

124>>3  =      1111 (15)

124>>4  =        111 (7)

124>>5   =         11 (3)

124>>6   =             1 (1)

[2] binary of 189 is 10111101   (189)

now 189>>0 =10111101    (189)

189>>1 =  1011110    (94)

189>>2  =   101111    (47)

189>>3  =     10111   (23)

189>>4   =      1011   (11)

189>>5  =         101   (5)

189>>6  =          10    (2)

189>>7=              1    (1)

if you notic that when we excute right shift on operand it convert value of operand half of value of operand.

note : the left swift and right shift operators should not be used for negative numbers, also the numbers is shifted is not more then the size of integers, that is 32

if we try to excute 189<<33 is underfined if integrs we stored using 32 bits. the size of integes is depend upon the your device  and complies that you use . it can be  16 bits.


Use to bitwise operators

In any fields such as embedded programming system programming and networking bitwise operators can be seen m solutions to many different problems.



6)Conditional or Ternary operator ( ? : )


The ternary operator use for decision making in place of longer if and else conditional statements, it follows the same algorithm as of If-else statement

Syntax condition? Value_if_true: value_if_false

The ternary operator takes three arguments:

  1. The first is comparison argument

  2. The second is the result if the condition is true

  3. The third is the result if the condition is false.


Here Example of a ternary operator to find the maximum of two numbers.

using if else condition


#include<stdio.h>

int main()

{

int a=5, b=7,c;


if(a>b){

            c=a;

}

else {

        c=b;

}


printf("%d",c);


return 0;


}

Output:

7

now same program using ternary  or conditional operator

#include<stdio.h>

int main()

{

int a=5, b=7, c;


c=a>b? a:b;

printf("%d",c);


return 0;


}


Output:

7



in above program c is set to b, because the condition a>b is false.

Remember that the arguments value_if_true and value_if_false must be of the same type.

Ternary operator can be nested just like if-else statement. Nested term=nary operator are evaluated from right to left. so we can use parenthesis() for better and accurate result.

consider the following program for better understanding.



Example:

find maximum number from given three number.

#include<stdio.h>

int main()

{

int a, b, c, d;


printf("Enter value of first number: ");

scanf("%d",&a);


printf("Enter value of second number: ");

scanf("%d",&b);


printf("Enter value of third number: ");

scanf("%d",&c);


if(a>b) {


if(a>c){

        d=a;

}

else {

        d=c;

}

}

else {

if(a>b) {

    d=b;

}

else {

    d=c;

}

}


printf("Maximum number is  %d", d);


return 0;


}


Output:

Enter value of first number 77

Enter value of second number 23

Enter value of third number 56

Maximum number is 77

 

Now same program using ternary or conditional operator.



#include<stdio.h>

int main()

{

int a, b, c, d;


printf("Enter value of first number: ");

scanf("%d",&a);


printf("Enter value of second number: ");

scanf("%d",&b);


printf("Enter value of third number: ");

scanf("%d",&c);


d=a>b ? ( a>c ? a:c) : (b>c ? b:c);


printf("Maximum number is %d",d);


return 0;


}


Output:

Enter value of first number 77

Enter value of second number 23

Enter value of third number 56

Maximum number is 77



7).Increment and decrement operator (++, --)


Increment and decrement operators are unary operators that add or subtract one from the value of the operand.

The increment operator is written as ++ and the decrement operator is written as --.

++ and -- operator as prefix and postfix.

++ and  -- as prefix

If we use the ++ or --  operator as prefix like ++x or --x  then the value of x is incremented or decremented by 1 then its return or assign value.



For Example

int x=4, y;

y= ++x;  //y=5 and x=5

y= --x;   //y=4 and x=4

++ and -- as postfix


If we use the ++ or -- operator as postfix like x++ or x-- then the value of x is incremented or decremented by 1, but its return or assign it original value.

In postfix the value of a variable is returned and assigned first then, the variable is increment and decrement according to operator(++,--)



For example

int x=4, y,z;

y=x++;    // y=4 and x=5

y=x--;      // y=4 and x=3

z=x--;     // z=4 and x=3




Example:

Increment operators


1)int x;

int y;

//pre increment x is incremented by 1,the y assigned the value of x

x=1

y=++x;   // x is now 2, y is also 2


2)int x;

int y;

//post increment y is assigned the value of x, then x is incremented bt 1.

x=1;

y=x++;      //x is now 2, y is 1




Decrement operator

3) int x;

int y;

//pre decrement -x is decremented by 1, then y is assigned the value of x

x=1;

y=--x;    // x is now 0, y is also 0

x=3;

y=--x   // x is now 2 , y is also 2



4)int x;

int y;

//post decrement -y is assigned the value of x, then x is decrement by 1

x=1;

y=x--;     // x is now 0, y is 1