SlideShare une entreprise Scribd logo
1  sur  37
Operators & Expressions
Presented by :
Er Jasleen Kaur
Assistant Professor
Applied Science(CSE)
Chandigarh University
Gharuan (Mohali).
Operators
• C supports rich set of operators.
• An operator is a symbol that tells the compiler to
perform certain mathematical or logical
manipulations.
• Operators are used in programs to manipulate data
and variables.
Types of Operators
.
Operators
TERNA
RY
BINARY
UNARY
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
Binary and Ternary Operators
• Binary operators?
• Ternary operators?
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
1. Arithmetic operator
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
10/19/2015
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
• 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
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’
10/19/2015
Relational And Logical Operators
True
!True i.e !1 =0
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
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
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;
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
10/19/2015
7. Bitwise operator
• C supports bitwise operators for manipulation of data at bit
level.
• Bitwise operators may not be applied to float or double.
• Bitwise operators are as follows:
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
~ One’s Complements
8. Special operator
• 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 -> ”
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 .
Precedence of operators
BODMAS RULE-
Brackets of Division Multiplication Addition Subtraction
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 r
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 : + -
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 to left.
• Hence the expression on the right is evaluated first and its value is
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 associate RIGHT
TO LEFT .
• All other remaining operators associate LEFT TO RIGHT
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
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
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
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.
10/19/2015
int + short int  int
• 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,
156 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
10/19/2015
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;
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.
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.
Creating your own header file
• This topic is not in your syllabus.
• Read only if interested.
• STEP 1: create new file and write following syntax in it.............
#ifndef<space>__NAME_H //NAME OF YOUR HEADER FILE
#define<space>__NAME_H
int factorial(int num) //define your function directly
{
int i,f=1;
for(i=n;i>=1;i--)
f*=i;
return(f);
}
/* if you want to print something in function you would require printf( ) so
you can include stdio.h in function definition also
#ifndef<space>__NAME_H //NAME OF YOUR HEADER FILE
#define<space>__NAME_H
#include<stdio.h>
........................................
.....................
*/
• SAVE THIS FILE AS NAME.H IN INCLUDE FOLDER OF TC //YOU
CAN USE ANY NAME //DEFINED ABOVE
• NOW CREATE NEW FILE SAY ANKIT.C /ANKIT.CPP
• TO USE HEADER FILE WRITE
#include<name.h> //your header file name
#include<stdio.h>
#include<conio.h>
void main( )
{ int c;
clrscr( );
c=factorial(5);
printf("factorial is %d",c); //change code accordingly in cpp
getch( );
}
Thank you
10/19/2015

Contenu connexe

Tendances

Operators in Python
Operators in PythonOperators in Python
Operators in PythonAnusuya123
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++ Bharat Kalia
 
Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILEDipta Saha
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressionsvishaljot_kaur
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++Jaspal Singh
 
Managing I/O operations In C- Language
Managing I/O operations In C- LanguageManaging I/O operations In C- Language
Managing I/O operations In C- LanguageRavindraSalunke3
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativityDr.Sandhiya Ravi
 

Tendances (20)

Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
Loops in c
Loops in cLoops in c
Loops in c
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
C tokens
C tokensC tokens
C tokens
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
 
Data types in C
Data types in CData types in C
Data types in C
 
Managing I/O operations In C- Language
Managing I/O operations In C- LanguageManaging I/O operations In C- Language
Managing I/O operations In C- Language
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 

En vedette

Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailgourav kottawar
 
Module 00 Bitwise Operators in C
Module 00 Bitwise Operators in CModule 00 Bitwise Operators in C
Module 00 Bitwise Operators in CTushar B Kute
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression Ashim Lamichhane
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++bajiajugal
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsTech
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanKavita Ganesan
 
Computer generation and language translator
Computer generation and language translatorComputer generation and language translator
Computer generation and language translatorShruti Pendharkar
 

En vedette (13)

Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
 
Module 00 Bitwise Operators in C
Module 00 Bitwise Operators in CModule 00 Bitwise Operators in C
Module 00 Bitwise Operators in C
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Java String
Java String Java String
Java String
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Computer generation and language translator
Computer generation and language translatorComputer generation and language translator
Computer generation and language translator
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Similaire à Operators & Expressions in C

Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cSowmya Jyothi
 
OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++ANANT VYAS
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.pptRithwikRanjan
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++Aahwini Esware gowda
 
data type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de ladata type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de laLEOFAKE
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unitmichaelaaron25322
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityAakash Singh
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001Ralph Weber
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...Fernando Loizides
 
Cprogrammingoperator
CprogrammingoperatorCprogrammingoperator
Cprogrammingoperatorteach4uin
 
L3 operators
L3 operatorsL3 operators
L3 operatorsteach4uin
 
L3 operators
L3 operatorsL3 operators
L3 operatorsteach4uin
 

Similaire à Operators & Expressions in C (20)

SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
 
c programming2.pptx
c programming2.pptxc programming2.pptx
c programming2.pptx
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
data type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de ladata type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de la
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...
 
Cprogrammingoperator
CprogrammingoperatorCprogrammingoperator
Cprogrammingoperator
 
operators in c++
operators in c++operators in c++
operators in c++
 
operators in c++
operators in c++operators in c++
operators in c++
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 

Plus de Jasleen Kaur (Chandigarh University)

Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE Jasleen Kaur (Chandigarh University)
 

Plus de Jasleen Kaur (Chandigarh University) (19)

Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
Static variables
Static variablesStatic variables
Static variables
 
Operating system notes pdf
Operating system notes pdfOperating system notes pdf
Operating system notes pdf
 
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Remote desktop connection
Remote desktop connectionRemote desktop connection
Remote desktop connection
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Calculating garbage value in case of overflow
Calculating garbage value in case of overflowCalculating garbage value in case of overflow
Calculating garbage value in case of overflow
 
Final jasleen ppt
Final jasleen pptFinal jasleen ppt
Final jasleen ppt
 
License Plate recognition
License Plate recognitionLicense Plate recognition
License Plate recognition
 
The solar system
The solar system The solar system
The solar system
 
The solar system
The solar systemThe solar system
The solar system
 
Afforestation environmental issue
Afforestation environmental issueAfforestation environmental issue
Afforestation environmental issue
 
Data aggregation in wireless sensor networks
Data aggregation in wireless sensor networksData aggregation in wireless sensor networks
Data aggregation in wireless sensor networks
 
Network security
Network securityNetwork security
Network security
 

Dernier

KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Dernier (20)

(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 

Operators & Expressions in C

  • 1. Operators & Expressions Presented by : Er Jasleen Kaur Assistant Professor Applied Science(CSE) Chandigarh University Gharuan (Mohali).
  • 2. Operators • C supports rich set of operators. • An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. • Operators are used in programs to manipulate data and variables.
  • 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
  • 5. Binary and Ternary Operators • Binary operators? • Ternary operators?
  • 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
  • 7. 1. Arithmetic operator + Addition - Subtraction * Multiplication / Division % Modulo division
  • 9. 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
  • 10. • 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
  • 11. 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’
  • 14. 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
  • 15. 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
  • 16. 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;
  • 17. 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
  • 19. 7. Bitwise operator • C supports bitwise operators for manipulation of data at bit level. • Bitwise operators may not be applied to float or double. • Bitwise operators are as follows: & bitwise AND | bitwise OR ^ bitwise exclusive OR << shift left >> shift right ~ One’s Complements
  • 20. 8. Special operator • 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 -> ”
  • 21. 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 .
  • 22. Precedence of operators BODMAS RULE- Brackets of Division Multiplication Addition Subtraction 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 r 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 : + -
  • 23. 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 to left. • Hence the expression on the right is evaluated first and its value is 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 associate RIGHT TO LEFT . • All other remaining operators associate LEFT TO RIGHT
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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. 10/19/2015 int + short int  int
  • 28. • 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, 156 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 10/19/2015
  • 29. 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;
  • 30. 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.
  • 31.
  • 32. 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.
  • 33. Creating your own header file • This topic is not in your syllabus. • Read only if interested.
  • 34. • STEP 1: create new file and write following syntax in it............. #ifndef<space>__NAME_H //NAME OF YOUR HEADER FILE #define<space>__NAME_H int factorial(int num) //define your function directly { int i,f=1; for(i=n;i>=1;i--) f*=i; return(f); } /* if you want to print something in function you would require printf( ) so you can include stdio.h in function definition also #ifndef<space>__NAME_H //NAME OF YOUR HEADER FILE #define<space>__NAME_H #include<stdio.h> ........................................ ..................... */
  • 35. • SAVE THIS FILE AS NAME.H IN INCLUDE FOLDER OF TC //YOU CAN USE ANY NAME //DEFINED ABOVE • NOW CREATE NEW FILE SAY ANKIT.C /ANKIT.CPP • TO USE HEADER FILE WRITE #include<name.h> //your header file name #include<stdio.h> #include<conio.h> void main( ) { int c; clrscr( ); c=factorial(5); printf("factorial is %d",c); //change code accordingly in cpp getch( ); }
  • 36.