SlideShare ist ein Scribd-Unternehmen logo
1 von 18
PROGRAMMING IN C –
Operators
Dr.K.Nithya
Assistant Professor,
Department of CSE,
SRMIST- Trichy
Operators in C
● Arithmetic operators
● Logical operators
● Relational operators
● Increment/ Decrement operators
● Assignment operator
● Bitwise operator
● Equality operator
● Other operators
Arithmetic operator
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c = a/b;
printf("a/b = %d n",c);
c = a%b;
printf("Remainder when a divided by b = %d n",c);
return 0;
}
Logical operator
An expression containing logical
operator returns either 0 or 1
depending upon whether expression
results true or false. Logical operators
are commonly used in decision
making in C programming.
&&
Logical AND. True only if all operands are true
If c = 5 and d = 2 then, expression ((c==5) &&
(d>5)) equals to 0.
||
Logical OR. True only if either one operand is true
If c = 5 and d = 2 then, expression ((c==5) ||
(d>5)) equals to 1.
!
Logical NOT. True only if the operand is 0
If c = 5 then, expression !(c==5) equals to 0.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d n", result);
result = !(a != b);
printf("!(a != b) is %d n", result);
result = !(a == b);
printf("!(a == b) is %d n", result);
return 0;
}
Relational operator
A relational operator checks the
relationship between two operands.
If the relation is true, it returns 1; if
the relation is false, it returns value
0.
Relational operators are used in
decision making and loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d n", a, b, a == b);
printf("%d == %d is %d n", a, c, a == c);
printf("%d > %d is %d n", a, b, a > b);
printf("%d > %d is %d n", a, c, a > c);
printf("%d < %d is %d n", a, b, a < b);
printf("%d < %d is %d n", a, c, a < c);
printf("%d != %d is %d n", a, b, a != b);
printf("%d != %d is %d n", a, c, a != c);
printf("%d >= %d is %d n", a, b, a >= b);
printf("%d >= %d is %d n", a, c, a >= c);
printf("%d <= %d is %d n", a, b, a <= b);
printf("%d <= %d is %d n", a, c, a <= c);
return 0;
}
Increment/Decrement operator
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
return 0;
}
increment ++ and decrement --
- to change the value of an operand
(constant or variable) by 1.
- Increment ++ increases the value by 1
decrement -- decreases the value by 1.
- These two operators are unary
operators (they only operate on a single
operand)
Assignment operator
#include <stdio.h>
void main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %dn", c);
c += a; // this means c=c+a;
printf("c = %dn", c);
c -= a; // c is 5
printf("c = %dn", c);
c *= a; // c is 25
printf("c = %dn", c);
c /= a; // c is 5
printf("c = %dn", c);
c %= a; // c = 0
printf("c = %dn", c);
}
Bitwise operator
- During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to
bit-level which makes processing faster and saves power.
- Bitwise operators are used in C programming to perform bit-level operations.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Ex: Bitwise AND (&)
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Ex: Bitwise Complement (~)
#include <stdio.h>
int main() {
printf(" %d n", ~35);
printf(" %dn", ~-12);
return 0;
}
Equality operator
- It’s a binary operator and works
on two operands, it returns 1 if
value of both operands are
equal else it returns 0.
Syntax:
Operand1 == Operand2
#include <stdio.h>
int main()
{
int a=10;
int b=10;
int result;
result = (a==b);
printf("result: %dn",result);
return 0;
}
Other operators
sizeof operator
● It is a compile-time unary operator which can be used to compute the size of its operand.
● The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
Comma Operator
● The comma operator (represented by the token) is a binary operator that evaluates its first operand and
discards the result, it then evaluates the second operand and returns this value (and type).
● The comma operator has the lowest precedence of any C operator.
● Comma acts as both operator and separator.
Conditional Operator
● The conditional operator is of the form expression1? expression2: expression3
● Here, Expression1 is the condition to be evaluated. If the condition(expression1) is True then we will execute and return the
result of expression2. otherwise if the condition(Expression1) is false, then we will execute and return the result of
expression3.
● We may replace the use of if..else statements with conditional operators.
dot (.) and arrow (->) Operators
● Member operators are used to reference individual members of classes, structures, and unions.
● The dot operator is applied to the actual object.
● The arrow operator is used with a pointer to an object.
Cast Operator
● Casting operators convert one data type to another. For example, int(2.2000) would return 2.
● A cast is a special operator that forces one data type to be converted into another.
● The most general cast supported by most of the C compilers is as follows − [ (type) expression ].
&,* Operator
● Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.
● Pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var.
Sizeof( ) operator
#include <stdio.h>
int main()
{
float i=78.0;
float j=6.78;
printf(" %d",sizeof(i+j));
return 0;
}
Comma operator
#include<stdio.h>
void main()
{
int p = 35;
int q = (p++, ++p);
printf("%d", q);
}
#include <stdio.h>
int main()
{
int a,b;
a = 10,20,30;
b = (10,20,30);
printf("%d, %dn",a,b);
return 0;
}
#include <stdio.h>
int main()
{
int a= 10,20,30;
int b;
b= (10,20,30);
printf("a= %d, b=
%dn",a,b);
return 0;
}
Conditional operator
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age);
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for
voting")); return 0;
}
#include <stdio.h>
int main()
{
int a=5, b;
b=((a==5)?(3):(2));
printf("The value of 'b'
variable is : %d",b);
return 0;
}
Cast operator
Syntax:
(type)value;
Example:
#include<stdio.h>
int main() {
float f= (float)9/4;
printf("f : %fn", f );
return 0;
}
Right Shift operator (>>)
#include<stdio.h>
int main()
{
int a = 5, b = 9;
printf("a>>1 = %dn", a>>2);
printf("b>>1 = %dn", b>>1);
return 0;
}
#include <stdio.h>
int main ()
{
unsigned int num = 0xff;
num = (num >> 2);
printf (" n After shifting the binary bits to the left side. ");
printf (" n The new value of the unsigned variable num = %d",
num);
return 0;
}

Weitere ähnliche Inhalte

Ähnlich wie PROGRAMMING IN C - Operators.pptx

Ähnlich wie PROGRAMMING IN C - Operators.pptx (20)

Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operators
 
Operators in c by anupam
Operators in c by anupamOperators in c by anupam
Operators in c by anupam
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
 
C Programming
C ProgrammingC Programming
C Programming
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdf
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
2. operator
2. operator2. operator
2. operator
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 

Kürzlich hochgeladen

MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
Krashi Coaching
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 

PROGRAMMING IN C - Operators.pptx

  • 1. PROGRAMMING IN C – Operators Dr.K.Nithya Assistant Professor, Department of CSE, SRMIST- Trichy
  • 2. Operators in C ● Arithmetic operators ● Logical operators ● Relational operators ● Increment/ Decrement operators ● Assignment operator ● Bitwise operator ● Equality operator ● Other operators
  • 3. Arithmetic operator #include <stdio.h> int main() { int a = 9,b = 4, c; c = a+b; printf("a+b = %d n",c); c = a-b; printf("a-b = %d n",c); c = a*b; printf("a*b = %d n",c); c = a/b; printf("a/b = %d n",c); c = a%b; printf("Remainder when a divided by b = %d n",c); return 0; }
  • 4. Logical operator An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. && Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0. || Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1. ! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0. #include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d n", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d n", result); result = !(a != b); printf("!(a != b) is %d n", result); result = !(a == b); printf("!(a == b) is %d n", result); return 0; }
  • 5. Relational operator A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision making and loops. Operator Meaning of Operator Example == Equal to 5 == 3 is evaluated to 0 > Greater than 5 > 3 is evaluated to 1 < Less than 5 < 3 is evaluated to 0 != Not equal to 5 != 3 is evaluated to 1 >= Greater than or equal to 5 >= 3 is evaluated to 1 <= Less than or equal to 5 <= 3 is evaluated to 0
  • 6. // Working of relational operators #include <stdio.h> int main() { int a = 5, b = 5, c = 10; printf("%d == %d is %d n", a, b, a == b); printf("%d == %d is %d n", a, c, a == c); printf("%d > %d is %d n", a, b, a > b); printf("%d > %d is %d n", a, c, a > c); printf("%d < %d is %d n", a, b, a < b); printf("%d < %d is %d n", a, c, a < c); printf("%d != %d is %d n", a, b, a != b); printf("%d != %d is %d n", a, c, a != c); printf("%d >= %d is %d n", a, b, a >= b); printf("%d >= %d is %d n", a, c, a >= c); printf("%d <= %d is %d n", a, b, a <= b); printf("%d <= %d is %d n", a, c, a <= c); return 0; }
  • 7. Increment/Decrement operator // Working of increment and decrement operators #include <stdio.h> int main() { int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d n", ++a); printf("--b = %d n", --b); printf("++c = %f n", ++c); printf("--d = %f n", --d); return 0; } increment ++ and decrement -- - to change the value of an operand (constant or variable) by 1. - Increment ++ increases the value by 1 decrement -- decreases the value by 1. - These two operators are unary operators (they only operate on a single operand)
  • 8. Assignment operator #include <stdio.h> void main() { int a = 5, c; c = a; // c is 5 printf("c = %dn", c); c += a; // this means c=c+a; printf("c = %dn", c); c -= a; // c is 5 printf("c = %dn", c); c *= a; // c is 25 printf("c = %dn", c); c /= a; // c is 5 printf("c = %dn", c); c %= a; // c = 0 printf("c = %dn", c); }
  • 9. Bitwise operator - During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power. - Bitwise operators are used in C programming to perform bit-level operations. Operators Meaning of operators & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left >> Shift right Ex: Bitwise AND (&) #include <stdio.h> int main() { int a = 12, b = 25; printf("Output = %d", a & b); return 0; } Ex: Bitwise Complement (~) #include <stdio.h> int main() { printf(" %d n", ~35); printf(" %dn", ~-12); return 0; }
  • 10. Equality operator - It’s a binary operator and works on two operands, it returns 1 if value of both operands are equal else it returns 0. Syntax: Operand1 == Operand2 #include <stdio.h> int main() { int a=10; int b=10; int result; result = (a==b); printf("result: %dn",result); return 0; }
  • 11. Other operators sizeof operator ● It is a compile-time unary operator which can be used to compute the size of its operand. ● The result of sizeof is of the unsigned integral type which is usually denoted by size_t. Comma Operator ● The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). ● The comma operator has the lowest precedence of any C operator. ● Comma acts as both operator and separator.
  • 12. Conditional Operator ● The conditional operator is of the form expression1? expression2: expression3 ● Here, Expression1 is the condition to be evaluated. If the condition(expression1) is True then we will execute and return the result of expression2. otherwise if the condition(Expression1) is false, then we will execute and return the result of expression3. ● We may replace the use of if..else statements with conditional operators. dot (.) and arrow (->) Operators ● Member operators are used to reference individual members of classes, structures, and unions. ● The dot operator is applied to the actual object. ● The arrow operator is used with a pointer to an object.
  • 13. Cast Operator ● Casting operators convert one data type to another. For example, int(2.2000) would return 2. ● A cast is a special operator that forces one data type to be converted into another. ● The most general cast supported by most of the C compilers is as follows − [ (type) expression ]. &,* Operator ● Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable. ● Pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var.
  • 14. Sizeof( ) operator #include <stdio.h> int main() { float i=78.0; float j=6.78; printf(" %d",sizeof(i+j)); return 0; }
  • 15. Comma operator #include<stdio.h> void main() { int p = 35; int q = (p++, ++p); printf("%d", q); } #include <stdio.h> int main() { int a,b; a = 10,20,30; b = (10,20,30); printf("%d, %dn",a,b); return 0; } #include <stdio.h> int main() { int a= 10,20,30; int b; b= (10,20,30); printf("a= %d, b= %dn",a,b); return 0; }
  • 16. Conditional operator #include <stdio.h> int main() { int age; // variable declaration printf("Enter your age"); scanf("%d",&age); (age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting")); return 0; } #include <stdio.h> int main() { int a=5, b; b=((a==5)?(3):(2)); printf("The value of 'b' variable is : %d",b); return 0; }
  • 17. Cast operator Syntax: (type)value; Example: #include<stdio.h> int main() { float f= (float)9/4; printf("f : %fn", f ); return 0; }
  • 18. Right Shift operator (>>) #include<stdio.h> int main() { int a = 5, b = 9; printf("a>>1 = %dn", a>>2); printf("b>>1 = %dn", b>>1); return 0; } #include <stdio.h> int main () { unsigned int num = 0xff; num = (num >> 2); printf (" n After shifting the binary bits to the left side. "); printf (" n The new value of the unsigned variable num = %d", num); return 0; }