Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Operators

The document discusses the different types of operators in C language including arithmetic, relational, logical, assignment, unary, conditional, and bitwise operators. It provides examples of each operator and explains their functionality. Operator precedence is also covered, with a table showing the precedence order from lowest to highest. Expressions in C are defined as sequences of operators and operands that evaluate to a value.

Uploaded by

Govindan G.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Operators

The document discusses the different types of operators in C language including arithmetic, relational, logical, assignment, unary, conditional, and bitwise operators. It provides examples of each operator and explains their functionality. Operator precedence is also covered, with a table showing the precedence order from lowest to highest. Expressions in C are defined as sequences of operators and operands that evaluate to a value.

Uploaded by

Govindan G.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11.

OPERATORS IN C LANGUAGE

 C language supports a rich set of built-in operators.
 An operator is a symbol that tells the compiler to perform certain
 mathematical or logical and relational manipulations.
 Operators are used in program to manipulate data and variables.

Types of operators:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Unary operators (Increment /Decrement operators)
6. Conditional operators
7. Bitwise operators
8. Special operators

i. Arithmetic operators
Arithmetic operators can be applied to any integer and floating point
number.
C supports all the basic arithmetic operators. The following table shows
all the basic arithmetic operators.

Operator Description
+ adds two operands
- subtract second operands from first
* multiply two operand
divide numerator by denumerator, gives quotient as
/ result
divide numerator by denumerator, gives remainder as
% result
ii. Relational operators
Also known as a comparison operator is an operator that compares two
values.
Expressions that contain relational operators are called relational
expressions.
The following table shows all relation operators supported by C.

Operator Description
Check if two operand are equal, returns 1 if both operands are
== equal otherwise it returns 0
Check if two operand are not equal. returns 1 if both operands
!= are not equal otherwise it returns 0
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand

iii. Logical operators


C language supports following 3 logical operators. Suppose a=1 and b=0,

Operator Description Example (a=1,b=0)


&& Logical AND (a && b) is false
|| Logical OR (a || b) is true
! Logical NOT (!a) is false

(iv)Assignment Operators
Assignment operator is used to assign the right side value/variable
to the left side variable.
Assignment operator is responsible for assigning values to the
variables. While the equal sign (=) is the fundamental assignment
operator.
Operator Description Example
= assigns values from right side operands to left side a=b
operand
+= adds right operand to the left operand and assign the a+=b is same as
result to left a=a+b
-= subtracts right operand from the left operand and assign a-=b is same as
the result to left operand a=a-b
*= multiply left operand with the right operand and assign a*=b is same as
the result to left operand a=a*b
/= divides left operand with the right operand and assign a/=b is same as
the result to left operand a=a/b
%= calculate modulus using two operands and assign the a%=b is same as
result to left operand a=a%b
V Unary operators (Increment /Decrement operators)
Unary operators act on single operands. C language supports three
unary operators: unary minus, increment, and decrement operators.
- unary minus(if the number is negative it becomes positive
after applying the unary minus, similarly if the number is
positive it becomes negative after applying the unary minus.
++ a=a+1 (increment and assignment)
-- a=a-1 (decrement and assignment)
a++,a-- (post increment and post decrement)
++a,--a(pre increment and pre decrement)

Operator Description
++a It will increment the value, and then assign the value to the variable.
--a It will decrement the value, and then assign the value to the variable.
a++ It will assign the value to the variable, and then increment the value.
a-- It will assign the value to the variable, and then decrement the value.
vi. Conditional operator
It is also known as ternary operator and used to evaluate
conditional expression. expr1 ? expr2 : expr3
If expr1 Condition is true ? Then value expr2 : Otherwise value expr3
num2>num3?num2:num3

vii. 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.

Operator Description a b a&b a|b a^b


& Bitwise AND 0 0 0 0 0
| Bitwise OR 0 1 0 1 1
^ Bitwise exclusive OR 1 0 0 1 1
1 1 1 1 0
>> right shift
<< left shift

viii. Special operators


The sizeof operator is a unary operator used to calculate the size of data
types.
This operator can be applied to all data types. When using this operator, the
keyword
sizeof is followed by a type name, variable, or expression.
sizeof(char) - returns 1,
E.g
int a=10;
unsigned int result;
result =sizeof(a);
result=2 which is the space required to store the variable a in memory. Since
a is an integer ,it requires 2 bytes of storage space.

Comma operator
The comma operator in c takes two operands. It works by evaluating the
first and discarding its value, and then evaluates the second and returns the
value as the result of the expression.

int a=2,b=3,x=0;
x = (++a, b+=a);
now the value of x =6.

Operator Description Example


sizeof Returns the size of an variable sizeof(x); return size of the variable x
& Returns the address of an variable &x ; return address of the variable x
* Pointer to a variable *x ; will be pointer to a variable x
C OPERATOR PRECEDENCE TABLE: Simplified table
Operator precedence determines the priority of an operator based on
which an expression will be evaluated.

Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition
operator.
Associativity indicates in what order operators of equal precedence in
an expression are applied.
In general
Comma operator has lowest precedence
Unary operators are having highest precedence
While solving expression, equal priority operators are handled on the
basis of FIFO.
S.no Operators type Operator Associativity
1 Parentheses ( ) [ ] . -> Left to Right
2 Post increment, Post decrement a++, a-- Right to left
3 Pre increment, Pre decrement ++a, --a Right to left
4 Logical NOT ! Right to Left
5 Mul, division, modulo division * / % Left to Right
6 Addition, subtract + - Left to Right
<<
7 Bitwise shift left, Bitwise shift right >> Left to right
8 Relational comparison > < <= >= Left to Right

9 Equality comparison == != Left to Right


&
10 Bitwise AND,XOR,OR ^| Left to right
11 Logical AND && Left to Right
12 Logical OR || Left to Right
13 Conditional operator ?: Right to Left
14 Assignment = Right to Left
15 comma , Left to right

Expressions :-
Expression is a sequence of operators and operands that specifies
computation of a value.

In c every expression evaluates to a value that is every expression results


in some value of a certain type that can be assigned to a variable.

a=9-12/3+3*2-1
a=?
a=9-4+3*2-1
a=9-4+6-1
a=5+6-1
a=11-1
a=10
A simple program using Expressions:-
#include<stdio.h>
int main()
{
int num1=10,num2=20;
int result;
result=num1*5 +num2*5+num1*5;
printf(“result is %d”,result);
return 0;

O/P:-

result is 200

You might also like