SlideShare a Scribd company logo
1 of 31
Download to read offline
Input and Output Operations
Unit 3
Data Input and Output
• A program without any input or output has no
meaning.
• Input  process  Output
• Ex. marks sheet of students
• Reading the data from input devices and
displaying the result are the two main tasks of
any program.
Ashim Lamichhane 2
• Input/output functions are the links between
the user and the terminal.
• Input functions are used to read data from
keyboard are called standard input functions.
– scanf(), getchar(),getche(),getch() etc.
• Output functions are used to display the result
on the screen are called standard output
functions.
– printf(), putchar(), putch(), puts() etc.
Ashim Lamichhane 3
• In C, the standard library stdio.h provides
functions for input and output.
• The instruction #include<stdio.h> tells the
compiler to search for a file named stdio.h
and places its contents at this point in the
program.
• The contents of the header file become part
of the source code when it is compiled.
Ashim Lamichhane 4
• The input/output functions are classified into
two types –
1. Formatted functions
2. Unformatted functions
Ashim Lamichhane 5
Formatted Functions
• Formatted functions allow the input read input from
the keyboard or the output displayed on screen to be
formatted according to our requirements.
– Input function: scanf()
– Output function: printf()
Ashim Lamichhane 6
Formatted Input
• Ex: consider the following data:
– 50, 13.45, Ram
– Int, float, char variables
• This is possible using the scanf function.
• scanf stands for scan formatted.
• The built-in function scanf() can be used to enter
input data into the computer from a standard
input device.
Ashim Lamichhane 7
• The general form of scanf is,
scanf(“control string” , arg1, arg1,….. argn);
– Control string format in which data is to be
entered.
– arg1,arg2…  location where the data is stored.
 preceded by ampersand (&)
Ashim Lamichhane 8
• The control string consists of individual groups of
data formats, with one group for each input data
item.
• Each data format must begin with a percentage sign.
• General form of control string:
[whitespace character][ordinary character]%[field width] conversion character
– Whitespace characters [optional]
– Ordinary characters [Optional]
– Field width [Optional]
Ashim Lamichhane 9
Ashim Lamichhane 10
#include<stdio.h>
#include<conio.h>
void main() // Whitespace characters example
{
int n1;
char ch;
clrscr();
printf("Enter a number: ");
scanf(“ %d",&n1);
printf("Enter a character: ");
scanf(“ %c",&ch);
printf("nNumber: %d t Character: %c",n1,ch);
getch();
}
Ashim Lamichhane 11
#include<stdio.h>
#include<conio.h>
Void main(){ // Ordinary characters
Int day,year,month;
clrscr();
printf(“enter day month year in DD-MM-YYY format”);
scanf(“%d-%d-%d”,&day,&month,&year);
getch();
}
Ashim Lamichhane 12
#include<stdio.h>
#include<conio.h>
void main(){ //Field width example
int d;
printf(“Enter Max 5 numbers”);
scanf(“%5d”,&d);
printf(“Entered Numbers: %d”,d);
getch();
}
Ashim Lamichhane 13
#include<stdio.h>
#include<conio.h>
void main(){ //Input String
char string[10];
printf(“Enter Your Name”);
scanf(“%s”,string);
printf(“Your Name is %s”);
getch();
}
Ashim Lamichhane 14
#include<stdio.h>
#include<conio.h>
void main(){
char string[10];
printf("Enter Your Name:");
scanf("%s",string);
printf("My Name is %s",string);
getch();
}
Ashim Lamichhane 15
• some versions of scanf() support the following
conversion specifications for strings:-
%[character]
– only characters specified within the brackets are
allowed in the input string.
%[^character]
– the character specified after the caret are not allowed
Ashim Lamichhane 16
#include<stdio.h>
#include<conio.h>
void main(){
char string[10];
printf("Enter Your Name in uppercase:");
scanf("%[A-Z]",string);
printf("Your Name is %s",string);
getch();
}
Ashim Lamichhane 17
#include<stdio.h>
#include<conio.h>
void main(){
char string[10];
printf("Enter Your Name:");
scanf("%[^n]",string);
printf("My Name is %s",string);
getch();
}
%[^n] tells the compiler to read a string until a
newline character is entered
Ashim Lamichhane 18
Reading Mixed Data Types
• printf(“enter an integer, floating number,
gender and name:”);
scanf(“%d %f %c%s”,&i,&n1,&gender,&name);
• scanf() can contain mixed mode data.
• care should be taken to ensure that the input
data items match the control specification.
Ashim Lamichhane 19
Formatted Output
• refers to the output of data that has been arranged in a
particular format.
• printf() is a built in function which is used to output data from
the computer onto a standard device i.e. screen
• General form:
printf(“control string”,arg1,arg2,.....,arg n)
• The control string consists of four types of items -
– characters that will be printed on the screen as they appear.
– format specifications that define the output format for display of each item
– escape sequence characters such as n, t etc.
– any combination of characters, format specifications and escape sequences.
Ashim Lamichhane 20
• The control string has the form:
%[flag] [field width][.precision] conversion character
• Flags [optional]
– “ – “ indicates data item to be left-justified
– “+” indicates a positive or negative sign to precede
– “0” indicates leading 0’s to appear instead of leading
blanks
• Field width[optional]
– Same as before
• Precision [optional]
– The operation of precision field depends on the type of
conversion. It must start with a period (.).
Ashim Lamichhane 21
Unformatted Functions
• Doesn’t allow user to read or display data in
desired format.
• These library functions basically deals with a
single character or a string of characters.
• The functions getchar(), putchar(),
gets(),puts(),getch(),getche(),putch() are
considered as unformatted functions.
Ashim Lamichhane 22
• getchar()
– Reads a character from a standard input device.
– It takes the form:
Character_variable= getchar();
– Character_variable is a valid C char type variable.
– When this statement is encountered, the
computer waits until a key is pressed and then
assigns this character to character_variable.
Ashim Lamichhane 23
• putchar()
– Displays a character to the standard output
device.
– Its form:
putchar(character_variable);
– Where character_variable is a char type variable
containing a character
Ashim Lamichhane 24
Void main(){
char gender;
clrscr();
printf(“Enter gender M or F”: );
gender=getchar();
printf(“Your Gender is: “);
putchar(gender);
getch();
}
Ashim Lamichhane 25
• getch() and getche()
– Reads single character the instant it is typed
without waiting for the enter key to be hit.
– getch() doesn’t print the character entered
– getche() displays the character when entered.
– General form
• character_variable=getch();
• character_variable=getche();
– In both functions, the character typed is assigned
to the char type variable character_variable.
Ashim Lamichhane 26
• putch()
– The function putch() prints a character onto the
screen
– General form
• putch(character_variable);
– Character variable is a char type.
NOTE: These three functions are defined under the
standard library functions conio.h
Ashim Lamichhane 27
void main(){
char ch1, ch2;
clrscr();
printf("Enter 1st character: ");
ch1=getch();
printf("n Enter 2nd character");
ch2=getche();
printf("n first character: ");
putch(ch1);
printf("nSecond character: ");
putch(ch2);
getch();
}
Ashim Lamichhane 28
• gets()
– Used to read string of text, containing
whitespaces, until a new line character is
encountered.
– General form
gets(string_variable);
• puts()
– Used to display the string onto the terminal
– General form:
puts(string_variable);
Ashim Lamichhane 29
void main()
{
char name[20];
clrscr();
printf("Enter your name:");
gets(name);
printf("Your Name is: ");
puts(name);
getch();
}
Ashim Lamichhane 30
END
Ashim Lamichhane 31

More Related Content

What's hot

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programmingManoj Tyagi
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 

What's hot (20)

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
C tokens
C tokensC tokens
C tokens
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
File in c
File in cFile in c
File in c
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 

Viewers also liked

Viewers also liked (14)

File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
File handling in c
File handling in c File handling in c
File handling in c
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Unit 11. Graphics
Unit 11. GraphicsUnit 11. Graphics
Unit 11. Graphics
 
File handling in C
File handling in CFile handling in C
File handling in C
 

Similar to Unit 3. Input and Output

SPL 3 | Introduction to C programming
SPL 3 | Introduction to C programmingSPL 3 | Introduction to C programming
SPL 3 | Introduction to C programmingMohammad Imam Hossain
 
Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio Bhavik Vashi
 
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
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in cniyamathShariff
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).pptadvRajatSharma
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C LanguageAdnan Khan
 
2.1 input and output in c
2.1 input and output in c2.1 input and output in c
2.1 input and output in cJawad Khan
 
C format string vulnerability
C format string vulnerabilityC format string vulnerability
C format string vulnerabilitysluge
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in cyazad dumasia
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingMd. Ashikur Rahman
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 

Similar to Unit 3. Input and Output (20)

Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
SPL 3 | Introduction to C programming
SPL 3 | Introduction to C programmingSPL 3 | Introduction to C programming
SPL 3 | Introduction to C programming
 
Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
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
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
2.1 input and output in c
2.1 input and output in c2.1 input and output in c
2.1 input and output in c
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
C format string vulnerability
C format string vulnerabilityC format string vulnerability
C format string vulnerability
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 

More from Ashim Lamichhane

More from Ashim Lamichhane (10)

Searching
SearchingSearching
Searching
 
Sorting
SortingSorting
Sorting
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Linked List
Linked ListLinked List
Linked List
 
Queues
QueuesQueues
Queues
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 

Recently uploaded

Auchitya Theory by Kshemendra Indian Poetics
Auchitya Theory by Kshemendra Indian PoeticsAuchitya Theory by Kshemendra Indian Poetics
Auchitya Theory by Kshemendra Indian PoeticsDhatriParmar
 
3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptx3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptxmary850239
 
25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...
25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...
25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...Nguyen Thanh Tu Collection
 
Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024bsellato
 
3.14.24 The Selma March and the Voting Rights Act.pptx
3.14.24 The Selma March and the Voting Rights Act.pptx3.14.24 The Selma March and the Voting Rights Act.pptx
3.14.24 The Selma March and the Voting Rights Act.pptxmary850239
 
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxMetabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxDr. Santhosh Kumar. N
 
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...AKSHAYMAGAR17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...Nguyen Thanh Tu Collection
 
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...gdgsurrey
 
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in PharmacyASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in PharmacySumit Tiwari
 
The OERs: Transforming Education for Sustainable Future by Dr. Sarita Anand
The OERs: Transforming Education for Sustainable Future by Dr. Sarita AnandThe OERs: Transforming Education for Sustainable Future by Dr. Sarita Anand
The OERs: Transforming Education for Sustainable Future by Dr. Sarita AnandDr. Sarita Anand
 
Alamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptxAlamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptxDhatriParmar
 
The First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdfThe First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdfdogden2
 
ICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfVanessa Camilleri
 
Pharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdfPharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdfSumit Tiwari
 
Dhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian PoeticsDhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian PoeticsDhatriParmar
 
2024.03.16 How to write better quality materials for your learners ELTABB San...
2024.03.16 How to write better quality materials for your learners ELTABB San...2024.03.16 How to write better quality materials for your learners ELTABB San...
2024.03.16 How to write better quality materials for your learners ELTABB San...Sandy Millin
 
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdfPHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdfSumit Tiwari
 
Metabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptxMetabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptxDr. Santhosh Kumar. N
 

Recently uploaded (20)

Auchitya Theory by Kshemendra Indian Poetics
Auchitya Theory by Kshemendra Indian PoeticsAuchitya Theory by Kshemendra Indian Poetics
Auchitya Theory by Kshemendra Indian Poetics
 
3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptx3.12.24 Freedom Summer in Mississippi.pptx
3.12.24 Freedom Summer in Mississippi.pptx
 
25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...
25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...
25 CHUYÊN ĐỀ ÔN THI TỐT NGHIỆP THPT 2023 – BÀI TẬP PHÁT TRIỂN TỪ ĐỀ MINH HỌA...
 
Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024Awards Presentation 2024 - March 12 2024
Awards Presentation 2024 - March 12 2024
 
3.14.24 The Selma March and the Voting Rights Act.pptx
3.14.24 The Selma March and the Voting Rights Act.pptx3.14.24 The Selma March and the Voting Rights Act.pptx
3.14.24 The Selma March and the Voting Rights Act.pptx
 
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptxMetabolism of  lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
Metabolism of lipoproteins & its disorders(Chylomicron & VLDL & LDL).pptx
 
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
DNA and RNA , Structure, Functions, Types, difference, Similarities, Protein ...
 
ANOVA Parametric test: Biostatics and Research Methodology
ANOVA Parametric test: Biostatics and Research MethodologyANOVA Parametric test: Biostatics and Research Methodology
ANOVA Parametric test: Biostatics and Research Methodology
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - HK2 (...
 
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
Certification Study Group - Professional ML Engineer Session 3 (Machine Learn...
 
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in PharmacyASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
ASTRINGENTS.pdf Pharmacognosy chapter 5 diploma in Pharmacy
 
The OERs: Transforming Education for Sustainable Future by Dr. Sarita Anand
The OERs: Transforming Education for Sustainable Future by Dr. Sarita AnandThe OERs: Transforming Education for Sustainable Future by Dr. Sarita Anand
The OERs: Transforming Education for Sustainable Future by Dr. Sarita Anand
 
Alamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptxAlamkara theory by Bhamaha Indian Poetics (1).pptx
Alamkara theory by Bhamaha Indian Poetics (1).pptx
 
The First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdfThe First National K12 TUG March 6 2024.pdf
The First National K12 TUG March 6 2024.pdf
 
ICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdfICS2208 Lecture4 Intelligent Interface Agents.pdf
ICS2208 Lecture4 Intelligent Interface Agents.pdf
 
Pharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdfPharmacology chapter No 7 full notes.pdf
Pharmacology chapter No 7 full notes.pdf
 
Dhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian PoeticsDhavni Theory by Anandvardhana Indian Poetics
Dhavni Theory by Anandvardhana Indian Poetics
 
2024.03.16 How to write better quality materials for your learners ELTABB San...
2024.03.16 How to write better quality materials for your learners ELTABB San...2024.03.16 How to write better quality materials for your learners ELTABB San...
2024.03.16 How to write better quality materials for your learners ELTABB San...
 
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdfPHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
PHARMACOGNOSY CHAPTER NO 5 CARMINATIVES AND G.pdf
 
Metabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptxMetabolism , Metabolic Fate& disorders of cholesterol.pptx
Metabolism , Metabolic Fate& disorders of cholesterol.pptx
 

Unit 3. Input and Output

  • 1. Input and Output Operations Unit 3
  • 2. Data Input and Output • A program without any input or output has no meaning. • Input  process  Output • Ex. marks sheet of students • Reading the data from input devices and displaying the result are the two main tasks of any program. Ashim Lamichhane 2
  • 3. • Input/output functions are the links between the user and the terminal. • Input functions are used to read data from keyboard are called standard input functions. – scanf(), getchar(),getche(),getch() etc. • Output functions are used to display the result on the screen are called standard output functions. – printf(), putchar(), putch(), puts() etc. Ashim Lamichhane 3
  • 4. • In C, the standard library stdio.h provides functions for input and output. • The instruction #include<stdio.h> tells the compiler to search for a file named stdio.h and places its contents at this point in the program. • The contents of the header file become part of the source code when it is compiled. Ashim Lamichhane 4
  • 5. • The input/output functions are classified into two types – 1. Formatted functions 2. Unformatted functions Ashim Lamichhane 5
  • 6. Formatted Functions • Formatted functions allow the input read input from the keyboard or the output displayed on screen to be formatted according to our requirements. – Input function: scanf() – Output function: printf() Ashim Lamichhane 6
  • 7. Formatted Input • Ex: consider the following data: – 50, 13.45, Ram – Int, float, char variables • This is possible using the scanf function. • scanf stands for scan formatted. • The built-in function scanf() can be used to enter input data into the computer from a standard input device. Ashim Lamichhane 7
  • 8. • The general form of scanf is, scanf(“control string” , arg1, arg1,….. argn); – Control string format in which data is to be entered. – arg1,arg2…  location where the data is stored.  preceded by ampersand (&) Ashim Lamichhane 8
  • 9. • The control string consists of individual groups of data formats, with one group for each input data item. • Each data format must begin with a percentage sign. • General form of control string: [whitespace character][ordinary character]%[field width] conversion character – Whitespace characters [optional] – Ordinary characters [Optional] – Field width [Optional] Ashim Lamichhane 9
  • 11. #include<stdio.h> #include<conio.h> void main() // Whitespace characters example { int n1; char ch; clrscr(); printf("Enter a number: "); scanf(“ %d",&n1); printf("Enter a character: "); scanf(“ %c",&ch); printf("nNumber: %d t Character: %c",n1,ch); getch(); } Ashim Lamichhane 11
  • 12. #include<stdio.h> #include<conio.h> Void main(){ // Ordinary characters Int day,year,month; clrscr(); printf(“enter day month year in DD-MM-YYY format”); scanf(“%d-%d-%d”,&day,&month,&year); getch(); } Ashim Lamichhane 12
  • 13. #include<stdio.h> #include<conio.h> void main(){ //Field width example int d; printf(“Enter Max 5 numbers”); scanf(“%5d”,&d); printf(“Entered Numbers: %d”,d); getch(); } Ashim Lamichhane 13
  • 14. #include<stdio.h> #include<conio.h> void main(){ //Input String char string[10]; printf(“Enter Your Name”); scanf(“%s”,string); printf(“Your Name is %s”); getch(); } Ashim Lamichhane 14
  • 15. #include<stdio.h> #include<conio.h> void main(){ char string[10]; printf("Enter Your Name:"); scanf("%s",string); printf("My Name is %s",string); getch(); } Ashim Lamichhane 15
  • 16. • some versions of scanf() support the following conversion specifications for strings:- %[character] – only characters specified within the brackets are allowed in the input string. %[^character] – the character specified after the caret are not allowed Ashim Lamichhane 16
  • 17. #include<stdio.h> #include<conio.h> void main(){ char string[10]; printf("Enter Your Name in uppercase:"); scanf("%[A-Z]",string); printf("Your Name is %s",string); getch(); } Ashim Lamichhane 17
  • 18. #include<stdio.h> #include<conio.h> void main(){ char string[10]; printf("Enter Your Name:"); scanf("%[^n]",string); printf("My Name is %s",string); getch(); } %[^n] tells the compiler to read a string until a newline character is entered Ashim Lamichhane 18
  • 19. Reading Mixed Data Types • printf(“enter an integer, floating number, gender and name:”); scanf(“%d %f %c%s”,&i,&n1,&gender,&name); • scanf() can contain mixed mode data. • care should be taken to ensure that the input data items match the control specification. Ashim Lamichhane 19
  • 20. Formatted Output • refers to the output of data that has been arranged in a particular format. • printf() is a built in function which is used to output data from the computer onto a standard device i.e. screen • General form: printf(“control string”,arg1,arg2,.....,arg n) • The control string consists of four types of items - – characters that will be printed on the screen as they appear. – format specifications that define the output format for display of each item – escape sequence characters such as n, t etc. – any combination of characters, format specifications and escape sequences. Ashim Lamichhane 20
  • 21. • The control string has the form: %[flag] [field width][.precision] conversion character • Flags [optional] – “ – “ indicates data item to be left-justified – “+” indicates a positive or negative sign to precede – “0” indicates leading 0’s to appear instead of leading blanks • Field width[optional] – Same as before • Precision [optional] – The operation of precision field depends on the type of conversion. It must start with a period (.). Ashim Lamichhane 21
  • 22. Unformatted Functions • Doesn’t allow user to read or display data in desired format. • These library functions basically deals with a single character or a string of characters. • The functions getchar(), putchar(), gets(),puts(),getch(),getche(),putch() are considered as unformatted functions. Ashim Lamichhane 22
  • 23. • getchar() – Reads a character from a standard input device. – It takes the form: Character_variable= getchar(); – Character_variable is a valid C char type variable. – When this statement is encountered, the computer waits until a key is pressed and then assigns this character to character_variable. Ashim Lamichhane 23
  • 24. • putchar() – Displays a character to the standard output device. – Its form: putchar(character_variable); – Where character_variable is a char type variable containing a character Ashim Lamichhane 24
  • 25. Void main(){ char gender; clrscr(); printf(“Enter gender M or F”: ); gender=getchar(); printf(“Your Gender is: “); putchar(gender); getch(); } Ashim Lamichhane 25
  • 26. • getch() and getche() – Reads single character the instant it is typed without waiting for the enter key to be hit. – getch() doesn’t print the character entered – getche() displays the character when entered. – General form • character_variable=getch(); • character_variable=getche(); – In both functions, the character typed is assigned to the char type variable character_variable. Ashim Lamichhane 26
  • 27. • putch() – The function putch() prints a character onto the screen – General form • putch(character_variable); – Character variable is a char type. NOTE: These three functions are defined under the standard library functions conio.h Ashim Lamichhane 27
  • 28. void main(){ char ch1, ch2; clrscr(); printf("Enter 1st character: "); ch1=getch(); printf("n Enter 2nd character"); ch2=getche(); printf("n first character: "); putch(ch1); printf("nSecond character: "); putch(ch2); getch(); } Ashim Lamichhane 28
  • 29. • gets() – Used to read string of text, containing whitespaces, until a new line character is encountered. – General form gets(string_variable); • puts() – Used to display the string onto the terminal – General form: puts(string_variable); Ashim Lamichhane 29
  • 30. void main() { char name[20]; clrscr(); printf("Enter your name:"); gets(name); printf("Your Name is: "); puts(name); getch(); } Ashim Lamichhane 30