SlideShare a Scribd company logo
Presented by :
Er. Anupam Sharma
Assistant Professor
Applied Science(CSE) ,CGC-TC
Operators in C by Anupam Sharma
Operators
The symbols which are used to perform logical and
mathematical operations in a C program are called C
operators.
These C operators join individual constants and
variables to form expressions.
Consider the expression A + B * 5.
where, +, * are operators, A, B are variables, 5 is
constant and A + B * 5 is an expression.
Operators in C by Anupam Sharma
Types of Operators based on number of
operands
.
Operators
UNAR
Y
BINARY
TERN
A
RY
Operators in C by Anupam Sharma
Unary Operators
• A unary operator is one which operates on one
value or operand. The minus sign (-) plays a
dual role, it is used for subtraction as a binary
operator and for negation as a unary operator.
This operator has a precedence higher than the
rest of the arithmetic operators.
• result = -x * y;
• in the above expression, if x has a value 20 and
y has a value 2, then result will contain a
negative value of 40 which is -40.
10/19/2015
Operators in C by Anupam Sharma
Binary and Ternary Operators
• Binary
operators?
• Ternary
operators?
Operators in C by Anupam Sharma
Types of ‘C’
operators
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and Decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
Operators in C by Anupam Sharma
1. Arithmetic operator
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulous or modulo division
Operators in C by Anupam Sharma
Example Program
#include<stdio.h>
int main()
{
int a, b, add, sub, mul, div, rem;
printf("Enter a, b values : ");
scanf("%d%d",&a,&b); // Reading two values
add=a+b; // Addition Operator
sub=a-b; // Subtraction Operator
mul=a*b; // Multiplication Operator
div=a/b; // Division Operator
rem=a%b; // Remainder (Modulo) Operator
printf("Result of addition is=%dn", add);
printf("Result of subtraction=%dn", sub);
printf("Result of multiplication is=%dn", mul);
printf("Result of division is=%dn", div);
printf("Result of remainder=%dn",rem);
return 0; }
Operators in C by Anupam Sharma
10/19/2015
Operators in C by Anupam Sharma
2. Relational
operator
C supports six Relational Operators
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
Operators in C by Anupam Sharma
• Suppose that a and b are integer variables
whose values are 100 and 4, respectively.
Several arithmetic expressions involving these
variables are shown below, together with their
resulting values.
10/19/2015
a=100, b=4
Operators in C by Anupam Sharma
Example Program
#include<stdio.h>
void main()
{
int a, b;
printf(“Enter values for a and b : ");
scanf("%d %d", &a, &b);
printf("n The < value of a is %d", a<b);
printf("n The <= value of a is %d", a<=b);
printf("n The > value of a is %d", a>b);
printf("n The >= value of a is %d", a>=b);
printf("n The == value of a is %d", a==b);
printf("n The != value of a is %d", a!=b);
}
Operators in C by Anupam Sharma
3.Logical operators
• Logical Operators
– &&, || and ! are the three logical operators.
– expr1 && expr2 has a value 1 if expr1 and expr2 both are
nonzero i.e. if both have values 1(true)
– expr1 || expr2 has a value 1 if either expr1 or expr2 or both
are nonzero i.e 1(true).
– !expr1 has a value 1 if expr1 is zero else 0.
– Example
– if ( marks >= 40 && attendance >= 75 ) grade = ‘P’
– If ( marks < 40 || attendance < 75 ) grade = ‘N’
Operators in C by Anupam Sharma
10/19/2015
Relational And Logical Operators
Operators in C by Anupam Sharma
Logical Operators Example Program
#include<stdio.h>
void main()
{
int a, b;
printf(“Enter values for a and b : ");
scanf(“%d %d", &a, &b);
printf("n %d",(a<b)&&(a!=b));
printf("n %d",(a<b)||(b<a));
printf("n %d",!(a==b));
}
Operators in C by Anupam Sharma
True
!True i.e !1 =0
Operators in C by Anupam Sharma
4. Assignment operators
• Assignment operators are used to assign the result of an expression
to a variable.
• C has a set of ‘shorthand’ assignment operator :
variable name =expression;
Exam - a + = 3;
a = a + 3;
Both are same.
Left side must be an object that
can receive a value
Operators in C by Anupam Sharma
Shorthand Assignment operators
Simple assignment
operator
Shorthand operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
Operators in C by Anupam Sharma
Assignment Operators Example
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter the values for a and b : ");
scanf("%d %d",&a,&b);
printf("n the values of= is:%d",c=a+b);
printf("n the values of +=is:%d",c+=b);
printf("n the value of-= is:%d",c-=a);
printf("n the value of *=is:%d",c*=a);
printf("n the value of /=is:%d",c/=b);
printf("n the value of %=is:%d",c%=b);
}
Operators in C by Anupam Sharma
5. Increment and decrement operators.
• Increment Operator ++
a=10;
a++ =10 (post increment but in memory its value is 11)
when you will again call value of a, then a=11
• Decrement Operator --
b=5;
b-- =4 in memory but output will be 5; when you will call b
again then value will be 4.
• Similarly increment and decrement operator is used in
subscripted variables as:
a[ i++]=5;
is equivalent to
a[ i]=5;
i=i+1;
Operators in C by Anupam Sharma
INCREMENT & DECREMENT OPERATORS EXAMPLE
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the values for a and b :");
scanf("%d %d", &a, &b);
printf("n The value of c is %d", c=++a);
printf("n The value of c is %d", c=a++);
printf("n The value of c is %d", c=--b);
printf("n The value of c is %d", c=b--);
}
Operators in C by Anupam Sharma
6. Conditional operator
• The conditional expression can be used as
shorthand for some if-else statements. It is a
ternary operator.
• This operator consist of two symbols: the question
mark (?) and the colon (:).
for example:
a=11; b=20;
x=(a>b) ? a : b;
Identifier Test Expression
Exp 1: Exp 2
Operators in C by Anupam Sharma
Conditional Operators Example
#include<stdio.h>
void main()
{
int a, b, x;
printf("Enter the values of a add b : ");
scanf("%d %d", &a, &b);
x=(a>b)?a:b;
printf("Biggest Value is :%d",x);
}
Operators in C by Anupam Sharma
Operators in C by Anupam Sharma
7. Bitwise operator
• C supports bitwise operators for manipulation of data at bit level.
• Bitwise operators may not be applied to float or double.
Operator Meaning
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise exclusive OR operator
~ Binary One's Complement
Operator is a unary operator
used to reverse the bits of an
expression
<< Left shift operator
>> Right shift operator
x y x & y x | y x ^ y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Operators in C by Anupam Sharma
Simple program that demonstrates bitwise logical
operators.
#include <stdio.h>
main()
{ unsigned int x = 48; /* 48 = 0011 0000 */
unsigned int y = 13; /* 13 = 0000 1101 */
int z = 0; z =x & y; /* 0 = 0000 0000 */
printf("Bitwise AND Operator - x & y = %dn", z );
z = x | y; /* 61 = 0011 1101 */
printf("Bitwise OR Operator - x | y = %dn", z );
z= x^y; /* 61 = 0011 1101 */
printf("Bitwise XOR Operator- x^y= %dn", z);
z = ~x; /*-61 = 1100 0011 */
printf("Bitwise One's Complement Operator - ~x = %dn", z);
z = x << 2; /* 192 = 1100 0000 */
printf("Bitwise Left Shift Operator x << 2= %dn", z );
z= x >> 2; /* 12 = 0000 1100 */
printf ("Bitwise Right Shift Operator x >> 2= %dn", z ); }
Operators in C by Anupam Sharma
Output:
Bitwise AND Operator - x & y = 0
Bitwise OR Operator - x | y = 61
Bitwise XOR Operator- x^y= 61
Bitwise One's Complement Operator - ~x = -49
Bitwise Left Shift Operator x << 2= 192
Bitwise Right Shift Operator x >> 2= 12
Operators in C by Anupam Sharma
8. Special operators
C supports some special operators such as:
• comma operator “,”
int a=5,b=6;
• size of operator “sizeof()”
• Address operator “&”
• pointer operator “*”
• member selection operator “. and -> ”
Operators in C by Anupam Sharma
Precedence of operators
• Precedence establishes the hierarchy of one set of operators
over another when an arithmetic expression has to be
evaluated.
• It refers to the order in which c evaluates operators.
• The evaluation of operators in an arithmetic
expression takes place from left to right for operators having
equal precedence .
Operators in C by Anupam Sharma
Precedence of operators
BODMAS RULE-
Brackets of Division Multiplication AdditionSubtraction
Brackets will have the highest precedence and have to be evaluated
first, then comes of , then comes division, multiplication, addition
and finally subtraction.
C language uses some rules in evaluating the expressions and they
are called as precedence rules or sometimes also referred to as
hierarchy of operations, with some operators with highest
precedence and some with least.
The 2 distinct priority levels of arithmetic operators in c are-
Highest priority : * / %
Lowest priority : + -
Operators in C by Anupam Sharma
Associativity of operators
•
•
•
•
•
•
• Associativity tells how an operator associates with its operands.
for eg:
Associativity means whether an expression like x R y R z
(where R is a operator such as + or <= ) should be evaluated
`left-to-right' i.e. as (x R y) R z or `right-to-left' i.e. as x R (y
R z)
The assignment operator = associates from right toleft.
Hence the expression on the right is evaluated first and its valueis
assigned to the variable on the left.
Associativity also refers to the order in which c evaluates operators in
an expression having same precedence.
Such type of operator can operate either left to right or vice versa.
The operator () function call has highest precedence & the comma
operator has lowest precedence
All unary , conditional & assignment operators associateRIGHT
TO LEFT .
All other remaining operators associate LEFT TORIGHT
Operators in C by Anupam Sharma
Rules for evaluation of expression
1. First parenthesized sub expression from left to right are
evaluated.
2. If parentheses are nested, the evaluation begins with the
innermost sub expression
3. The precedence rule is applied in determining the order of
application of operators in evaluating sub expressions
4. The associatively rule is applied when 2 or more operators
of the same precedence level appear in a sub expression.
5. Arithmetic expressions are evaluated from left to right using
the rules of precedence
6. When parentheses are used, the expressions within parentheses
assume highest priority
Operators in C by Anupam Sharma
Hierarchy of operators
Operator Description Associativity
( ), [ ] Function call, array element
reference
Left to Right
+, -, ++, - -
,!,~,*,&
Unary plus, minus,
increment, decrement,
logical negation, 1’s
complement, pointer
reference, address
Right to Left
*, / , % Multiplication,
division, modulus
Left to Right
Operators in C by Anupam Sharma
Type Casting
• Type casting is a way to convert a variable from
one data type to another data type.
• When variables and constants of different
types are combined in an expression then they
are converted to same data type. The process
of converting one predefined type into another is
called type conversion.
DATATYPE 1 DATATYPE 2
Operators in C by Anupam Sharma
Implicit Type Casting
• When the type conversion is performed
automatically by the compiler without
programmers intervention, such type of
conversion is known as implicit type conversion
or type promotion.
• For example when you add values having
different data types, both values are first
converted to the same type: when a short int
value and an int value are added together, the
short int value is converted to the int type.
int + short int  int
Operators in C by Anupam Sharma
• C does implicit DataType conversion when the need
arises.
• When a floating point value is assigned to an integer
variable,the decimal portion is truncated.
When a value 156.43 is assigned to an integer variable, 15
is stored and the decimal portion is discarded.
If an integer 200 is assigned to a floating point variable,the
value is converted to 200.000000 and stored.
(integer type variable)a= 156.43 156.43
(float type variable) float b = 200  200.000000
Operators in C by Anupam Sharma
Explicit Type Casting
• The type conversion performed by the programmer
by posing the data type of the expression of specific
type is known as explicit type conversion.
• Type casting in c is done in the following
form:(data_type) expression;
where, data_type is any valid c data type, and
expression may be constant, variable or expression.
For example:
x=(int)a+b*d;
Operators in C by Anupam Sharma
Example
#include <stdio.h>
main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean :
%fn", mean );
}
Output is
Value of mean : 3.400000
It should be noted here
that the cast operator
has precedence over
division,
so the value of sum is
first converted to type
double and finally it
gets divided by count
yielding a double
value.
Operators in C by Anupam Sharma
Operators in C by Anupam Sharma
Rules for Implicit Type Casting
The following rules have to be followed while
converting the expression from one type to
another to avoid the loss of information:
• All integer types to be converted to float.
• All float types to be converted to double.
• All character types to be converted to integer.
Operators in C by Anupam Sharma
Thank you
Operators in C by Anupam Sharma

More Related Content

What's hot

2. operators in c
2. operators in c2. operators in c
2. operators in c
amar kakde
 
Theory3
Theory3Theory3
Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++bajiajugal
 
C operators
C operatorsC operators
C operators
GPERI
 
Operators
OperatorsOperators
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
zeeshan turi
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operatorsdishti7
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
Rohit Shrivastava
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
REHAN IJAZ
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
Prabhu Govind
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
PreSolutions Softwares
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATOR
rricky98
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++Pranav Ghildiyal
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
Shobi P P
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
Online
 

What's hot (20)

2. operators in c
2. operators in c2. operators in c
2. operators in c
 
Theory3
Theory3Theory3
Theory3
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
C operators
C operatorsC operators
C operators
 
Operators
OperatorsOperators
Operators
 
Expressions in c++
 Expressions in c++ Expressions in c++
Expressions in c++
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATOR
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 

Similar to Operators in c by anupam

PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
Nithya K
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
MaryJacob24
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
ramanathan2006
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operators
Amrinder Sidhu
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
ruchisuru20001
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
Kaushal Patel
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
Ashim Lamichhane
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
Hemantha Kulathilake
 
C operators
C operators C operators
C operators
AbiramiT9
 
2. operator
2. operator2. operator
2. operator
Shankar Gangaju
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
Ralph Weber
 
C programming
C programmingC programming
C programming
Xad Kuain
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdf
RichardMathengeSPASP
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
HARSHSHARMA840
 
Operators
OperatorsOperators
Operators
VijayaLakshmi506
 
C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh
AnkitSinghRajput35
 
Coper in C
Coper in CCoper in C
Coper in C
thirumalaikumar3
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)jahanullah
 

Similar to Operators in c by anupam (20)

PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operators
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 
C operators
C operators C operators
C operators
 
2. operator
2. operator2. operator
2. operator
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
C programming
C programmingC programming
C programming
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdf
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
Operators
OperatorsOperators
Operators
 
C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh
 
Coper in C
Coper in CCoper in C
Coper in C
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 

More from CGC Technical campus,Mohali

Gender Issues CS.pptx
Gender Issues CS.pptxGender Issues CS.pptx
Gender Issues CS.pptx
CGC Technical campus,Mohali
 
Intellectual Property Rights.pptx
Intellectual Property Rights.pptxIntellectual Property Rights.pptx
Intellectual Property Rights.pptx
CGC Technical campus,Mohali
 
Cyber Safety ppt.pptx
Cyber Safety ppt.pptxCyber Safety ppt.pptx
Cyber Safety ppt.pptx
CGC Technical campus,Mohali
 
Python Modules .pptx
Python Modules .pptxPython Modules .pptx
Python Modules .pptx
CGC Technical campus,Mohali
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
CGC Technical campus,Mohali
 
Arrays in c
Arrays in cArrays in c
Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
Datatypes in c
Datatypes in cDatatypes in c
Fundamentals of-computer
Fundamentals of-computerFundamentals of-computer
Fundamentals of-computer
CGC Technical campus,Mohali
 
Searching
Searching Searching
File handling-c
File handling-cFile handling-c
Structure in C language
Structure in C languageStructure in C language
Structure in C language
CGC Technical campus,Mohali
 
Function in c program
Function in c programFunction in c program
Function in c program
CGC Technical campus,Mohali
 
Function in c
Function in cFunction in c
string in C
string in Cstring in C
C arrays
C arraysC arrays
Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)
CGC Technical campus,Mohali
 
data analysis and report wring in research (Section d)
data analysis and report wring  in research (Section d)data analysis and report wring  in research (Section d)
data analysis and report wring in research (Section d)
CGC Technical campus,Mohali
 
Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )
CGC Technical campus,Mohali
 

More from CGC Technical campus,Mohali (20)

Gender Issues CS.pptx
Gender Issues CS.pptxGender Issues CS.pptx
Gender Issues CS.pptx
 
Intellectual Property Rights.pptx
Intellectual Property Rights.pptxIntellectual Property Rights.pptx
Intellectual Property Rights.pptx
 
Cyber Safety ppt.pptx
Cyber Safety ppt.pptxCyber Safety ppt.pptx
Cyber Safety ppt.pptx
 
Python Modules .pptx
Python Modules .pptxPython Modules .pptx
Python Modules .pptx
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
Fundamentals of-computer
Fundamentals of-computerFundamentals of-computer
Fundamentals of-computer
 
Searching
Searching Searching
Searching
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Intro
IntroIntro
Intro
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in c
Function in cFunction in c
Function in c
 
string in C
string in Cstring in C
string in C
 
C arrays
C arraysC arrays
C arrays
 
Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)
 
data analysis and report wring in research (Section d)
data analysis and report wring  in research (Section d)data analysis and report wring  in research (Section d)
data analysis and report wring in research (Section d)
 
Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )
 

Recently uploaded

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 

Recently uploaded (20)

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 

Operators in c by anupam

  • 1. Presented by : Er. Anupam Sharma Assistant Professor Applied Science(CSE) ,CGC-TC Operators in C by Anupam Sharma
  • 2. Operators The symbols which are used to perform logical and mathematical operations in a C program are called C operators. These C operators join individual constants and variables to form expressions. Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression. Operators in C by Anupam Sharma
  • 3. Types of Operators based on number of operands . Operators UNAR Y BINARY TERN A RY Operators in C by Anupam Sharma
  • 4. Unary Operators • A unary operator is one which operates on one value or operand. The minus sign (-) plays a dual role, it is used for subtraction as a binary operator and for negation as a unary operator. This operator has a precedence higher than the rest of the arithmetic operators. • result = -x * y; • in the above expression, if x has a value 20 and y has a value 2, then result will contain a negative value of 40 which is -40. 10/19/2015 Operators in C by Anupam Sharma
  • 5. Binary and Ternary Operators • Binary operators? • Ternary operators? Operators in C by Anupam Sharma
  • 6. Types of ‘C’ operators 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and Decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators Operators in C by Anupam Sharma
  • 7. 1. Arithmetic operator + Addition - Subtraction * Multiplication / Division % Modulous or modulo division Operators in C by Anupam Sharma
  • 8. Example Program #include<stdio.h> int main() { int a, b, add, sub, mul, div, rem; printf("Enter a, b values : "); scanf("%d%d",&a,&b); // Reading two values add=a+b; // Addition Operator sub=a-b; // Subtraction Operator mul=a*b; // Multiplication Operator div=a/b; // Division Operator rem=a%b; // Remainder (Modulo) Operator printf("Result of addition is=%dn", add); printf("Result of subtraction=%dn", sub); printf("Result of multiplication is=%dn", mul); printf("Result of division is=%dn", div); printf("Result of remainder=%dn",rem); return 0; } Operators in C by Anupam Sharma
  • 9. 10/19/2015 Operators in C by Anupam Sharma
  • 10. 2. Relational operator C supports six Relational Operators < Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to == Is equal to != Is not equal to Operators in C by Anupam Sharma
  • 11. • Suppose that a and b are integer variables whose values are 100 and 4, respectively. Several arithmetic expressions involving these variables are shown below, together with their resulting values. 10/19/2015 a=100, b=4 Operators in C by Anupam Sharma
  • 12. Example Program #include<stdio.h> void main() { int a, b; printf(“Enter values for a and b : "); scanf("%d %d", &a, &b); printf("n The < value of a is %d", a<b); printf("n The <= value of a is %d", a<=b); printf("n The > value of a is %d", a>b); printf("n The >= value of a is %d", a>=b); printf("n The == value of a is %d", a==b); printf("n The != value of a is %d", a!=b); } Operators in C by Anupam Sharma
  • 13. 3.Logical operators • Logical Operators – &&, || and ! are the three logical operators. – expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero i.e. if both have values 1(true) – expr1 || expr2 has a value 1 if either expr1 or expr2 or both are nonzero i.e 1(true). – !expr1 has a value 1 if expr1 is zero else 0. – Example – if ( marks >= 40 && attendance >= 75 ) grade = ‘P’ – If ( marks < 40 || attendance < 75 ) grade = ‘N’ Operators in C by Anupam Sharma
  • 14. 10/19/2015 Relational And Logical Operators Operators in C by Anupam Sharma
  • 15. Logical Operators Example Program #include<stdio.h> void main() { int a, b; printf(“Enter values for a and b : "); scanf(“%d %d", &a, &b); printf("n %d",(a<b)&&(a!=b)); printf("n %d",(a<b)||(b<a)); printf("n %d",!(a==b)); } Operators in C by Anupam Sharma
  • 16. True !True i.e !1 =0 Operators in C by Anupam Sharma
  • 17. 4. Assignment operators • Assignment operators are used to assign the result of an expression to a variable. • C has a set of ‘shorthand’ assignment operator : variable name =expression; Exam - a + = 3; a = a + 3; Both are same. Left side must be an object that can receive a value Operators in C by Anupam Sharma
  • 18. Shorthand Assignment operators Simple assignment operator Shorthand operator a = a+1 a + =1 a = a-1 a - =1 a = a* (m+n) a * = m+n a = a / (m+n) a / = m+n a = a %b a %=b Operators in C by Anupam Sharma
  • 19. Assignment Operators Example #include<stdio.h> void main() { int a, b, c; printf("Enter the values for a and b : "); scanf("%d %d",&a,&b); printf("n the values of= is:%d",c=a+b); printf("n the values of +=is:%d",c+=b); printf("n the value of-= is:%d",c-=a); printf("n the value of *=is:%d",c*=a); printf("n the value of /=is:%d",c/=b); printf("n the value of %=is:%d",c%=b); } Operators in C by Anupam Sharma
  • 20. 5. Increment and decrement operators. • Increment Operator ++ a=10; a++ =10 (post increment but in memory its value is 11) when you will again call value of a, then a=11 • Decrement Operator -- b=5; b-- =4 in memory but output will be 5; when you will call b again then value will be 4. • Similarly increment and decrement operator is used in subscripted variables as: a[ i++]=5; is equivalent to a[ i]=5; i=i+1; Operators in C by Anupam Sharma
  • 21. INCREMENT & DECREMENT OPERATORS EXAMPLE #include<stdio.h> void main() { int a,b,c; printf("Enter the values for a and b :"); scanf("%d %d", &a, &b); printf("n The value of c is %d", c=++a); printf("n The value of c is %d", c=a++); printf("n The value of c is %d", c=--b); printf("n The value of c is %d", c=b--); } Operators in C by Anupam Sharma
  • 22. 6. Conditional operator • The conditional expression can be used as shorthand for some if-else statements. It is a ternary operator. • This operator consist of two symbols: the question mark (?) and the colon (:). for example: a=11; b=20; x=(a>b) ? a : b; Identifier Test Expression Exp 1: Exp 2 Operators in C by Anupam Sharma
  • 23. Conditional Operators Example #include<stdio.h> void main() { int a, b, x; printf("Enter the values of a add b : "); scanf("%d %d", &a, &b); x=(a>b)?a:b; printf("Biggest Value is :%d",x); } Operators in C by Anupam Sharma
  • 24. Operators in C by Anupam Sharma
  • 25. 7. Bitwise operator • C supports bitwise operators for manipulation of data at bit level. • Bitwise operators may not be applied to float or double. Operator Meaning & Bitwise AND operator | Bitwise OR operator ^ Bitwise exclusive OR operator ~ Binary One's Complement Operator is a unary operator used to reverse the bits of an expression << Left shift operator >> Right shift operator x y x & y x | y x ^ y 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 Operators in C by Anupam Sharma
  • 26. Simple program that demonstrates bitwise logical operators. #include <stdio.h> main() { unsigned int x = 48; /* 48 = 0011 0000 */ unsigned int y = 13; /* 13 = 0000 1101 */ int z = 0; z =x & y; /* 0 = 0000 0000 */ printf("Bitwise AND Operator - x & y = %dn", z ); z = x | y; /* 61 = 0011 1101 */ printf("Bitwise OR Operator - x | y = %dn", z ); z= x^y; /* 61 = 0011 1101 */ printf("Bitwise XOR Operator- x^y= %dn", z); z = ~x; /*-61 = 1100 0011 */ printf("Bitwise One's Complement Operator - ~x = %dn", z); z = x << 2; /* 192 = 1100 0000 */ printf("Bitwise Left Shift Operator x << 2= %dn", z ); z= x >> 2; /* 12 = 0000 1100 */ printf ("Bitwise Right Shift Operator x >> 2= %dn", z ); } Operators in C by Anupam Sharma
  • 27. Output: Bitwise AND Operator - x & y = 0 Bitwise OR Operator - x | y = 61 Bitwise XOR Operator- x^y= 61 Bitwise One's Complement Operator - ~x = -49 Bitwise Left Shift Operator x << 2= 192 Bitwise Right Shift Operator x >> 2= 12 Operators in C by Anupam Sharma
  • 28. 8. Special operators C supports some special operators such as: • comma operator “,” int a=5,b=6; • size of operator “sizeof()” • Address operator “&” • pointer operator “*” • member selection operator “. and -> ” Operators in C by Anupam Sharma
  • 29. Precedence of operators • Precedence establishes the hierarchy of one set of operators over another when an arithmetic expression has to be evaluated. • It refers to the order in which c evaluates operators. • The evaluation of operators in an arithmetic expression takes place from left to right for operators having equal precedence . Operators in C by Anupam Sharma
  • 30. Precedence of operators BODMAS RULE- Brackets of Division Multiplication AdditionSubtraction Brackets will have the highest precedence and have to be evaluated first, then comes of , then comes division, multiplication, addition and finally subtraction. C language uses some rules in evaluating the expressions and they are called as precedence rules or sometimes also referred to as hierarchy of operations, with some operators with highest precedence and some with least. The 2 distinct priority levels of arithmetic operators in c are- Highest priority : * / % Lowest priority : + - Operators in C by Anupam Sharma
  • 31. Associativity of operators • • • • • • • Associativity tells how an operator associates with its operands. for eg: Associativity means whether an expression like x R y R z (where R is a operator such as + or <= ) should be evaluated `left-to-right' i.e. as (x R y) R z or `right-to-left' i.e. as x R (y R z) The assignment operator = associates from right toleft. Hence the expression on the right is evaluated first and its valueis assigned to the variable on the left. Associativity also refers to the order in which c evaluates operators in an expression having same precedence. Such type of operator can operate either left to right or vice versa. The operator () function call has highest precedence & the comma operator has lowest precedence All unary , conditional & assignment operators associateRIGHT TO LEFT . All other remaining operators associate LEFT TORIGHT Operators in C by Anupam Sharma
  • 32. Rules for evaluation of expression 1. First parenthesized sub expression from left to right are evaluated. 2. If parentheses are nested, the evaluation begins with the innermost sub expression 3. The precedence rule is applied in determining the order of application of operators in evaluating sub expressions 4. The associatively rule is applied when 2 or more operators of the same precedence level appear in a sub expression. 5. Arithmetic expressions are evaluated from left to right using the rules of precedence 6. When parentheses are used, the expressions within parentheses assume highest priority Operators in C by Anupam Sharma
  • 33. Hierarchy of operators Operator Description Associativity ( ), [ ] Function call, array element reference Left to Right +, -, ++, - - ,!,~,*,& Unary plus, minus, increment, decrement, logical negation, 1’s complement, pointer reference, address Right to Left *, / , % Multiplication, division, modulus Left to Right Operators in C by Anupam Sharma
  • 34. Type Casting • Type casting is a way to convert a variable from one data type to another data type. • When variables and constants of different types are combined in an expression then they are converted to same data type. The process of converting one predefined type into another is called type conversion. DATATYPE 1 DATATYPE 2 Operators in C by Anupam Sharma
  • 35. Implicit Type Casting • When the type conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion or type promotion. • For example when you add values having different data types, both values are first converted to the same type: when a short int value and an int value are added together, the short int value is converted to the int type. int + short int  int Operators in C by Anupam Sharma
  • 36. • C does implicit DataType conversion when the need arises. • When a floating point value is assigned to an integer variable,the decimal portion is truncated. When a value 156.43 is assigned to an integer variable, 15 is stored and the decimal portion is discarded. If an integer 200 is assigned to a floating point variable,the value is converted to 200.000000 and stored. (integer type variable)a= 156.43 156.43 (float type variable) float b = 200  200.000000 Operators in C by Anupam Sharma
  • 37. Explicit Type Casting • The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion. • Type casting in c is done in the following form:(data_type) expression; where, data_type is any valid c data type, and expression may be constant, variable or expression. For example: x=(int)a+b*d; Operators in C by Anupam Sharma
  • 38. Example #include <stdio.h> main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %fn", mean ); } Output is Value of mean : 3.400000 It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value. Operators in C by Anupam Sharma
  • 39. Operators in C by Anupam Sharma
  • 40. Rules for Implicit Type Casting The following rules have to be followed while converting the expression from one type to another to avoid the loss of information: • All integer types to be converted to float. • All float types to be converted to double. • All character types to be converted to integer. Operators in C by Anupam Sharma
  • 41. Thank you Operators in C by Anupam Sharma