[go: up one dir, main page]

0% found this document useful (0 votes)
14 views14 pages

Operators in C

The document provides an overview of operators in C programming, detailing their types, such as arithmetic, relational, logical, bitwise, assignment, and other operators. It includes examples of each operator type, their usage, and the concept of operator precedence. Additionally, it explains how to perform operations using these operators with sample code snippets.

Uploaded by

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

Operators in C

The document provides an overview of operators in C programming, detailing their types, such as arithmetic, relational, logical, bitwise, assignment, and other operators. It includes examples of each operator type, their usage, and the concept of operator precedence. Additionally, it explains how to perform operations using these operators with sample code snippets.

Uploaded by

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

OPERATORS IN C:

● Operators are symbols that help us to perform specific mathematical and


logical computations on operands.
● In other words, we can say that an operator operates the operands.
● For example, ‘+’ is an operator used for addition, as shown below:

c = a + b;

● Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the operands
‘a’ and ‘b’.

TYPES:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operators:
● These operators are used to perform arithmetic/mathematical operations on
operands.

● Examples: (+, -, *, /, %,++,–). Arithmetic operators are of two types:

a) Unary Operators:
● Operators that operate or work with a single operand are unary operators.
● For example: Increment(++) and Decrement(- - ) Operators
int val = 5;

++val; // 6

b) Binary Operators:
● Operators that operate or work with two operands are binary operators.
● For example: Addition(+), Subtraction(-), multiplication(*), Division(/)
operators
int a = 7;

int b = 2;

Example:

A=5 B=5

Operator Description Example

A + B =
+ Addition between 2 operands.
10

− Subtraction between 2 operands. A−B=0

A * B =
* Multiplication between 2 operands.
25

/ Division between 2 operands. B/A=1

Modulus Operator and remainder of after an integer


% B%A=0
division.

++ Increases the integer value by one. A++ = 6

— Decreases the integer value by one. B– = 4

int c= a+b; // 9
Example:

int main()
{
int a = 10,b = 15, result;
result = a+b;
printf("Addition: a+b = %d \n",result);
result = a-b;
printf("Subtraction: a-b = %d \n",result);
result = a*b;
printf("Multiplication: a*b = %d \n",result);
result = a/b;
printf("Division: a/b = %d \n",result);
result = a%b;
printf("Modulo Division: %d \n",result);
result = ++a;
printf("Increment the value of a by 1: %d \n",result);
result = --b;
printf("Decremented the value of b by 1: %d \n",result);
return 0;
}
Output:

Addition: a+b = 25

Subtraction: a-b = -5

Multiplication: a*b = 150

Division: a/b = 0

Modulo Division: 10

Increment the value of a by 1: 11

Decremented the value of b by 1: 14

2. Relational Operators:
● These are used for the comparison of the values of two operands.

● For example, checking if one operand is equal to the other operand or not, an
operand is greater than the other operand or not, etc.

● Some of the relational operators are (==, >= , <= )

int a = 3;

int b = 5;

a < b;

// operator to check if a is smaller than b

Operator Description Example

== Equal to 5==5 will be 1

> Greater than 5>6 will be 0


< Less than 6<7 will be 1

>= Greater than equal to 2 >= 1 will be 1

<= Less than equal to 1 <= 2 will be 1

!= Not equal to 5 != 6 will be 1

3. Logical Operators:
● Logical Operators are used to combine two or more conditions/constraints or
to complement the evaluation of the original condition in consideration.
● The result of the operation of a logical operator is a Boolean value
either true or false.
● For example, the logical AND represented as ‘&&’ operator in C returns
true when both the conditions under consideration are satisfied. Otherwise, it
returns false.
● Therefore, a && b returns true when both a and b are true (i.e. non-zero)
(4 != 5) && (4 < 5); // true

Operator Description

&& (Logical AND) True only if all conditions satisfy.

|| (Logical OR) True only if either one condition satisfies.

! (Logical Not) True only if the operand is 0.

4. Bitwise Operators:
● The Bitwise operators are used to perform BIT-LEVEL OPERATIONS on
the operands.
● The operators are first converted to bit-level and then the calculation is
performed on the operands.
Operator Also known as
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary Ones Complement Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator
● The mathematical operations such as addition, subtraction, multiplication, etc.
can be performed at bit-level for faster processing.
● For example, the bitwise AND represented as & operator in C takes two
numbers as operands and does AND on every bit of two numbers.
● The result of AND is 1 only if both bits are 1.
int a = 5, b = 9; // a = 5(00000101),

b = 9(00001001)

(a ^ b); // 00001100

(~a); // 11111010

5. Assignment Operators:
● Assignment operators are used to assigning value to a variable.

● The left side operand of the assignment operator is a variable and the right
side operand of the assignment operator is a value.
● The value on the right side must be of the same data type as the variable on
the left side otherwise the compiler will raise an error.

● Different types of assignment operators are shown below:


a. “=”: This is the simplest assignment operator. This operator is used to
assign the value on the right to the variable on the left.
For example:
a = 10;
b = 20;
ch = 'y';

Operator Example
= a=b or b=a
+= a += b or a = a+b
-= a -=b or a = a-b
*= a *= b or a = a*b
/= a /= b or a = a/b

b. “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first
adds the current value of the variable on left to the value on the right and then
assigns the result to the variable on the left.
For example:
(a += b) can be written as (a = a + b)

If initially value stored in a is 5. Then (a += 6) = 11.

(a -= b) can be written as (a = a - b)

c. “-=”: This operator is a combination of ‘-‘ and ‘=’ operators. This operator first
subtracts the value on the right from the current value of the variable on left and
then assigns the result to the variable on the left.
For example:
(a -= b) can be written as (a = a - b)

If initially value stored in a is 8. Then (a -= 6) = 2.

d. “*=”: This operator is a combination of ‘*’ and ‘=’ operators. This operator first
multiplies the current value of the variable on left to the value on the right and then
assigns the result to the variable on the left.
For example:
(a *= b) can be written as (a = a * b)

If initially, the value stored in a is 5. Then (a *= 6) = 30

e. “/=”: This operator is a combination of ‘/’ and ‘=’ operators. This operator first
divides the current value of the variable on left by the value on the right and then
assigns the result to the variable on the left.
For example:

(a /= b) can be written as (a = a / b)

If initially, the value stored in a is 6. Then (a /= 2) = 3.

6. Other Operators:

Apart from the above operators, there are some other operators available in C used
to perform some specific tasks.

a. sizeof operator:

● sizeof is much used in the C programming language.


● It is a compile-time unary operator which can be used to compute the size of
its operand.
● The result of sizeof is of the unsigned integral type which is usually denoted
by size_t.
● Basically, the sizeof the operator is used to compute the size of the variable
b. Comma Operator:

● The comma operator (represented by the token) is a binary operator that


evaluates its first operand and discards the result, it then evaluates the second
operand and returns this value (and type).
● The comma operator has the lowest precedence of any C operator.
● Comma acts as both operator and separator.

c. Conditional Operator:

● The conditional operator is of the form Expression1? Expression2:


Expression3.
● Here, Expression1 is the condition to be evaluated. If the
condition(Expression1) is True then we will execute and return the result of
Expression2 otherwise if the condition(Expression1) is false then we will
execute and return the result of Expression3.
● We may replace the use of if..else statements with conditional operators.

OPERATOR PRECEDENCE
● Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher
precedence than others;
● For example, the multiplication operator has a higher precedence than the
addition operator.
● The below table describes the precedence order and associativity of operators
in C. The precedence of the operator decreases from top to bottom.

Precedence Operator Description Associativity

1 () Parentheses (function call) left-to-right


Precedence Operator Description Associativity

[] Brackets (array subscript)

. Member selection via object name

-> Member selection via a pointer

++/– Postfix increment/decrement

++/– Prefix increment/decrement right-to-left

+/- Unary plus/minus

!~ Logical negation/bitwise complement

Cast (convert value to temporary value


(type) of type)

* Dereference

& Address (of operand)

Determine size in bytes on this


2 sizeof implementation

3 *,/,% Multiplication/division/modulus left-to-right

4 +/- Addition/subtraction left-to-right


Precedence Operator Description Associativity

5 << , >> Bitwise shift left, Bitwise shift right left-to-right

< , <= Relational less than/less than or equal to left-to-right

Relational greater than/greater than or


6 > , >= equal to left-to-right

7 == , != Relational is equal to/is not equal to left-to-right

8 & Bitwise AND left-to-right

9 ^ Bitwise exclusive OR left-to-right

10 | Bitwise inclusive OR left-to-right

11 && Logical AND left-to-right

12 || Logical OR left-to-right

13 ?: Ternary conditional right-to-left

= Assignment right-to-left

+= , -= Addition/subtraction assignment

*= , /= Multiplication/division assignment

14 %= , &= Modulus/bitwise AND assignment


Precedence Operator Description Associativity

Bitwise exclusive/inclusive OR
^= , |= assignment

<>= Bitwise shift left/right assignment

15 , expression separator left-to-right

Example for operator precedence:

#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50

You might also like