SlideShare ist ein Scribd-Unternehmen logo
1 von 20
1
Department of Computer Application
BCA Semester – 1
Group – 2
Roll No. – CACOM21007 – CACOM21012
2
C - PROGRAMMING
Content :-
1. Overview of C
2. Operators & Expressions
3
Overview Of
History of Language
C programming language was developed in 1972 at bell
laboratories of AT&T(American Telephone & Telegraph),
located in the U.S.A.
Dennis Ritchie is known as the founder of the C
language.
4
• ALGOL
1960
• BCPL
1967
• B
1970
• Traditional
1973
• C or C89
1989
• ISO C
1990
International Group
Martin Richard
Ken Thompson
Dennis Ritchie
ANSI Committee
ISO Committee
Year Developed by
Language
5
OPERATORS in C
Operators
( ++ , -- )
( + , - , * , / , % )
( < , <= , > , >= , == , != )
( && , || , ! )
( &, | , ^ , << , >> , ~ )
( = ,+= ,-= ,*= ,/= ,%= )
?:
Type
Unary
Operators
Arithmetic
Operators
Relational
Operators
Logical
Operators
Bitwise
Operators
Assignment
Operators
Ternary or Conditional
Operators
Binary Operators
Unary Operators
Ternary Operators
6
Arithmetic Operators
These operators are used to perform arithmetic / mathematical operations on operands.
Arithmetic operators are of two types:
Unary Operators
Operators that operate or work with a single operand are
unary operators. For example: Increment(++) and
Decrement(– –) Operators
int val = 5; ++val; // 6
int val1 = 5; --val1; // 4
Binary Operators
Operators that operate or work with two operands are binary operators. For
example: Addition(+), Subtraction(-), multiplication(*), Division(/) operators
int a = 7;
int b = 2;
printf(“%d”,a + b); // 9
printf(“%d”,a – b); // 5
Examples: (+, -, *, /, %,++,–).
7
8
// W orking of arit hmet ic operat ors
#include < st dio.h >
int main( )
{
int a = 9 ,b = 4 , c;
c = a+ b ;
printf ( "a+b = %d n",c) ;
c = a - b;
print f ( "a - b = %d n",c ) ;
c = a*b;
print f ( "a*b = %d n",c ) ;
c = a/b;
print f ( "a/b = %d n",c ) ;
c = a%b ;
print f ( "R emainder w hen a divided by b = %d n",c ) ;
return 0;
}
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
Output
Relational Operators
These are used for the comparison of the values of two
operands.
For example : checking if one operand is equal to the other operand or not, an operand is greater than
the other operand or not, etc. Some of the relational operators are (==, >= , <= , < , > )
int a = 3;
int b = 5;
a < b;
// operator to check if a is smaller than b
9
10
// Working of relational operators
#include < st dio.h >
int main ( )
{
int a = 5 , b = 5 , c = 1 0 ;
print f ( "%d = = %d is %d n", a, b, a = =
b) ;
print f ( "%d = = %d is %d n", a, c, a = =
c) ;
print f ( "%d > %d is %d n", a, b, a >
b) ;
print f ( "%d > %d is %d n", a, c, a > c) ;
print f ( "%d < %d is %d n", a, b, a <
b) ;
print f ( "%d < %d is %d n", a, c, a < c) ;
printf ( "%d ! = %d is %d n", a, b, a !=
b) ;
print f ( "%d ! = %d is %d n", a, c, a ! =
c) ;
print f ( "%d > = %d is %d n", a, b, a > =
b);
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
Output
Logical Operators
The result of the operation of a logical operator is a Boolean value
either true or false.
For example : the logical AND represented as ‘&&’ operator in C returns true when both the conditions
under consideration are satisfied. Otherwise, it returns false. Therefore, a && b returns true when both a
and b are true.
Logical operators are used to evaluate two or more conditions.
(4 != 5) && (4 < 5); // true
11
12
/ / W o r k i n g o f l o g i c a l o p e r a t o r s
# i n c l u d e < s t d i o . h >
i n t m a i n ( )
{
i n t a = 5 , b = 5 , c = 1 0 , r e s u l t ;
r e s u l t = ( a = = b ) & & ( c > b ) ;
p r i n t f ( " ( a = = b ) & & ( c > b ) i s % d  n " , r e s u l t ) ;
r e s u l t = ( a = = b ) & & ( c < b ) ;
p r i n t f ( " ( a = = b ) & & ( c < b ) i s % d  n " , r e s u l t ) ;
r e s u l t = ( a = = b ) | | ( c < b ) ;
p r i n t f ( " ( a = = b ) | | ( c < b ) i s % d  n " , r e s u l t ) ;
r e s u l t = ( a ! = b ) | | ( c < b ) ;
p r i n t f ( " ( a ! = b ) | | ( c < b ) i s % d  n " , r e s u l t ) ;
r e s u l t = ! ( a ! = b ) ;
p r i n t f ( " ! ( a ! = b ) i s % d  n " , r e s u l t ) ;
r e s u l t = ! ( a = = b ) ;
p r i n t f ( " ! ( a = = b ) i s % d  n " , r e s u l t ) ;
r e t u r n 0 ;
}
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Output
Bitwise Operators
The operators are first converted to bit-level and then the calculation is performed on the
operands.
For example: the bitwise AND represented as & operator in C takes two numbers as operands and
does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
The Bitwise operators are used to perform bit-level operations on the operands.
The mathematical operations such as addition, subtraction, multiplication, etc. can be performed at bit-level for faster
processing.
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
printf(“%d”,a ^ b); // 00001100 //Bitwise XOR
printf(“%d”,~a); // 11111010 //Bitwise Complement
13
14
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d",
a&b);
return 0;
}
Output = 8
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)
Bitwise AND
Bitwise OR
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d",
a|b);
return 0;
}
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12
and 25
00001100
| 00011001
________
00011101 = 29 (In
decimal)
Output = 29
Assignment Operators
Assignment operators are used to assigning value to a variable.
“=” : This operator is used to assign the value on the right to the variable on the left.
Different types of assignment operators are shown below:
“+=”:This operator first adds the current value of the variable on left to the value on the right
and then assigns the result to the variable on the left.
a = 10;
b = 20;
ch = 'y';
For example:
For example: (a += b) can be written as (a = a + b)
A.
B.
15
“-=”: This operator first subtracts the value on the right from the current value of the variable
on left and then assigns the result to the variable on the left.
“*=”: This operator first multiplies the current value of the variable on left to the value on the
right and then assigns the result to the variable on the left.
For example:
For example: (a *= b) can be written as (a = a * b)
C.
D.
(a -= b) can be written as (a = a - b)
E.
“/=”: This operator first divides the current value of the variable on left by the value on the right and
then assigns the result to the variable on the left.
For example: (a /= b) can be written as (a = a / b)
16
17
// W orking of assignment operat ors
#include < st dio.h >
int main ( )
{
int a = 5 , c;
c = a; // c is 5
print f ( "c = %d n", c) ;
c + = a; // c is 10
print f ( "c = %d n", c) ;
c - = a; // c is 5
print f ( "c = %d n", c) ;
c *= a; // c is 2 5
print f ( "c = %d n", c) ;
c /= a; // c is 5
printf ( "c = %d n", c);
c %= a; // c = 0
print f ( "c = %d n", c) ;
ret urn 0 ;
}
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
Output
Operators Precedence
18
At first, the expressions within parenthesis are evaluated. If no parenthesis is present, then the arithmetic
expression is evaluated from left to right.
There are two priority levels of operators in C.
High priority : * / %
Low priority : + -
19
#include <stdio.h>
int main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %dn", e
);
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %dn" ,
e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %dn",
e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %dn" , e
);
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Output
20
Presented By:
( 07 12)

Weitere ähnliche Inhalte

Ähnlich wie C - programming - Ankit Kumar Singh

C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionalish sha
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESLeahRachael
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzationKaushal Patel
 
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 structuresPradipta Mishra
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7REHAN IJAZ
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures Pradipta Mishra
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsAmrinder Sidhu
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 

Ähnlich wie C - programming - Ankit Kumar Singh (20)

C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
C Programming
C ProgrammingC Programming
C Programming
 
C operators
C operatorsC operators
C operators
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Operators
OperatorsOperators
Operators
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpression
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 
Basics of c
Basics of cBasics of c
Basics of c
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
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
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
Cpl
CplCpl
Cpl
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
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 and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operators in c by anupam
Operators in c by anupamOperators in c by anupam
Operators in c by anupam
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 

Kürzlich hochgeladen

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Kürzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

C - programming - Ankit Kumar Singh

  • 1. 1
  • 2. Department of Computer Application BCA Semester – 1 Group – 2 Roll No. – CACOM21007 – CACOM21012 2
  • 3. C - PROGRAMMING Content :- 1. Overview of C 2. Operators & Expressions 3
  • 4. Overview Of History of Language C programming language was developed in 1972 at bell laboratories of AT&T(American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie is known as the founder of the C language. 4
  • 5. • ALGOL 1960 • BCPL 1967 • B 1970 • Traditional 1973 • C or C89 1989 • ISO C 1990 International Group Martin Richard Ken Thompson Dennis Ritchie ANSI Committee ISO Committee Year Developed by Language 5
  • 6. OPERATORS in C Operators ( ++ , -- ) ( + , - , * , / , % ) ( < , <= , > , >= , == , != ) ( && , || , ! ) ( &, | , ^ , << , >> , ~ ) ( = ,+= ,-= ,*= ,/= ,%= ) ?: Type Unary Operators Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Ternary or Conditional Operators Binary Operators Unary Operators Ternary Operators 6
  • 7. Arithmetic Operators These operators are used to perform arithmetic / mathematical operations on operands. Arithmetic operators are of two types: Unary Operators Operators that operate or work with a single operand are unary operators. For example: Increment(++) and Decrement(– –) Operators int val = 5; ++val; // 6 int val1 = 5; --val1; // 4 Binary Operators Operators that operate or work with two operands are binary operators. For example: Addition(+), Subtraction(-), multiplication(*), Division(/) operators int a = 7; int b = 2; printf(“%d”,a + b); // 9 printf(“%d”,a – b); // 5 Examples: (+, -, *, /, %,++,–). 7
  • 8. 8 // W orking of arit hmet ic operat ors #include < st dio.h > int main( ) { int a = 9 ,b = 4 , c; c = a+ b ; printf ( "a+b = %d n",c) ; c = a - b; print f ( "a - b = %d n",c ) ; c = a*b; print f ( "a*b = %d n",c ) ; c = a/b; print f ( "a/b = %d n",c ) ; c = a%b ; print f ( "R emainder w hen a divided by b = %d n",c ) ; return 0; } a+b = 13 a-b = 5 a*b = 36 a/b = 2 Remainder when a divided by b=1 Output
  • 9. Relational Operators These are used for the comparison of the values of two operands. For example : checking if one operand is equal to the other operand or not, an operand is greater than the other operand or not, etc. Some of the relational operators are (==, >= , <= , < , > ) int a = 3; int b = 5; a < b; // operator to check if a is smaller than b 9
  • 10. 10 // Working of relational operators #include < st dio.h > int main ( ) { int a = 5 , b = 5 , c = 1 0 ; print f ( "%d = = %d is %d n", a, b, a = = b) ; print f ( "%d = = %d is %d n", a, c, a = = c) ; print f ( "%d > %d is %d n", a, b, a > b) ; print f ( "%d > %d is %d n", a, c, a > c) ; print f ( "%d < %d is %d n", a, b, a < b) ; print f ( "%d < %d is %d n", a, c, a < c) ; printf ( "%d ! = %d is %d n", a, b, a != b) ; print f ( "%d ! = %d is %d n", a, c, a ! = c) ; print f ( "%d > = %d is %d n", a, b, a > = b); 5 == 5 is 1 5 == 10 is 0 5 > 5 is 0 5 > 10 is 0 5 < 5 is 0 5 < 10 is 1 5 != 5 is 0 5 != 10 is 1 5 >= 5 is 1 5 >= 10 is 0 5 <= 5 is 1 5 <= 10 is 1 Output
  • 11. Logical Operators The result of the operation of a logical operator is a Boolean value either true or false. For example : the logical AND represented as ‘&&’ operator in C returns true when both the conditions under consideration are satisfied. Otherwise, it returns false. Therefore, a && b returns true when both a and b are true. Logical operators are used to evaluate two or more conditions. (4 != 5) && (4 < 5); // true 11
  • 12. 12 / / W o r k i n g o f l o g i c a l o p e r a t o r s # i n c l u d e < s t d i o . h > i n t m a i n ( ) { i n t a = 5 , b = 5 , c = 1 0 , r e s u l t ; r e s u l t = ( a = = b ) & & ( c > b ) ; p r i n t f ( " ( a = = b ) & & ( c > b ) i s % d n " , r e s u l t ) ; r e s u l t = ( a = = b ) & & ( c < b ) ; p r i n t f ( " ( a = = b ) & & ( c < b ) i s % d n " , r e s u l t ) ; r e s u l t = ( a = = b ) | | ( c < b ) ; p r i n t f ( " ( a = = b ) | | ( c < b ) i s % d n " , r e s u l t ) ; r e s u l t = ( a ! = b ) | | ( c < b ) ; p r i n t f ( " ( a ! = b ) | | ( c < b ) i s % d n " , r e s u l t ) ; r e s u l t = ! ( a ! = b ) ; p r i n t f ( " ! ( a ! = b ) i s % d n " , r e s u l t ) ; r e s u l t = ! ( a = = b ) ; p r i n t f ( " ! ( a = = b ) i s % d n " , r e s u l t ) ; r e t u r n 0 ; } (a == b) && (c > b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0 Output
  • 13. Bitwise Operators The operators are first converted to bit-level and then the calculation is performed on the operands. For example: the bitwise AND represented as & operator in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The Bitwise operators are used to perform bit-level operations on the operands. The mathematical operations such as addition, subtraction, multiplication, etc. can be performed at bit-level for faster processing. int a = 5, b = 9; // a = 5(00000101), b = 9(00001001) printf(“%d”,a ^ b); // 00001100 //Bitwise XOR printf(“%d”,~a); // 11111010 //Bitwise Complement 13
  • 14. 14 #include <stdio.h> int main() { int a = 12, b = 25; printf("Output = %d", a&b); return 0; } Output = 8 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bit Operation of 12 and 25 00001100 & 00011001 ________ 00001000 = 8 (In decimal) Bitwise AND Bitwise OR #include <stdio.h> int main() { int a = 12, b = 25; printf("Output = %d", a|b); return 0; } 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ________ 00011101 = 29 (In decimal) Output = 29
  • 15. Assignment Operators Assignment operators are used to assigning value to a variable. “=” : This operator is used to assign the value on the right to the variable on the left. Different types of assignment operators are shown below: “+=”:This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. a = 10; b = 20; ch = 'y'; For example: For example: (a += b) can be written as (a = a + b) A. B. 15
  • 16. “-=”: This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. “*=”: This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. For example: For example: (a *= b) can be written as (a = a * b) C. D. (a -= b) can be written as (a = a - b) E. “/=”: This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. For example: (a /= b) can be written as (a = a / b) 16
  • 17. 17 // W orking of assignment operat ors #include < st dio.h > int main ( ) { int a = 5 , c; c = a; // c is 5 print f ( "c = %d n", c) ; c + = a; // c is 10 print f ( "c = %d n", c) ; c - = a; // c is 5 print f ( "c = %d n", c) ; c *= a; // c is 2 5 print f ( "c = %d n", c) ; c /= a; // c is 5 printf ( "c = %d n", c); c %= a; // c = 0 print f ( "c = %d n", c) ; ret urn 0 ; } c = 5 c = 10 c = 5 c = 25 c = 5 c = 0 Output
  • 18. Operators Precedence 18 At first, the expressions within parenthesis are evaluated. If no parenthesis is present, then the arithmetic expression is evaluated from left to right. There are two priority levels of operators in C. High priority : * / % Low priority : + -
  • 19. 19 #include <stdio.h> int main() { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 printf("Value of (a + b) * c / d is : %dn", e ); e = ((a + b) * c) / d; // (30 * 15 ) / 5 printf("Value of ((a + b) * c) / d is : %dn" , e ); e = (a + b) * (c / d); // (30) * (15/5) printf("Value of (a + b) * (c / d) is : %dn", e ); e = a + (b * c) / d; // 20 + (150/5) printf("Value of a + (b * c) / d is : %dn" , e ); Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50 Output