Operators in C - Computer for Competition Exams

Operators in C

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language supports a rich set of built-in operators.Operators are used in program to manipulate data and variables.
C language provides the following types of operators -

  • Arithmetic Operators
  • Increment and Decrement Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Conditional Operators
  • Bitwise Operators
  • Special Operators

Arithmetic  Operators :

An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).


Arithmetic Operators/Operation
Example
+ (Addition)
A+B
– (Subtraction)
A-B
* (multiplication)
A*B
/ (Division)
A/B
% (Modulus)
A%B

Increment and Decrement operators :

C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand. 
                                              
Operator
Operator/Description
Pre increment operator (++i)value of i is incremented before assigning it to the variable i
Post increment operator (i++)value of i is incremented after assigning it to the variable i
Pre decrement operator (–i)value of i is decremented before assigning it to the variable i
Post decrement operator (i–)value of i is decremented after assigning it to variable i


Assignment Operators :

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =.


Operators
Example/Description
=
sum = 10;
10 is assigned to variable sum
+=
sum += 10;
This is same as sum = sum + 10
-=
sum -= 10;
This is same as sum = sum – 10
*=
sum *= 10;
This is same as sum = sum * 10
/=
sum /= 10;
This is same as sum = sum / 10
%=
sum %= 10;
This is same as sum = sum % 10
&=
sum&=10;
This is same as sum = sum & 10
^=
sum ^= 10;
This is same as sum = sum ^ 10

Relational Operators :

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision making and loops.
Operators
Example/Description
>
x > y (x is greater than y)
<
x < y (x is less than y)
>=
x >= y (x is greater than or equal to y)
<=
x <= y (x is less than or equal to y)
==
x == y (x is equal to y)
!=
x != y (x is not equal to y)

Logical Operators :

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making.

Operators
Example/Description
&& (logical AND)
(x>5)&&(y<5)
It returns true when both conditions are true
|| (logical OR)
(x>=10)||(y>=10)
It returns true when at-least one of the condition is true
! (logical NOT)
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false

Bitwise Operators :

Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of bits from right to left. Bitwise operators are not applied to float or double.
The truth tables for &, |, and ^ is as follows −
pqp & qp | qp ^ q
00000
01011
11110
10011
Below are the Bitwise Operators and their name in C language :

  1. & - Bitwise AND
  2. | - Bitwise OR
  3. ^ - Bitwise Exclusive OR
  4. >> - Right Shift
  5. << - Left Shift
  6. ~ - Bitwise Complement

Special Operators :

Below are some of special operators that the C language offers :

Operators
Description
&
This is used to get the address of the variable.
Example : &a will give address of a.
*
This is used as pointer to a variable.
Example : * a  where, * is pointer to the variable a.
Sizeof ()
This gives the size of the variable.
Example : size of (char) will give us 1.

Operators in C Operators in C Reviewed by Go Programming on July 18, 2017 Rating: 5
Powered by Blogger.