0% found this document useful (0 votes)
32 views7 pages

Operators in C

Uploaded by

kirti Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Operators in C

Uploaded by

kirti Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

C Operators

Operators in C is a special symbol used to perform any mathematical or logical operation.

The data items on which the operators are applied are known as operands. Operators are applied between the
operands.

Depending on the number of operands, operators are

Unary Operators

A unary operator is an operator applied to the single operand.

For example: increment operator (++), decrement operator (--) etc.

Binary Operators

The binary operator is an operator applied between two operands.

The following is the list of the binary operators:

o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operators

Ternary Operator

It involves the use of 3 operands. For example ?: is used in place of if-else condition.

Special (Misc.) Operators

Binary Operators
1. Arithmetic Operators
Following table shows all the arithmetic operators supported by C language.
Suppose variable A holds 10 and variable B holds 20 then:
Operator Description Example
+ Adds two operands.(numbers) A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands (numbes) A * B will give 200
/ Divide one operand by another operand B / A will give 2
% Modulus Operator finds remainder after an integer division B % A will give 0

CBPCC Page 1
 Integer Arithmetic: -
When both the operands are integers, then the expression is known as an integer expression and the operation
is known as an integer arithmetic operation.
The result of the integer arithmetic is always the integer.
e.g. 5 + 6 = 11
34 – 3 = 31
3*3=9
4/2=2
9/5=1 (Fractional part is truncated)
8%5=3 (remainder value)
 Real Arithmetic: -
If in arithmetic operation both the operands are real then the expression is known as the real expression and the
operation is known as the real arithmetic operation.
In real arithmetic result is always the real.
e.g. 4.5 + 43.3 = 47.8
5.5 - 1.2 = 4.3
We cannot use modulus operator with the real arithmetic.
 Mixed mode arithmetic: -
If in arithmetic operation one operand is integer and another is real then this expression is known as the mixed
mode expression and operation is known as the mixed mode arithmetic operation.
e.g. 4.5 + 45 = 49.5
In mixed mode arithmetic result is real.

2. Relational Operators: -
The relational operators can do the comparison of two quantities(numbers).
The expression containing the relational operator is known as the relational expression.
C supports six different relational operators:
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to

CBPCC Page 2
e.g. a < b is relational expression
10==20 is false
10!=50 is true
10 < 20 is true
20 < 10 is false
Note : The result of the relational operation is either true (1) or false (0).
The relational expression is used to check condition in if …. else statement in C.
3. Logical Operators: -
C provides following logical operators:
Operator Meaning
&& And
|| Or
! Not
These operators are used when we want to check more than one condition.
e.g. a > b && a == 10
10>20 && 10>30
a>b && a>c
a>10 || a>b
Note:
An expression, which contains logical operators, is known as the logical expression.
The result of the logical expression is also either true or false.
The logical expression is used to check multiple conditions in if …. else statement in C.
4. Bitwise Operators
C provides bitwise operators for manipulation (Calculation) of data at bit level.
Bitwise operators work on bits and perform bit by bit operation.
These operators are used for testing the bits, or shifting them right or left.
These operators can’t be used with the float or double.

Operator Syntax Name Example Result Description


& a&b Bitwise and 3 & 5 1 1 if both bits are 1.
| a|b Bitwise or 3|5 7 1 if either bit is 1.
^ a^b Bitwise xor 3 ^ 5 6 1 if both bits are different.
~ ~a Bitwise not ~3 -4 Inverts the bits.
<< n << p left shift 3 << 2 12 Shifts the bits of n left side by p positions.
>> n >> p right shift 5 >> 2 1 Shifts the bits of n right p positions.
CBPCC Page 3
Bitwise operators work on bits and perform bit-by-bit operation. The truth tables for &, |, ^ and ~ is as follows

P q p&q p|q p^q ~p

0 0 0 0 0 1

0 1 0 1 1 1

1 1 1 1 0 0

1 0 0 1 1 0

Suppose A = 60 and B = 13 in binary format, they will be as follows −


A = 0011 1100
B = 0000 1101
------------------
A&B = 0000 1100 (12 in decimal)
A|B = 0011 1101 (61 in decimal)
A^B = 0011 0001 (49 in decimal)
~A = 1100 0011 (-61 in decimal)
5. Assignment Operator:
Assignment operator is used to assign some value to the variable. There is only one assignment operator that
is ‘=’
e.g. a = 30;
Above expression assign value 30 to ‘a’.
Also assignment operator can be used for the shorthand assignment operator. It is used to perform arithmetic
operation with the variable itself.
Simple Expression Shorthand Assignment
Operators
a = a + 1; a += 1;
a = a –23; a -= 23;
a = a * 4; a * = 4;
a = a / 5; a /= 5;
a = a % 10; a %= 10;

CBPCC Page 4
The advantages of shorthand assignment operators are:
It is easy to write since we do not have to write the operand two times.
It is easy to read.
It is more efficient.
Unary Operator

Increment and Decrement Operator:


C provides two operators increment operator ( ++ ) and decrement operator (--). Increment operator is used to
add 1 into the variable previous value. Decrement operator is used to subtract 1 from the variable previous
value.
e.g. i++; // If initially i value is 10 then after this instruction execution it becomes 11
i--; // If initially i value is 10 then after this instruction execution it becomes 9
Increment operator has two formats: one is postfix and another is prefix operator.
I++ and ++i

When first one is used in assignment it first performs the assignment and then increment the value of ‘i’, while
later one is used then it increments the value of ‘i’ first and then performs assignment.
e.g. i=10;
a=i++;
printf(“i = %d \t a= %d\n”,i,a);
This statement print the output
i = 11 a = 10
i=10;
a= ++i;
printf(“i = %d \t a = %d\n”,i,a);

This statement print the output


i = 11 a = 11
Same thing is true for the decrement operator.

Ternary or conditional Operator (?:)


The Conditional Operator ‘?:’ takes three operands , so it is known as ternary operator.
The conditional operator (?:) is kind of similar to the if-else statement.

CBPCC Page 5
Syntax:
variable = Expression1 ? Expression2 : Expression3;

It can be visualized into if-else statement as:


if (Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}

Working:
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then
Expression2 will be executed. Otherwise, if the condition(Expression1) is false then Expression3 will be
executed.
Example:
#include <stdio.h>

void main()
{
int a = 10, b = 20, c;
c=(a < b)?a:b;
printf("Minimum number=%d", c);
}

Output:

Minimum number=10

Special (Misc.) Operators


C supports some other operators, which have some specific purpose and mean. Those operators are known as
the special operators. Some special operators are comma operator, sizeof operator, &, * etc.

CBPCC Page 6
Comma Operator
Comma operator is used to link the expression together and entire expression is evaluated from left to right and
the right-most expression value is assigned to left side of the assignment operator.
e.g. v = (x= 10, y = 5, x + y);
In above expression first x has assigned value 10, then y has assigned value 5, and then addition operation of x
and y is performed and the result is assigned to the v.
sizeof Operator
It is operator, which gives the bytes occupied by the operand in memory. Operand may be constant, variable or
any other qualifier.
E.g. m = sizeof(int); // Give the size of integer data type
k = sizeof(14); // Give the no. of bytes occupied by simple value 14
l = sizeof(p); // Give the no. of bytes occupied by variable p

CBPCC Page 7

You might also like