SlideShare a Scribd company logo
1 of 17
UNIT-II
In programming input means reading data from the input devices
or from a file. Output means displaying results on the screen. C provides
number of input and output functions. These input and output functions
are predefined in their respective header files. Header file for standard
input and output functions is stdio.h. These are included into program
prefixed with #include statement. Input and Output functions are
classified into 2 categories. Decision making statements (or) Control
statements are used to check the condition, if the condition is true it will
executes a block of statements.
C Formatted IO Functions
Input data or output results are formatted as per requirement.
Formatted function improves the readability of the input and
output. Formatted functions can be used to read and write data
of all data type(char, int, float, double). Formatted input and
output functions require format specifiers(%c, %d, %f, %lf) to
identify the type of data.
scanf() Function
Scanf function reads all types of data value from input devices
(or) from a file. The address operator '&' is to indicate the
memory location of the variable. This memory location is used to
store the data which is read through keyboard.
scanf() Function Program
scanf.c
#include <stdio.h> //header file section
int main()
{
int a, b, c;
printf("Enter the value of a = ");
scanf("%d",&a) ; //read value a through keyboard
printf("nEnter the value of b = ");
scanf("%d",&b); //read value b through keyboard
c = a + b;
printf("na + b = %d ",c); //Print the sum of two value a and b
return 0;
}
 Enter the value of a = 6
 Enter the value of b = 4
 a + b = 10
Width specifier program
widthspecifier.c
#include <stdio.h> //header file section
int main()
{
printf("n%.2s","abcdefg ");
printf("n%.3s","abcdefg ");
printf("n%.4s","abcdefg ");
printf("n%.5s","abcdefg ");
printf("n%.6s","abcdefg ");
return 0;
}
 ab
 abc
 abcd
 abcde
 abcdef
Note:
Output itself shows the process done by %.2s, %.3s, etc..
scanf to Get Input
getinput.c
#include <stdio.h> //header file section
int main()
{
float a, b, c, d;
printf("Enter three float numbers:n ");
scanf("n %f %f %f ",&a,&b,&c);
d = (a + b + c)/3;
printf("nAverage of given number is %f ",d);
return 0;
}
 Enter three float numbers:
 4.5
 4.5
 5.0
 Average of given number is 4.666667
Note:
Here, three float variables a, b and c read through scanf() function. The
resultant average value is stored in a variable d.
C Unformatted Functions
Unformatted input and output functions are only work with
character data type. Unformatted input and output functions do
not require any format specifiers. Because they only work with
character data type.
Character IO Functions
getchar() Function
The getchar() function reads character type data form the input.
The getchar() function reads one character at a time till the user
presses the enter key.
getchar() C Program
getchar.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c;
printf("Enter a character : ");
c = getchar();
printf("nEntered character : %c ", c);
return 0;
}
 Enter a character : y
 Entered character : y
Note:
Here, getchar() reads the input from the user and display back to the
user.
getch() Function
The getch() function reads the alphanumeric character input
from the user. But, that the entered character will not be
displayed.
getch() C Program
getch.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
printf("nHello, press any alphanumeric character to exit ");
getch();
return 0;
}
 Hello, press any alphanumeric character to exit
Note:
The above program will run until you press one of many alphanumeric
characters. The key pressed by you will not be displayed.
getche() Function
getche() function reads the alphanumeric character from the
user input. Here, character you entered will be echoed to the
user until he/she presses any key.
getche() C Program
getche.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
printf("nHello, press any alphanumeric character or symbol to exit n ");
getche();
return 0;
}
 Hello, press any alphanumeric character or symbol to exit
 k
Note:
The above program will run until you press one of many alphanumeric
characters. The key pressed by you will be echoed.
putchar() Function
putchar() function prints only one character at a time.
putchar() C Program
putchar.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c = 'K';
putchar(c);
return 0;
}
 K
Note:
Here, variable c is assigned to a character 'K'. The variable c is displayed
by the putchar(). Use Single quotation mark ' ' for a character.
putch() Function
The putch() function prints any alphanumeric character.
putch() C Program
putch.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c;
printf("Press any key to continuen ");
c = getch();
printf("input : ");
putch(c);
return 0;
}
 Press any key to continue
 input : d
Note:
The getch() function will not echo a character. The putch() function
displays the input you pressed.
String IO Functions
gets() Function
The gets() function can read a full string even blank spaces
presents in a string. But, the scanf() function leave a string after
blank space space is detected. The gets() function is used to get
any string from the user.
gets() C Program
gets.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c[25];
printf("Enter a string : ");
gets(c);
printf("n%s is awesome ",c);
return 0;
}
 Enter a string : Randy Orton
 Randy Orton is awesome
Note:
The gets() function reads a string from through keyboard and stores it in
character array c[25]. The printf() function displays a string on the
console.
puts() Function
The puts() function prints the charater array or string on the
console. The puts() function is similar to printf() function, but we
cannot print other than characters using puts() function.
puts() C Program
puts.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
char c[25];
printf("Enter your Name : ");
gets(c);
puts(c);
return 0;
}
 Enter your Name: john
 john
clrscr() in C
clrscr() is an inbuilt library function which is used to clear the
previous output displayed in a screen. clrscr() is defined
in #include <conio.h> header file.
clrscr() Function Program
clrscr.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
printf("Before clrscr");
clrscr();
printf("clrscr() will clear the screen");
return 0;
}
 clrscr() will clear the screen
Note:
clrscr(); works in Turbo C/C++ compiler.
exit() in C
exit() is an inbuilt library function which is used to terimate the
program irrespective of the statements followed by it. exit() is
defined in #include <stdlib.h> header file.
exit() Function Program
exit.c
#include <stdio.h> //header file section
#include <stdlib.h>
int main()
{
printf("This statement is before exit(); function ");
exit(0);
printf("It will not display ");
return 0;
}
 This statement is before exit(); function
Note:
exit (some numeric value); will exit the program.
sleep() in C
sleep() is an inbuilt library function which is used to delay the
program's output. sleep() is defined in #include <unistd.h>
header file.
sleep() Function Program
sleep.c
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("Countdown... ");
printf("n 3");
sleep(1);
printf("n 2");
sleep(1);
printf("n 1");
sleep(1);
printf("n Celebration Time ");
return 0;
}
 Countdown...
 3
 2
 1
 Celebration Time
Note:
sleep (seconds); will delay the program's output with respect to seconds
which you mentioned.
C Control Statements
C Programs that we encountered up to now were executed in
the same order which they appeared in it. In practical
applications, there is numerous situations where we have to
neglect some parts of program codes. For this purpose, C
Programming uses the control statements to control the flow of a
program. If the condition is satisfied the code followed by that
condition will execute. If not simply that the code will be
neglected by the compiler.
Types of Control statements
 if
 if else
 if-else-if control statement
 switch() case control statement
C if statement
Why if Statement?
All programming languages enable you to make decisions. They
enable the program to follow a certain course of action
depending on whether a particular condition is met. This is
what gives programming language their intelligence.
C if Syntax And Definition
 C uses the keyword if to execute a set of statements when logical
condition is true.
 When the logical condition is false, the compiler simply skips the
statement within the block.
 The "if" statement is also known as one way decision statement.
Syntax if statement
Syntax
if(condition)
{
here goes statements;
.
.
.
.
here goes statements;
}
if Statement Uses
if statements are commonly used in following scenarios
 Is A bigger than B?
 Is X equal to Y?
 Is M not equal to N?
Realtime Time if Statement Uses
Arduino microcontroller make use C programming, where you
need to blink a warning light(red light) when certain condition
met. Example program is as below:
realtimeif
if(x = 123)
digitalWrite(LEDpin, high)
Note:
x is a variable which get its input from a sensor continuously.
if Statement Rules
 The expression (or) condition is always enclosed within pair of
parenthesis. e.g.) if ( a > b )
 The if statement should not be terminated with a semicolon. If it happens,
the block of statements will not execute even the condition is true.
 The statements following the if condition is normally enclosed between 2
braces (in curly braces).
if statement Flow Chart
Following flow chart will clearly explain how if statement works
if statement C Program
ifstatement.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
int age = 18;
if(age > 17){
printf("you are eligible for voting ");
}
printf("nThis is normal flow ");
return 0;
}
 you are eligible for voting
 This is normal flow
Note:
If the user input is greater than 17, then the condition will be true and the
statements under if condition gets executed. Otherwise, it executes next
to the if block.

More Related Content

What's hot

C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structuresindra Kishor
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C ProgrammingKamal Acharya
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
Input and output in c
Input and output in cInput and output in c
Input and output in cRachana Joshi
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
C Prog. - Decision & Loop Controls
C Prog. - Decision & Loop ControlsC Prog. - Decision & Loop Controls
C Prog. - Decision & Loop Controlsvinay arora
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c programNishmaNJ
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manualAnil Bishnoi
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2Rumman Ansari
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 

What's hot (20)

7 functions
7  functions7  functions
7 functions
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Input and output in c
Input and output in cInput and output in c
Input and output in c
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
C Prog. - Decision & Loop Controls
C Prog. - Decision & Loop ControlsC Prog. - Decision & Loop Controls
C Prog. - Decision & Loop Controls
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2C Programming Language Step by Step Part 2
C Programming Language Step by Step Part 2
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Deep C
Deep CDeep C
Deep C
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 

Similar to UNIT-II CP DOC.docx

Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C LanguageAdnan Khan
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
Basic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.comBasic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.comAkanchha Agrawal
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
C programming language for beginners
C programming language for beginners C programming language for beginners
C programming language for beginners ShreyaSingh291866
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.pptatulchaudhary821
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7alish sha
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSivakumar R D .
 

Similar to UNIT-II CP DOC.docx (20)

First c program
First c programFirst c program
First c program
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
Basic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.comBasic of c programming www.eakanchha.com
Basic of c programming www.eakanchha.com
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C programming language for beginners
C programming language for beginners C programming language for beginners
C programming language for beginners
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
Cinfo
CinfoCinfo
Cinfo
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 

More from JavvajiVenkat

unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxJavvajiVenkat
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
Nested Loops in C unit2.docx
Nested Loops in C unit2.docxNested Loops in C unit2.docx
Nested Loops in C unit2.docxJavvajiVenkat
 

More from JavvajiVenkat (6)

CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptxunit2 C-ProgrammingChapter 2 Control statements.pptx
unit2 C-ProgrammingChapter 2 Control statements.pptx
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Nested Loops in C unit2.docx
Nested Loops in C unit2.docxNested Loops in C unit2.docx
Nested Loops in C unit2.docx
 

Recently uploaded

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 

Recently uploaded (20)

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 

UNIT-II CP DOC.docx

  • 1. UNIT-II In programming input means reading data from the input devices or from a file. Output means displaying results on the screen. C provides number of input and output functions. These input and output functions are predefined in their respective header files. Header file for standard input and output functions is stdio.h. These are included into program prefixed with #include statement. Input and Output functions are classified into 2 categories. Decision making statements (or) Control statements are used to check the condition, if the condition is true it will executes a block of statements.
  • 2. C Formatted IO Functions Input data or output results are formatted as per requirement. Formatted function improves the readability of the input and output. Formatted functions can be used to read and write data of all data type(char, int, float, double). Formatted input and output functions require format specifiers(%c, %d, %f, %lf) to identify the type of data. scanf() Function Scanf function reads all types of data value from input devices (or) from a file. The address operator '&' is to indicate the memory location of the variable. This memory location is used to store the data which is read through keyboard. scanf() Function Program scanf.c
  • 3. #include <stdio.h> //header file section int main() { int a, b, c; printf("Enter the value of a = "); scanf("%d",&a) ; //read value a through keyboard printf("nEnter the value of b = "); scanf("%d",&b); //read value b through keyboard c = a + b; printf("na + b = %d ",c); //Print the sum of two value a and b return 0; }  Enter the value of a = 6  Enter the value of b = 4  a + b = 10 Width specifier program widthspecifier.c #include <stdio.h> //header file section int main() { printf("n%.2s","abcdefg "); printf("n%.3s","abcdefg "); printf("n%.4s","abcdefg "); printf("n%.5s","abcdefg "); printf("n%.6s","abcdefg "); return 0; }
  • 4.  ab  abc  abcd  abcde  abcdef Note: Output itself shows the process done by %.2s, %.3s, etc.. scanf to Get Input getinput.c #include <stdio.h> //header file section int main() { float a, b, c, d; printf("Enter three float numbers:n "); scanf("n %f %f %f ",&a,&b,&c); d = (a + b + c)/3; printf("nAverage of given number is %f ",d); return 0; }  Enter three float numbers:  4.5  4.5  5.0  Average of given number is 4.666667 Note: Here, three float variables a, b and c read through scanf() function. The resultant average value is stored in a variable d.
  • 5. C Unformatted Functions Unformatted input and output functions are only work with character data type. Unformatted input and output functions do not require any format specifiers. Because they only work with character data type. Character IO Functions getchar() Function The getchar() function reads character type data form the input. The getchar() function reads one character at a time till the user presses the enter key. getchar() C Program getchar.c #include <stdio.h> //header file section #include <conio.h> int main() { char c; printf("Enter a character : "); c = getchar(); printf("nEntered character : %c ", c); return 0; }  Enter a character : y  Entered character : y Note:
  • 6. Here, getchar() reads the input from the user and display back to the user. getch() Function The getch() function reads the alphanumeric character input from the user. But, that the entered character will not be displayed. getch() C Program getch.c #include <stdio.h> //header file section #include <conio.h> int main() { printf("nHello, press any alphanumeric character to exit "); getch(); return 0; }  Hello, press any alphanumeric character to exit Note: The above program will run until you press one of many alphanumeric characters. The key pressed by you will not be displayed. getche() Function getche() function reads the alphanumeric character from the user input. Here, character you entered will be echoed to the user until he/she presses any key. getche() C Program
  • 7. getche.c #include <stdio.h> //header file section #include <conio.h> int main() { printf("nHello, press any alphanumeric character or symbol to exit n "); getche(); return 0; }  Hello, press any alphanumeric character or symbol to exit  k Note: The above program will run until you press one of many alphanumeric characters. The key pressed by you will be echoed. putchar() Function putchar() function prints only one character at a time. putchar() C Program putchar.c #include <stdio.h> //header file section #include <conio.h> int main() { char c = 'K'; putchar(c); return 0;
  • 8. }  K Note: Here, variable c is assigned to a character 'K'. The variable c is displayed by the putchar(). Use Single quotation mark ' ' for a character. putch() Function The putch() function prints any alphanumeric character. putch() C Program putch.c #include <stdio.h> //header file section #include <conio.h> int main() { char c; printf("Press any key to continuen "); c = getch(); printf("input : "); putch(c); return 0; }  Press any key to continue  input : d Note: The getch() function will not echo a character. The putch() function displays the input you pressed.
  • 9. String IO Functions gets() Function The gets() function can read a full string even blank spaces presents in a string. But, the scanf() function leave a string after blank space space is detected. The gets() function is used to get any string from the user. gets() C Program gets.c #include <stdio.h> //header file section #include <conio.h> int main() { char c[25]; printf("Enter a string : "); gets(c); printf("n%s is awesome ",c); return 0; }  Enter a string : Randy Orton  Randy Orton is awesome Note: The gets() function reads a string from through keyboard and stores it in character array c[25]. The printf() function displays a string on the console. puts() Function
  • 10. The puts() function prints the charater array or string on the console. The puts() function is similar to printf() function, but we cannot print other than characters using puts() function. puts() C Program puts.c #include <stdio.h> //header file section #include <conio.h> int main() { char c[25]; printf("Enter your Name : "); gets(c); puts(c); return 0; }  Enter your Name: john  john clrscr() in C clrscr() is an inbuilt library function which is used to clear the previous output displayed in a screen. clrscr() is defined in #include <conio.h> header file. clrscr() Function Program clrscr.c #include <stdio.h> //header file section
  • 11. #include <conio.h> int main() { printf("Before clrscr"); clrscr(); printf("clrscr() will clear the screen"); return 0; }  clrscr() will clear the screen Note: clrscr(); works in Turbo C/C++ compiler. exit() in C exit() is an inbuilt library function which is used to terimate the program irrespective of the statements followed by it. exit() is defined in #include <stdlib.h> header file. exit() Function Program exit.c #include <stdio.h> //header file section #include <stdlib.h> int main() { printf("This statement is before exit(); function "); exit(0); printf("It will not display "); return 0; }
  • 12.  This statement is before exit(); function Note: exit (some numeric value); will exit the program. sleep() in C sleep() is an inbuilt library function which is used to delay the program's output. sleep() is defined in #include <unistd.h> header file. sleep() Function Program sleep.c #include <stdio.h> #include <unistd.h> int main() { printf("Countdown... "); printf("n 3"); sleep(1); printf("n 2"); sleep(1); printf("n 1"); sleep(1); printf("n Celebration Time "); return 0; }  Countdown...  3  2  1  Celebration Time
  • 13. Note: sleep (seconds); will delay the program's output with respect to seconds which you mentioned. C Control Statements C Programs that we encountered up to now were executed in the same order which they appeared in it. In practical applications, there is numerous situations where we have to neglect some parts of program codes. For this purpose, C Programming uses the control statements to control the flow of a program. If the condition is satisfied the code followed by that condition will execute. If not simply that the code will be neglected by the compiler. Types of Control statements  if  if else  if-else-if control statement
  • 14.  switch() case control statement C if statement Why if Statement? All programming languages enable you to make decisions. They enable the program to follow a certain course of action depending on whether a particular condition is met. This is what gives programming language their intelligence. C if Syntax And Definition  C uses the keyword if to execute a set of statements when logical condition is true.  When the logical condition is false, the compiler simply skips the statement within the block.  The "if" statement is also known as one way decision statement. Syntax if statement Syntax if(condition)
  • 15. { here goes statements; . . . . here goes statements; } if Statement Uses if statements are commonly used in following scenarios  Is A bigger than B?  Is X equal to Y?  Is M not equal to N? Realtime Time if Statement Uses Arduino microcontroller make use C programming, where you need to blink a warning light(red light) when certain condition met. Example program is as below: realtimeif if(x = 123)
  • 16. digitalWrite(LEDpin, high) Note: x is a variable which get its input from a sensor continuously. if Statement Rules  The expression (or) condition is always enclosed within pair of parenthesis. e.g.) if ( a > b )  The if statement should not be terminated with a semicolon. If it happens, the block of statements will not execute even the condition is true.  The statements following the if condition is normally enclosed between 2 braces (in curly braces). if statement Flow Chart Following flow chart will clearly explain how if statement works if statement C Program ifstatement.c #include <stdio.h> //header file section #include <conio.h> int main() {
  • 17. int age = 18; if(age > 17){ printf("you are eligible for voting "); } printf("nThis is normal flow "); return 0; }  you are eligible for voting  This is normal flow Note: If the user input is greater than 17, then the condition will be true and the statements under if condition gets executed. Otherwise, it executes next to the if block.