SlideShare ist ein Scribd-Unternehmen logo
1 von 8
MCA DEPARTMENT | Operators& Expressions
C
1
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.
 Operators, functions, constants and variables are
combined together 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.
Types ofC operators:
C language offers many types of operators.
They are,
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators
C operators:
S.no Types of Operators Description
1 Arithmeticoperators
These are used to perform
mathematical calculations like
addition, subtraction,
multiplication, division and
modulus
2 Assignmen_operators
These are used to assign
the valuesfor the variablesin C
programs.
3 Relational operators
These operators are used
to compare the value of two
variables.
4 Logical operators
These operators are used to
perform logical operations on
the given two variables.
5 Bit wise operators
These operators are used to
perform bit operations on
given two variables.
6
Conditional (ternary)
operators
Conditional operators return
one value if condition is true
and returns another value is
condition is false.
7
Increment/decrement
operators
These operators are used to
either increase or decrease the
value of the variable by one.
8 Special operators
&, *, sizeof( ) and ternary
operators.
1. AirthmeticOperators
operator Description Example
+ Adds two operands A + B will give its sum
-
Subtracts second operand
from the first
A - B will give the subtracted
value
* Multiply both operands
A * B will give the multiplied
value
/
Divide numerator by
denominator
B / A will give the quotient
%
Modulus Operator and
remainder of after an
integer division
B % A will give remainder
++
Increment operator,
increases integer value by
one
A++ will give incremented
value by 1
--
Decrement operator,
decreases integer value by
one
A-- will give decremented
by 1
Ex:1 // use of arithmeticoperators
#include<stdio.h>
void main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
OPERATORS AND EXPRESSIONS
MCA DEPARTMENT | Operators& Expressions
C
2
printf("Line 1- Value of c is%dn", c );
c = a - b;
printf("Line 2- Value of c is%dn", c );
c = a * b;
printf("Line 3- Value of c is%dn", c );
c = a / b;
printf("Line 4- Value of c is%dn", c );
c = a % b;
printf("Line 5- Value of c is%dn", c );
c = a++;
printf("Line 6- Value of c is%dn", c );
c = a--;
printf("Line 7- Value of c is%dn", c );
}
Output:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
Ex:2
/* Program to demonstrate the workingof arithmetic operators
in C. */
#include <stdio.h>
int main(){
int a=9,b=4,c;
c=a+b;
printf("a+b=%dn",c);
c=a-b;
printf("a-b=%dn",c);
c=a*b;
printf("a*b=%dn",c);
c=a/b;
printf("a/b=%dn",c);
c=a%b;
printf("Remainderwhena dividedbyb=%dn",c);
return 0;
}
Output:
a+b=13
a-b=5
a*b=36
a/b=2
Remainderwhena dividedby b=1
Ex:3
//Suppose a=5.0, b=2.0, c=5 and d=2
#include <stdio.h>
int main(){
float a=5.0, b=2.0;
c=a+b;
printf("a+b=%fn",c);
c=a-b;
printf("a-b=%fn",c);
c=a*b;
printf("a*b=%fn",c);
c=a/b;
printf("a/b=%fn",c);
return 0;
}
Output:
a+b=7.0
a-d=3.0
a/b=2.5
Note: % operator can onlybe used with integers.
2. AssignmentOperators
The most common assignmentoperator is =. This operator
assigns the value inright side to the leftside.For example:
var=5 //5 is assignedto var
a=c; //value of c is assignedto a
5=c; // Error! 5 is a constant.
Operator Example Same as
= a=b a=b
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b
MCA DEPARTMENT | Operators& Expressions
C
3
Ex: 4
//use of assignment operator
#include<stdio.h>
int main()
{
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %dn", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %dn", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %dn", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %dn", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %dn", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %dn", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %dn", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %dn", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %dn", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %dn", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %dn", c );
return 0;
}
Output:
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2
3. Relational Operator
 Relational operators checks relationshipbetweentwo
operands.
 If the relationis true,it returns value 1 and if the
relationis false,it returns value 0.
Operator Meaning of Operator Example
== Equal to 5==3 returnsfalse (0)
> Greater than 5>3 returns true (1)
< Less than 5<3 returns false (0)
!= Not equal to 5!=3 returnstrue(1)
>=
Greater than or equal
to
5>=3 returnstrue (1)
<= Less than or equal to 5<=3 return false (0)
Ex:5
// use of relational operator
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m== n)
{
printf(“mand n are equal”);
}
else
{
printf(“mand n are not equal”);
}
}
Output:
m and n are not equal
4. Logical Operators
Logical operators are usedto combine expressionscontaining
relationoperators. In C, there are 3 logical operators:
MCA DEPARTMENT | Operators& Expressions
C
4
Operator
Meaning of
Operator
Example
&& Logial AND
If c=5 and d=2 then,((c==5) && (d>5)) returns
false.
|| Logical OR
If c=5 and d=2 then, ((c==5) || (d>5)) returns
true.
! Logical NOT If c=5 then, !(c==5) returns false.
Ex:6
// use of relational operators
#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf(“&& Operator: Both conditionsare truen”);
}
if (o>p || p!=20)
{
printf(“||Operator : Only one conditionis truen”);
}
if (!(m>n&& m !=0))
{
printf(“!Operator : Both conditionsare truen”);
}
else
{
printf(“!Operator : Both conditionsare true. ” 
“But, status is invertedas falsen”);
}
}
Output:
&& Operator : Both conditionsare true
|| Operator : Onlyone conditionis true
! Operator : Both conditionsare true. But, status isinvertedas
false
5. Bit wise operators
These operators are used to perform bit operations. Decimal
values are converted into binary values which are the
sequence of bits and bit wise operators work on these bits.
Bit wise operators in C language are & (bitwise AND), |
(bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >>
(right shift).
Truth table for bit wise operation Bit wise operators
x y x|y x & y x ^ y Operator_symbol Operator_name
0 0 0 0 0 & Bitwise_AND
0 1 1 0 1 | Bitwise OR
1 0 1 0 1 ~ Bitwise_NOT
1 1 1 1 0 ^ XOR
<< Left Shift
>> Right Shift
 Consider x=40 and y=80. Binary form of these values are
given below.
x = 00101000
y= 01010000
 All bit wise operations for x and y are given below.
x&y = 00000000 (binary) = 0 (decimal)
x|y = 01111000 (binary) = 120 (decimal)
~x = 11111111111111111111111111
11111111111111111111111111111111010111
.. ..= -41 (decimal)
x^y = 01111000 (binary) = 120 (decimal)
x << 1 = 01010000 (binary) = 80 (decimal)
x >> 1 = 00010100 (binary) = 20 (decimal)
Note:
 Bit wise NOT : Value of 40 in binary is
00000000000000000000000000000000
00000000000000000010100000000000.So, all 0′s are
converted into 1′s in bit wise NOT operation.
 Bit wise left shift and right shift : In left shift operation “x <<
1 “, 1 means that the bits will be left shifted by one place. If
we use it as “x << 2 “, then, it means that the bits will be left
shifted by 2 places.
Example program for bit wise operators in C:
 In this example program, bit wise operations are performed
as shown above and output is displayed in decimal format.
MCA DEPARTMENT | Operators& Expressions
C
5
EX:7
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf(“AND_opr value = %dn”,AND_opr );
printf(“OR_opr value = %dn”,OR_opr );
printf(“NOT_opr value = %dn”,NOT_opr );
printf(“XOR_opr value = %dn”,XOR_opr );
printf(“left_shift value = %dn”, m << 1);
printf(“right_shift value = %dn”, m >> 1);
}
Output:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
6. Conditional or ternary operators:
 Conditional operators return one value if condition is true
and returns another value is condition is false.
 This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
.
 In above example, if A is greater than 100, 0 is returned else 1
is returned. This is equal to if else conditional statements.
Ex:8
// use of conditional operator
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf(“x value is %dn”, x);
printf(“y value is %d”, y);
}
Output:
x value is 1
y value is 2
6. Increment /Decrement operators
 Increment operators are used to increase the value of the
variable by one and decrement operators are used to
decrease the value of the variable by one in C programs.
 Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
…
 Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : - - i ; i- - ;
Ex:9
//Example for increment operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
Output:
1 2 3 4 5 6 7 8 9
Ex:10
//Example for decrement operators
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
Output:
MCA DEPARTMENT | Operators& Expressions
C
6
20 19 18 17 16 15 14 13 12 11
Differencebetweenpre/postincrement&decrement
operatorsinC:
S.no Operator type Operator Description
1 Pre increment
++i Value of i is incremented before
assigning it to variable i.
2 Post-increment
i++ Value of i is incremented after
assigning it to variable i.
3 Pre decrement
– –i Value of i is decremented before
assigning it to variable i.
4 Post_decrement
i– – Value of i is decremented after
assigning it to variable i.
Ex: 11
//pre – increment operators in C:
//Example for increment operators
#include <stdio.h>
int main()
{
int i=0;
while(++i < 5 )
{
printf("%d ",i);
}
return 0;
}
Output:
1 2 3 4
Ex:12 // post – increment operators in C:
#include <stdio.h>
int main()
{
int i=0;
while(i++ < 5 )
{
printf("%d ",i);
}
return 0;
}
Output:
1 2 3 4 5
Ex:13
//pre - decrement operators in C:
#include <stdio.h>
int main()
{
int i=10;
while(--i > 5 )
{
printf("%d ",i);
}
return 0;
}
Output:
9 8 7 6
Ex:15
// post - decrement operators in C:
#include <stdio.h>
int main()
{
int i=10;
while(i-- > 5 )
{
printf("%d ",i);
}
return 0;
}
Output:
9 8 7 6 5
Ex:16
// & and * operators in C:
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q’s value using ptr variable */
printf(“%d”, *ptr);
return 0;
}
Output:
MCA DEPARTMENT | Operators& Expressions
C
7
50
Ex:17
//program for sizeof() operator in C:
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf(“Storage size for int data type:%d n”,sizeof(a));
printf(“Storage size for char data type:%d n”,sizeof(b));
printf(“Storage size for float data type:%d n”,sizeof(c));
printf(“Storage size for double data type:%dn”,sizeof(d));
return 0;
}
Output:
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8
Operator Precedence& Associativity
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary
+ - ! ~ ++ - - (type) *
& sizeof
Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment
= += -= *= /= %= >>=
<<= &= ^= |=
Right to left
Comma , Left to right
EXPRESSIONS
Combinations of operands and operators with seperators are called as
expressions.
Ex:
int a, b, c:
c = a+b; //--- Expression statement
TypeConversions
 Converting from one data type to another data type is called
type conversion
 There are two kinds of type conversion
Implicit: Same data type conversion
 Smaller data type into bigger
(memory size)
Explicit: Different Data type
 Type Casting
(Float--- int (or) int ---- float)
Ex:18
// Implicit conversion
#include <stdio.h>
int main() {
int a = 2;
double b = 3.5;
Type Conversion
Implicit Explicit
MCA DEPARTMENT | Operators& Expressions
C
8
double c = a * b;
double d = a / b;
int e = a * b;
int f = a / b;
printf("a=%d, b=%.3f, c=%.3f, d=%.3f, e=%d, f=%dn",
a, b, c, d, e, f);
return 0;
}
Ex:19
// Explicit Conversion (or) Type Casting
#include <stdio.h>
#include <stdio.h>
int main() {
int a = 2;
int b = 3;
printf("a / b = %.3fn", a/b);
printf("a / b = %.3fn", (double) a/b);
return 0;
}

Weitere ähnliche Inhalte

Was ist angesagt?

Operators in c language
Operators in c languageOperators in c language
Operators in c language
Amit Singh
 
Operators and Expression
Operators and ExpressionOperators and Expression
Operators and Expression
shubham_jangid
 

Was ist angesagt? (19)

Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
Operators and Expression
Operators and ExpressionOperators and Expression
Operators and Expression
 
C operators
C operatorsC operators
C operators
 
Operator of C language
Operator of C languageOperator of C language
Operator of C language
 
Operators in C/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
Operators
OperatorsOperators
Operators
 
C operators
C operatorsC operators
C operators
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
 
2. operators in c
2. operators in c2. operators in c
2. operators in c
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATOR
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
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
 
C Operators
C OperatorsC Operators
C Operators
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 

Ähnlich wie Theory3

PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
Nithya K
 

Ähnlich wie Theory3 (20)

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
 
C++ chapter 2
C++ chapter 2C++ chapter 2
C++ chapter 2
 
Operators
OperatorsOperators
Operators
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operators
 
C operators
C operators C operators
C operators
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
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 by anupam
Operators in c by anupamOperators in c by anupam
Operators in c by anupam
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 

Mehr von Dr.M.Karthika parthasarathy

Mehr von Dr.M.Karthika parthasarathy (20)

IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
 
Linux Lab Manual.doc
Linux Lab Manual.docLinux Lab Manual.doc
Linux Lab Manual.doc
 
Unit 2 IoT.pdf
Unit 2 IoT.pdfUnit 2 IoT.pdf
Unit 2 IoT.pdf
 
Unit 3 IOT.docx
Unit 3 IOT.docxUnit 3 IOT.docx
Unit 3 IOT.docx
 
Unit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptxUnit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptx
 
Unit I What is Artificial Intelligence.docx
Unit I What is Artificial Intelligence.docxUnit I What is Artificial Intelligence.docx
Unit I What is Artificial Intelligence.docx
 
Introduction to IoT - Unit II.pptx
Introduction to IoT - Unit II.pptxIntroduction to IoT - Unit II.pptx
Introduction to IoT - Unit II.pptx
 
IoT Unit 2.pdf
IoT Unit 2.pdfIoT Unit 2.pdf
IoT Unit 2.pdf
 
Chapter 3 heuristic search techniques
Chapter 3 heuristic search techniquesChapter 3 heuristic search techniques
Chapter 3 heuristic search techniques
 
Ai mcq chapter 2
Ai mcq chapter 2Ai mcq chapter 2
Ai mcq chapter 2
 
Introduction to IoT unit II
Introduction to IoT  unit IIIntroduction to IoT  unit II
Introduction to IoT unit II
 
Introduction to IoT - Unit I
Introduction to IoT - Unit IIntroduction to IoT - Unit I
Introduction to IoT - Unit I
 
Internet of things Unit 1 one word
Internet of things Unit 1 one wordInternet of things Unit 1 one word
Internet of things Unit 1 one word
 
Unit 1 q&amp;a
Unit  1 q&amp;aUnit  1 q&amp;a
Unit 1 q&amp;a
 
Overview of Deadlock unit 3 part 1
Overview of Deadlock unit 3 part 1Overview of Deadlock unit 3 part 1
Overview of Deadlock unit 3 part 1
 
Examples in OS synchronization for UG
Examples in OS synchronization for UG Examples in OS synchronization for UG
Examples in OS synchronization for UG
 
Process Synchronization - Monitors
Process Synchronization - MonitorsProcess Synchronization - Monitors
Process Synchronization - Monitors
 
.net progrmming part4
.net progrmming part4.net progrmming part4
.net progrmming part4
 
.net progrmming part3
.net progrmming part3.net progrmming part3
.net progrmming part3
 
.net progrmming part1
.net progrmming part1.net progrmming part1
.net progrmming part1
 

Kürzlich hochgeladen

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Kürzlich hochgeladen (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 

Theory3

  • 1. MCA DEPARTMENT | Operators& Expressions C 1 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.  Operators, functions, constants and variables are combined together 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. Types ofC operators: C language offers many types of operators. They are, 1. Arithmetic operators 2. Assignment operators 3. Relational operators 4. Logical operators 5. Bit wise operators 6. Conditional operators (ternary operators) 7. Increment/decrement operators 8. Special operators C operators: S.no Types of Operators Description 1 Arithmeticoperators These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus 2 Assignmen_operators These are used to assign the valuesfor the variablesin C programs. 3 Relational operators These operators are used to compare the value of two variables. 4 Logical operators These operators are used to perform logical operations on the given two variables. 5 Bit wise operators These operators are used to perform bit operations on given two variables. 6 Conditional (ternary) operators Conditional operators return one value if condition is true and returns another value is condition is false. 7 Increment/decrement operators These operators are used to either increase or decrease the value of the variable by one. 8 Special operators &, *, sizeof( ) and ternary operators. 1. AirthmeticOperators operator Description Example + Adds two operands A + B will give its sum - Subtracts second operand from the first A - B will give the subtracted value * Multiply both operands A * B will give the multiplied value / Divide numerator by denominator B / A will give the quotient % Modulus Operator and remainder of after an integer division B % A will give remainder ++ Increment operator, increases integer value by one A++ will give incremented value by 1 -- Decrement operator, decreases integer value by one A-- will give decremented by 1 Ex:1 // use of arithmeticoperators #include<stdio.h> void main() { int a = 21; int b = 10; int c ; c = a + b; OPERATORS AND EXPRESSIONS
  • 2. MCA DEPARTMENT | Operators& Expressions C 2 printf("Line 1- Value of c is%dn", c ); c = a - b; printf("Line 2- Value of c is%dn", c ); c = a * b; printf("Line 3- Value of c is%dn", c ); c = a / b; printf("Line 4- Value of c is%dn", c ); c = a % b; printf("Line 5- Value of c is%dn", c ); c = a++; printf("Line 6- Value of c is%dn", c ); c = a--; printf("Line 7- Value of c is%dn", c ); } Output: Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22 Ex:2 /* Program to demonstrate the workingof arithmetic operators in C. */ #include <stdio.h> int main(){ int a=9,b=4,c; c=a+b; printf("a+b=%dn",c); c=a-b; printf("a-b=%dn",c); c=a*b; printf("a*b=%dn",c); c=a/b; printf("a/b=%dn",c); c=a%b; printf("Remainderwhena dividedbyb=%dn",c); return 0; } Output: a+b=13 a-b=5 a*b=36 a/b=2 Remainderwhena dividedby b=1 Ex:3 //Suppose a=5.0, b=2.0, c=5 and d=2 #include <stdio.h> int main(){ float a=5.0, b=2.0; c=a+b; printf("a+b=%fn",c); c=a-b; printf("a-b=%fn",c); c=a*b; printf("a*b=%fn",c); c=a/b; printf("a/b=%fn",c); return 0; } Output: a+b=7.0 a-d=3.0 a/b=2.5 Note: % operator can onlybe used with integers. 2. AssignmentOperators The most common assignmentoperator is =. This operator assigns the value inright side to the leftside.For example: var=5 //5 is assignedto var a=c; //value of c is assignedto a 5=c; // Error! 5 is a constant. Operator Example Same as = a=b a=b += a+=b a=a+b -= a-=b a=a-b *= a*=b a=a*b /= a/=b a=a/b %= a%=b a=a%b
  • 3. MCA DEPARTMENT | Operators& Expressions C 3 Ex: 4 //use of assignment operator #include<stdio.h> int main() { int a = 21; int c ; c = a; printf("Line 1 - = Operator Example, Value of c = %dn", c ); c += a; printf("Line 2 - += Operator Example, Value of c = %dn", c ); c -= a; printf("Line 3 - -= Operator Example, Value of c = %dn", c ); c *= a; printf("Line 4 - *= Operator Example, Value of c = %dn", c ); c /= a; printf("Line 5 - /= Operator Example, Value of c = %dn", c ); c = 200; c %= a; printf("Line 6 - %= Operator Example, Value of c = %dn", c ); c <<= 2; printf("Line 7 - <<= Operator Example, Value of c = %dn", c ); c >>= 2; printf("Line 8 - >>= Operator Example, Value of c = %dn", c ); c &= 2; printf("Line 9 - &= Operator Example, Value of c = %dn", c ); c ^= 2; printf("Line 10 - ^= Operator Example, Value of c = %dn", c ); c |= 2; printf("Line 11 - |= Operator Example, Value of c = %dn", c ); return 0; } Output: Line 1 - = Operator Example, Value of c = 21 Line 2 - += Operator Example, Value of c = 42 Line 3 - -= Operator Example, Value of c = 21 Line 4 - *= Operator Example, Value of c = 441 Line 5 - /= Operator Example, Value of c = 21 Line 6 - %= Operator Example, Value of c = 11 Line 7 - <<= Operator Example, Value of c = 44 Line 8 - >>= Operator Example, Value of c = 11 Line 9 - &= Operator Example, Value of c = 2 Line 10 - ^= Operator Example, Value of c = 0 Line 11 - |= Operator Example, Value of c = 2 3. Relational Operator  Relational operators checks relationshipbetweentwo operands.  If the relationis true,it returns value 1 and if the relationis false,it returns value 0. Operator Meaning of Operator Example == Equal to 5==3 returnsfalse (0) > Greater than 5>3 returns true (1) < Less than 5<3 returns false (0) != Not equal to 5!=3 returnstrue(1) >= Greater than or equal to 5>=3 returnstrue (1) <= Less than or equal to 5<=3 return false (0) Ex:5 // use of relational operator #include <stdio.h> int main() { int m=40,n=20; if (m== n) { printf(“mand n are equal”); } else { printf(“mand n are not equal”); } } Output: m and n are not equal 4. Logical Operators Logical operators are usedto combine expressionscontaining relationoperators. In C, there are 3 logical operators:
  • 4. MCA DEPARTMENT | Operators& Expressions C 4 Operator Meaning of Operator Example && Logial AND If c=5 and d=2 then,((c==5) && (d>5)) returns false. || Logical OR If c=5 and d=2 then, ((c==5) || (d>5)) returns true. ! Logical NOT If c=5 then, !(c==5) returns false. Ex:6 // use of relational operators #include <stdio.h> int main() { int m=40,n=20; int o=20,p=30; if (m>n && m !=0) { printf(“&& Operator: Both conditionsare truen”); } if (o>p || p!=20) { printf(“||Operator : Only one conditionis truen”); } if (!(m>n&& m !=0)) { printf(“!Operator : Both conditionsare truen”); } else { printf(“!Operator : Both conditionsare true. ” “But, status is invertedas falsen”); } } Output: && Operator : Both conditionsare true || Operator : Onlyone conditionis true ! Operator : Both conditionsare true. But, status isinvertedas false 5. Bit wise operators These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits. Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >> (right shift). Truth table for bit wise operation Bit wise operators x y x|y x & y x ^ y Operator_symbol Operator_name 0 0 0 0 0 & Bitwise_AND 0 1 1 0 1 | Bitwise OR 1 0 1 0 1 ~ Bitwise_NOT 1 1 1 1 0 ^ XOR << Left Shift >> Right Shift  Consider x=40 and y=80. Binary form of these values are given below. x = 00101000 y= 01010000  All bit wise operations for x and y are given below. x&y = 00000000 (binary) = 0 (decimal) x|y = 01111000 (binary) = 120 (decimal) ~x = 11111111111111111111111111 11111111111111111111111111111111010111 .. ..= -41 (decimal) x^y = 01111000 (binary) = 120 (decimal) x << 1 = 01010000 (binary) = 80 (decimal) x >> 1 = 00010100 (binary) = 20 (decimal) Note:  Bit wise NOT : Value of 40 in binary is 00000000000000000000000000000000 00000000000000000010100000000000.So, all 0′s are converted into 1′s in bit wise NOT operation.  Bit wise left shift and right shift : In left shift operation “x << 1 “, 1 means that the bits will be left shifted by one place. If we use it as “x << 2 “, then, it means that the bits will be left shifted by 2 places. Example program for bit wise operators in C:  In this example program, bit wise operations are performed as shown above and output is displayed in decimal format.
  • 5. MCA DEPARTMENT | Operators& Expressions C 5 EX:7 #include <stdio.h> int main() { int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ; AND_opr = (m&n); OR_opr = (m|n); NOT_opr = (~m); XOR_opr = (m^n); printf(“AND_opr value = %dn”,AND_opr ); printf(“OR_opr value = %dn”,OR_opr ); printf(“NOT_opr value = %dn”,NOT_opr ); printf(“XOR_opr value = %dn”,XOR_opr ); printf(“left_shift value = %dn”, m << 1); printf(“right_shift value = %dn”, m >> 1); } Output: AND_opr value = 0 OR_opr value = 120 NOT_opr value = -41 XOR_opr value = 120 left_shift value = 80 right_shift value = 20 6. Conditional or ternary operators:  Conditional operators return one value if condition is true and returns another value is condition is false.  This operator is also called as ternary operator. Syntax : (Condition? true_value: false_value); Example : (A > 100 ? 0 : 1); .  In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements. Ex:8 // use of conditional operator #include <stdio.h> int main() { int x=1, y ; y = ( x ==1 ? 2 : 0 ) ; printf(“x value is %dn”, x); printf(“y value is %d”, y); } Output: x value is 1 y value is 2 6. Increment /Decrement operators  Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.  Syntax: Increment operator: ++var_name; (or) var_name++; Decrement operator: – -var_name; (or) var_name – -; …  Example: Increment operator : ++ i ; i ++ ; Decrement operator : - - i ; i- - ; Ex:9 //Example for increment operators #include <stdio.h> int main() { int i=1; while(i<10) { printf("%d ",i); i++; } } Output: 1 2 3 4 5 6 7 8 9 Ex:10 //Example for decrement operators #include <stdio.h> int main() { int i=20; while(i>10) { printf("%d ",i); i--; } } Output:
  • 6. MCA DEPARTMENT | Operators& Expressions C 6 20 19 18 17 16 15 14 13 12 11 Differencebetweenpre/postincrement&decrement operatorsinC: S.no Operator type Operator Description 1 Pre increment ++i Value of i is incremented before assigning it to variable i. 2 Post-increment i++ Value of i is incremented after assigning it to variable i. 3 Pre decrement – –i Value of i is decremented before assigning it to variable i. 4 Post_decrement i– – Value of i is decremented after assigning it to variable i. Ex: 11 //pre – increment operators in C: //Example for increment operators #include <stdio.h> int main() { int i=0; while(++i < 5 ) { printf("%d ",i); } return 0; } Output: 1 2 3 4 Ex:12 // post – increment operators in C: #include <stdio.h> int main() { int i=0; while(i++ < 5 ) { printf("%d ",i); } return 0; } Output: 1 2 3 4 5 Ex:13 //pre - decrement operators in C: #include <stdio.h> int main() { int i=10; while(--i > 5 ) { printf("%d ",i); } return 0; } Output: 9 8 7 6 Ex:15 // post - decrement operators in C: #include <stdio.h> int main() { int i=10; while(i-- > 5 ) { printf("%d ",i); } return 0; } Output: 9 8 7 6 5 Ex:16 // & and * operators in C: #include <stdio.h> int main() { int *ptr, q; q = 50; /* address of q is assigned to ptr */ ptr = &q; /* display q’s value using ptr variable */ printf(“%d”, *ptr); return 0; } Output:
  • 7. MCA DEPARTMENT | Operators& Expressions C 7 50 Ex:17 //program for sizeof() operator in C: #include <stdio.h> #include <limits.h> int main() { int a; char b; float c; double d; printf(“Storage size for int data type:%d n”,sizeof(a)); printf(“Storage size for char data type:%d n”,sizeof(b)); printf(“Storage size for float data type:%d n”,sizeof(c)); printf(“Storage size for double data type:%dn”,sizeof(d)); return 0; } Output: Storage size for int data type:4 Storage size for char data type:1 Storage size for float data type:4 Storage size for double data type:8 Operator Precedence& Associativity Category Operator Associativity Postfix () [] -> . ++ - - Left to right Unary + - ! ~ ++ - - (type) * & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left Comma , Left to right EXPRESSIONS Combinations of operands and operators with seperators are called as expressions. Ex: int a, b, c: c = a+b; //--- Expression statement TypeConversions  Converting from one data type to another data type is called type conversion  There are two kinds of type conversion Implicit: Same data type conversion  Smaller data type into bigger (memory size) Explicit: Different Data type  Type Casting (Float--- int (or) int ---- float) Ex:18 // Implicit conversion #include <stdio.h> int main() { int a = 2; double b = 3.5; Type Conversion Implicit Explicit
  • 8. MCA DEPARTMENT | Operators& Expressions C 8 double c = a * b; double d = a / b; int e = a * b; int f = a / b; printf("a=%d, b=%.3f, c=%.3f, d=%.3f, e=%d, f=%dn", a, b, c, d, e, f); return 0; } Ex:19 // Explicit Conversion (or) Type Casting #include <stdio.h> #include <stdio.h> int main() { int a = 2; int b = 3; printf("a / b = %.3fn", a/b); printf("a / b = %.3fn", (double) a/b); return 0; }