0% found this document useful (0 votes)
21 views

Operators in C

Uploaded by

Durga Devi P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Operators in C

Uploaded by

Durga Devi P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Operators in C: Types and Examples

C operators are one of the features in C which has symbols that can be used to
perform mathematical, relational, bitwise, conditional, or logical manipulations.
The C programming language has a lot of built-in operators to perform various
tasks as per the need of the program. Usually, operators take part in a program
for manipulating data and variables and form a part of the mathematical,
conditional, or logical expressions.

In other words, we can also say that an operator is a symbol that tells the
compiler to perform specific mathematical, conditional, or logical functions. It
is a symbol that operates on a value or a variable. For example, + and - are the
operators to perform addition and subtraction in any C program. C has many
operators that almost perform all types of operations. These operators are really
useful and can be used to perform every operation.

Types of Operators in C

1. Arithmetic Operator

2. Increment/Decrement Operator

3. Assignment Operator

4. Logical Operator

5. Bitwise Operator

6. Misc Operator
Let's look at these operators in c in detail.

Arithmetic Operator With Example

Arithmetic Operators are the operators which are used to perform mathematical
calculations like addition (+), subtraction (-), multiplication (*), division (/), and
modulus (%). It performs all the operations on numerical values (constants and
variables).

The following table provided below shows all the arithmetic operators
supported by the C language for performing arithmetic operators.

 (Addition) It adds two operands

− (Subtraction) It subtracts second operand


from the first

* (Multiplication) It multiplies both operands

/ (Division) It is responsible for dividing


numerator by the

% (Modulus) This operator gives the


remainder of an integer after
Let’s look at an example of arithmetic operations in C below assuming variable
a holds 7 and variable b holds 5.
// Examples of arithmetic operators in C
#include <stdio.h>
int main()
{
int a = 7,b = 5, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a is divided by b = %d \n",c);
return 0;
}

Output:

a+b = 12

a-b = 2

a*b = 35

a/b = 1
Remainder when a divided by b = 2

The operators shown in the program are +, -, and * that computes addition,
subtraction, and multiplication respectively. In normal calculation, 7/5 = 1.4.
However, the output is 1 in the above program. The reason behind this is that
both the variables a and b are integers. Hence, the output should also be an
integer. So, the compiler neglects the term after the decimal point and shows 2
instead of 2.25 as the output of the program.

A modulo operator can only be used with integers.

Using modulo operator (%), you can compute the remainder of any integer.
When a=7 is divided by b=5, the remainder is 2. If we want the result of our
division operator in decimal values, then either one of the operands should be a
floating-point number.

Suppose a = 7.0, b = 2.0, c = 5, and d = 3, the output will be:

// When either one of the operands is a floating-point number


a/b = 3.50
a/d = 2.33
c/b = 1.66
// when both operands are integers
c/d = 1

Increment/Decrement Operator With Example

C programming has basically two operators which can increment ++ and


decrement -- the value of a variable. It can change the value of an operand
(constant or variable) by 1. Increment and Decrement Operators are very useful
operators that are generally used to minimize the calculation. These two
operators are unary operators, which means they can only operate on a single
operand. For example, ++x and x++ means x=x+1 or --x and x−− means x=x-1.

There is a slight distinction between ++ or −− when written before or after any


operand.

If we use the operator as a pre-fix, it adds 1 to the operand, and the result is
assigned to the variable on the left. Whereas, when it is used as a post-fix, it
first assigns the value to the variable on the left i.e., it first returns the original
value, and then the operand is incremented by 1.

Operator Description

This increment operator increases the integer


++
value by 1.

This decrement operator decreases the integer


--
value by 1.

Here is an example demonstrating the working of increment and decrement


operator:

// Examples of increment and decrement operators


#include <stdio.h>
int main()
{
int a = 11, b = 90;
float c = 100.5, d = 10.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}

Output:

++a = 12
--b = 89
++c = 101.500000
--d = 9.500000

In the above code example, the increment and decrement operators ++ and --
have been used as prefixes. Note that these two operators can also be used as
postfixes like a++ and a-- when required.

Assignment Operator With Example

An assignment operator is mainly responsible for assigning a value to a variable


in a program. Assignment operators are applied to assign the result of an
expression to a variable. This operator plays a crucial role in assigning the
values to any variable. The most common assignment operator is =.

👍C language has a collection of shorthand assignment operators that can be


used for C programming. The table below lists all the assignment operators
supported by the C language:
Operator Description Example

Assign

C = A + B will assign the value of


= Used to assign the values from
A + B to C.
right side of the operands to
left side of the operand.

Add then assign

Adds the value of the right


+= C += A is same as C = C + A
operand to the value of the left
operand and assigns the result
to the left operand.

Subtract then assign

Subtracts the value of the right


-= C -= A is same as C = C - A
operand from the value of the
left operand and assigns the
result to the left operand.

*= Multiply then assign C *= A is same as C = C * A


Multiplies the value of the
right operand with the value of
the left operand and assigns
the result to the left operand.

Divide then assign

Divides the value of the left


/= C /= A is same as C = C / A
operand with the value of the
right operand and assigns the
result to the left operand.

Modulus then assign

Takes modulus using the


%= C %= A is same as C = C % A
values of the two operands and
assigns the result to the left
operand.

Left shift and assign

<<= C <<= 4 is same as C = C << 4


Used for left shift AND
assignment operator.

>>= Right shift and assign C >>= 5 is same as C = C >> 5


Used for right shift AND
assignment operator.

Bitwise AND assign

&= C &= 7 is same as C = C & 7


Used for bitwise AND
assignment operator.

Used for bitwise exclusive OR


^= C ^= 6 is same as C = C ^ 6
and assignment operator.

Used for bitwise inclusive OR


|= C |= 9 is same as C = C | 9
and assignment operator.

The below example explains the working of assignment operator.

// Examples of assignment operators


#include <stdio.h>
int main()
{
int a = 7, b;
b = a; // b is 7
printf("b = %d\n", b);
b += a; // b is 14
printf("b = %d\n", b);
b -= a; // b is 7
printf("b = %d\n", b);
b *= a; // b is 49
printf("b = %d\n", b);
b /= a; // b is 7
printf("c = %d\n", c);
b %= a; // b = 0
printf("b = %d\n", b);
return 0;
}

Output:

b=7

b = 14

b=7

b = 49

b=7

b=0

Relational Operator With Example


Relational operators are specifically used to compare two quantities or values in
a program. It checks the relationship between two operands. If the given relation
is true, it will return 1 and if the relation is false, then it will return 0. Relational
operators are heavily used in decision-making and performing loop operations.

The table below shows all the relational operators supported by C. Here, we
assume that the variable A holds 15 and the variable B holds the 25.
Operator Description Example

It is used to check if the values


of the two operands are equal
== or not. If the values of the two (A == B) is not true.
operands are equal, then the
condition becomes true.

It is used to check if the values


of the two operands are equal
!= or not. If the values are not (A != B) is true.
equal, then the condition
becomes true.

It is used to check if the value


of left operand is greater than
> the value of right operand. If (A > B) is not true.
the left operand is greater, then
the condition becomes true.

< It is used to check if the value (A < B) is true.


of left operand is less than the
value of right operand. If the
left operand is lesser, then the
condition becomes true.

It is used to check if the value


of left operand is greater than
or equal to the value of right
>= operand. If the value of the left (A >= B) is not true.
operand is greater than or
equal to the value, then the
condition becomes true.

It is used to check if the value


of left operand is less than or
equal to the value of right
<= operand. If the value of the left (A <= B) is true.
operand is less than or equal to
the value, then the condition
becomes true.

Below is an example showing the working of the relational operator:

// Example of relational operators


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

Output:

8 == 10 is False(0)
8 != 10 is True(1)
8 > 10 is False(0)
8 < 10 is True(1)
8 >= 10 is False(0)
8 <=10 is True(1)

All the relational operators work in the same manner as described in the table
above.

3 simple steps to get noticed by recruiters from Top companies for your C
programming skills:
Step 1: Enroll in ‘C Basics Online Tutorial Course for Beginners’ for FREE

Step 2: Complete the 3 hours course with 90 days free access

Step 3: Post completion, Unlock the verified certificate and share on your
resume/CV/ job profile

Logical Operator With Example

In the C programming language, we have three logical operators when we need


to test more than one condition to make decisions. These logical operators are:

 && (meaning logical AND)


 || (meaning logical OR)

 ! (meaning logical NOT)

An expression containing a logical operator in C language returns either 0 or 1


depending upon the condition whether the expression results in true or false.
Logical operators are generally used for decision-making in C programming.

The table below shows all the logical operators supported by the C
programming language. We are here assuming that the variable A holds 7 and
variable B holds 3.

Operator Description Example

This is the AND operator in C


programming language. It
performs logical conjunction
of two expressions. (If both
&& ((A==7) && (B>7)) equals to 0
expressions evaluate to True,
then the result is True. If either
of the expression evaluates to
False, then the result is False)

|| It is the NOT operator in C ((A==7) || (B>7)) equals to 1


programming language. It
performs a logical disjunction
on two expressions. (If either
or both of the expressions
evaluate to True, then the
result is True)

It is the Logical NOT Operator


in C programming language. It
is used to reverse the logical
! state of its operand. If a !(A && B) is true
condition is true, then the
Logical NOT operator will
make it false and vice versa.

Following is the example that easily elaborates the working of the relational
operator:

// Working of logical operators


#include <stdio.h>
int main()
{
int a = 15, b = 15, c = 20, results;
results = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", results);
results = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", results);
results = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", results);
results = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", results);
results = !(a != b);
printf("!(a != b) is %d \n", results);
results = !(a == b);
printf("!(a == b) is %d \n", results);
return 0;
}

Output:

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

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

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

(a != b) || (c < b) is 0

!(a != b) is 1

!(a == b) is 0

 (a == b) && (c > 15) evaluates to 1 because both the operands (a == b) and


(c > b) are 1 (true).

 (a == b) && (c < b) evaluates to 0 because of the operand (c < b) is 0 (false).

 (a == b) || (c < b) evaluates to 1 because of the operand (a = b) is 1 (true).

 (a != b) || (c < b) evaluates to 0 because both the operand (a != b) and (c < b)


are 0 (false).

 !(a != b) evaluates to 1 because the operand (a != b) is 0 (false). Hence, !(a !=


b) is 1 (true).

 !(a == b) evaluates to 0 because the (a == b) is 1 (true). Hence, !(a == b) is 0


(false).
Bitwise Operator With Example

Bitwise operators are the operators which work on bits and perform the bit-by-
bit operation. Mathematical operations like addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and easier
to implement during computation and compiling of the program.

Bitwise operators are especially used in C programming for performing bit-level


operations. C programming language supports a special operator for bit
operation between two variables.

The truth tables for &, |, and ^ is provided below:

p q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Here, we will assume that A = 50 and B = 25 in binary format as follows.

A = 00110010
B = 00011001

-----------------

A&B = 00010000

A|B = 00111011

A^B = 00101011

~A = 11001101

The table provided below demonstrates the bitwise operators supported by C.


Assume variable 'A' holds 50 and variable 'B' holds 25.

Operator Description Example

Binary AND Operator.

& (A & B) = 16, i.e. 00010000


It copies a bit to the result if it
exists in both the operands.

Binary OR Operator.

| (A | B) = 59, i.e. 00111011


It copies a bit if and only if it
exists in either operand.
Binary XOR Operator.

^ (A ^ B) = 43, i.e. 00101011


It copies the bit only if it is set
in one operand but not both.

Binary One's Complement


Operator.
~ (~A ) = ~(50), i.e,. -0111101
It is unary and has the effect of
'flipping' bits.

Binary Left Shift Operator.

The value of the left operands


<< A << 2 = 200 i.e. 11001000
is moved left by the number of
bits specified by the right
operand.

Binary Right Shift Operator.

The value of the left operands


>> A >> 2 = 12 i.e., 00001100
is moved right by the number
of bits specified by the right
operand.
Misc Operator With Example

Besides all the other operators discussed above, the C programming language
also offers a few other important operators including sizeof, comma, pointer(*),
and conditional operator (?:).

Operator Description Example

sizeof(a), where a is integer, will


return 4.
The sizeof is a unary operator sizeof(b), where b is float, will
that returns the size of data return 4.
sizeof()
(constants, variables, array, sizeof(c), where c is double, will
structure, etc). return 8.
sizeof(d), where d is integer, will
return 1.

&a; returns the actual address of


It returns the address of a the variable.
&
memory location of a variable. It can be any address in the
memory like 4, 70,104.

*a; It points to the value of the


* Pointer to a variable.
variable.
conditional operator (?: in If Condition is true ? then value
?: combination) to construct X : otherwise value Y will be
conditional expressions. returned as output.

Operator Precedence in C

Operator precedence is also one of the features in the C programming language


which helps to determine the grouping of terms in an expression and decides
how an expression is evaluated as per the provided expressions. Some operators
have higher precedence than others and some have lower precedence than
others. For example, in C Language, the multiplication operator has higher
precedence than the addition operator.

Example:

For expression x = 7 + 4 * 2 , x is assigned 15 and not 22 because


Multiplication operator * has higher precedence than the addition operator +.
So, it first multiplies 4 with 2 and then adds 7 into the expression.

Provided below is a table for better understanding of operator precedence. As


we can see that the operators with the highest precedence appear at the top of
the table and those with the lowest precedence appear at the bottom of the table.
Within an expression in a C program, operators with higher precedence will be
evaluated first and the operators with lower precedence will be evaluated later.
Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right


Bitwise OR | Left to right

You might also like