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

PPTX
Expression and Operartor In C Programming
PPTX
Operator in c programming
DOCX
C – operators and expressions
PPT
Basic c operators
PPT
C Prog. - Operators and Expressions
PPTX
Operator of C language
PPT
Operators and Expressions in C++
PPT
C operator and expression
Expression and Operartor In C Programming
Operator in c programming
C – operators and expressions
Basic c operators
C Prog. - Operators and Expressions
Operator of C language
Operators and Expressions in C++
C operator and expression

What's hot (20)

PPT
2. operators in c
PPT
Operators in c language
PPT
Operator & Expression in c++
PPT
C operators
PPTX
PPT
Expressions in c++
PPTX
Basic c operators
PPTX
Operators and expressions in c language
PPT
Operators in C Programming
PPT
6 operators-in-c
DOCX
Programming Fundamentals lecture 7
PPT
Types of operators in C
PPTX
Operators and expressions in C++
PPTX
Operators in C & C++ Language
PPTX
C OPERATOR
PPT
CBSE Class XI :- Operators in C++
PPTX
Operators in C/C++
PPT
Operation and expression in c++
PPT
Operators in C++
2. operators in c
Operators in c language
Operator & Expression in c++
C operators
Expressions in c++
Basic c operators
Operators and expressions in c language
Operators in C Programming
6 operators-in-c
Programming Fundamentals lecture 7
Types of operators in C
Operators and expressions in C++
Operators in C & C++ Language
C OPERATOR
CBSE Class XI :- Operators in C++
Operators in C/C++
Operation and expression in c++
Operators in C++
Ad

Similar to Operators in c by anupam (20)

PDF
C Operators and Control Structures.pdf
PPTX
C Operators and Control Structures.pptx
PPTX
C operators
PDF
Types of Operators in C programming .pdf
PDF
Module2.1_Programming_Branching_and_looping.pdf
PDF
Unit ii chapter 1 operator and expressions in c
PPTX
PROGRAMMING IN C - Operators.pptx
PPTX
Operators and Expressions
PPTX
OPERATORS OF C++
PPTX
operators.pptx
PDF
Types of Operators in C
PPTX
3. C_OperatorsExpressions on c languyage.pptx
PDF
Coper in C
PPTX
C programming Tutorial Session 3
PPTX
C programming Tutorial Session 4
PPTX
operatorsincprogramming-190221094522.pptx
PPTX
C operators
PPTX
Precedence of operators IN C PROGRAMMING
PPTX
Basic operators and it's types in c languages
PPTX
Operators inc c language
C Operators and Control Structures.pdf
C Operators and Control Structures.pptx
C operators
Types of Operators in C programming .pdf
Module2.1_Programming_Branching_and_looping.pdf
Unit ii chapter 1 operator and expressions in c
PROGRAMMING IN C - Operators.pptx
Operators and Expressions
OPERATORS OF C++
operators.pptx
Types of Operators in C
3. C_OperatorsExpressions on c languyage.pptx
Coper in C
C programming Tutorial Session 3
C programming Tutorial Session 4
operatorsincprogramming-190221094522.pptx
C operators
Precedence of operators IN C PROGRAMMING
Basic operators and it's types in c languages
Operators inc c language
Ad

More from CGC Technical campus,Mohali (20)

PPTX
Gender Issues CS.pptx
PPTX
Intellectual Property Rights.pptx
PPTX
Cyber Safety ppt.pptx
PPTX
Python Modules .pptx
PPT
Dynamic allocation
PPT
Control statments in c
PPTX
PPT
Fundamentals of-computer
PPT
PPTX
Structure in C language
PPTX
Function in c program
PPTX
PPTX
Data processing and Report writing in Research(Section E)
PPTX
data analysis and report wring in research (Section d)
PPTX
Section C(Analytical and descriptive surveys... )
Gender Issues CS.pptx
Intellectual Property Rights.pptx
Cyber Safety ppt.pptx
Python Modules .pptx
Dynamic allocation
Control statments in c
Fundamentals of-computer
Structure in C language
Function in c program
Data processing and Report writing in Research(Section E)
data analysis and report wring in research (Section d)
Section C(Analytical and descriptive surveys... )

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Pre independence Education in Inndia.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Pharma ospi slides which help in ospi learning
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Complications of Minimal Access Surgery at WLH
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
102 student loan defaulters named and shamed – Is someone you know on the list?
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pre independence Education in Inndia.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPH.pptx obstetrics and gynecology in nursing
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
human mycosis Human fungal infections are called human mycosis..pptx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Microbial disease of the cardiovascular and lymphatic systems
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Pharma ospi slides which help in ospi learning
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
VCE English Exam - Section C Student Revision Booklet
Complications of Minimal Access Surgery at WLH
Supply Chain Operations Speaking Notes -ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

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