SlideShare a Scribd company logo
1 of 26
R.Sandhiya,
AP,CIT
Console I/O Functions
Formatted I/O :
scanf(),printf(),getchar(),putchar()
C Input and Output
Input - provide the program with some data to be used
Output - display data on screen or write the data to a printer or
a file.
- Built-in functions are present in C header files
- I/O is essentially done one character (or byte) at a time
stream -- a sequence of characters flowing from one place to
another
input stream: data flows from input device (keyboard, file, etc) into
memory
output stream: data flows from memory to output device
(monitor, file, printer, etc)
Standard I/O streams
•stdin: standard input stream (default is keyboard)
•stdout: standard output stream (defaults to monitor)
•stderr: standard error stream
Formatted I/O -- refers to the conversion of data to and from
a stream of characters, for printing (or reading) in plain text
format
• All text I/O we do is considered formatted I/O
• other option is reading/writing direct binary information
Syntax:
printf (format_string, list_of_expressions);
Printing Integers
int numStudents = 35123;
printf(“I Year count is %d students", numStudents);
// Output:
// I Year count is 35123 students
• We can specify the field width
(i.e. how many 'spaces' the item prints in).
• Defaults to right-justification.
• Place a number between the % and the d.
• Example:
field width is 10:
printf(" I Year count is %10d students", numStudents);
Output:
I Year count is 35123 students
To left justify, use a negative number
in the field width:
printf("FSU has %-10d students", numStudents);
// Output:
// FSU has 35123 students
If the field width is too small or left unspecified,
it defaults to the minimum number of characters
required to print the item:
• printf("FSU has %2d students", numStudents);
// Output:
// FSU has 35123 students
Printing Floating-point numbers
Use the %f modifer to print floating point values in fixed notation:
//example
double cost = 123.45;
printf("Your total is $%f todayn", cost);
// Output:
// Your total is $123.450000 today
- control the decimal precision - the number of places after the decimal.
- Output will round to the appropriate number of decimal places, if necessary:
• printf("Your total is $%.2f todayn", cost);
// Output:
// Your total is $123.45 today
-Controlling field width :
• printf("Your total is $%9.2f todayn", cost);
// Output:
// Your total is $ 123.45 today
Printing characters and strings
•Use the formatting specifier %c for characters.
•Default field size is 1 character:
•char letter = 'Q'; printf("%c%c%cn", '*', letter, '*');
•// Output is: *Q*
•Use %s for printing strings.
•Field widths :
•printf("%s%10s%-10sENDn", "Hello", "Alice", "Bob");
//Output ???????? assignment
Scanf():
- read data in from standard input (keyboard)
- Syntax:
- scanf(format_string, list_of_variable_addresses);
- White space is skipped by default in consecutive numeric reads.
- But it is not skipped for character/string inputs.
#include <stdio.h>
int main()
{
int i;
float f;
char c;
printf("Enter an integer and a float, then Y or Nn> ");
scanf("%d%f%c", &i, &f, &c);
printf("You entered:n");
printf("i = %d, f = %f, c = %cn", i, f, c);
}
Sample run #1
Enter an integer and a float, then Y or N
> 34 45.6Y
You entered: i = 34, f = 45.600, c = Y
Sample Run #2
Enter an integer and a float,
then Y or N
 12 34.5678 N
 You entered: i = 12, f = 34.568, c =
• character that was read was NOT the letter 'N'.
• It was the space.
• (Remember, white space not skipped on character
reads).
• Consider if the scanf line looked like this:
scanf("%d%f %c", &i, &f, &c);
• There's a space between the %f and the %c in the format string.
• This allows the user to type a space.
Suppose this is the typed input:
12 34.5678 N
• Then the character variable c will now contain the 'N'.
Assignment :
#include <stdio.h>
int main()
{
int i;
double f;
char c;
printf("Enter an integer and a float, then Y or Nn> ");
scanf("%d%lf %c", &i, &f, &c);
printf("You entered:n");
printf("i = %d, f = %f, c = %cn", i, f, c);
}
#include <stdio.h>
int main()
{
int i;
scanf("%1d",&i);
printf("%d",i);
return 0;
}
//Output:
//?
getchar() & putchar() functions:
• getchar()
-reads a character from the terminal
- returns it as an integer
- reads only single character at a time
- to read more than one character – LOOP
putchar() :
- Displays char passed to it on the screen
- returns the same character
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character");
c = getchar();
putchar(c);
}
#include<stdio.h>
void main()
{
char str[100];
printf("Enter a string");
gets( str );
puts( str );
getch();
}
ASSIGNMENT:
Difference Between Scanf and gets()?
Thanks!
rsandhiya@cit.edu.in

More Related Content

What's hot

20 C programs
20 C programs20 C programs
20 C programs
navjoth
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
ecko_disasterz
 

What's hot (20)

CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
T03 b basicioscanf
T03 b basicioscanfT03 b basicioscanf
T03 b basicioscanf
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
CP Handout#1
CP Handout#1CP Handout#1
CP Handout#1
 
Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1
 
CP Handout#3
CP Handout#3CP Handout#3
CP Handout#3
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMCLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
 
CP Handout#4
CP Handout#4CP Handout#4
CP Handout#4
 
Conversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad balochConversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad baloch
 
How to code?
How to code?How to code?
How to code?
 
20 C programs
20 C programs20 C programs
20 C programs
 
Presention programming
Presention programmingPresention programming
Presention programming
 
c++
c++c++
c++
 
Data types
Data typesData types
Data types
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
 
AS TASKS #8
AS TASKS #8AS TASKS #8
AS TASKS #8
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
 
C program
C programC program
C program
 

Similar to I o functions

Similar to I o functions (20)

Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 
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
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
 
C Language Programming Introduction Lecture
C Language Programming Introduction LectureC Language Programming Introduction Lecture
C Language Programming Introduction Lecture
 
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
 
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
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
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
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Cpu
CpuCpu
Cpu
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
Input and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesInput and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequences
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Input And Output
 Input And Output Input And Output
Input And Output
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 

Recently uploaded

Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating System
Sampad Kar
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
drjose256
 

Recently uploaded (20)

Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating System
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdf
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Low Altitude Air Defense (LAAD) Gunner’s Handbook
Low Altitude Air Defense (LAAD) Gunner’s HandbookLow Altitude Air Defense (LAAD) Gunner’s Handbook
Low Altitude Air Defense (LAAD) Gunner’s Handbook
 
AI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdfAI in Healthcare Innovative use cases and applications.pdf
AI in Healthcare Innovative use cases and applications.pdf
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
Tembisa Central Terminating Pills +27838792658 PHOMOLONG Top Abortion Pills F...
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 

I o functions

  • 3. C Input and Output Input - provide the program with some data to be used Output - display data on screen or write the data to a printer or a file. - Built-in functions are present in C header files - I/O is essentially done one character (or byte) at a time
  • 4. stream -- a sequence of characters flowing from one place to another input stream: data flows from input device (keyboard, file, etc) into memory output stream: data flows from memory to output device (monitor, file, printer, etc)
  • 5. Standard I/O streams •stdin: standard input stream (default is keyboard) •stdout: standard output stream (defaults to monitor) •stderr: standard error stream
  • 6. Formatted I/O -- refers to the conversion of data to and from a stream of characters, for printing (or reading) in plain text format • All text I/O we do is considered formatted I/O • other option is reading/writing direct binary information Syntax: printf (format_string, list_of_expressions);
  • 7. Printing Integers int numStudents = 35123; printf(“I Year count is %d students", numStudents); // Output: // I Year count is 35123 students
  • 8. • We can specify the field width (i.e. how many 'spaces' the item prints in). • Defaults to right-justification. • Place a number between the % and the d. • Example: field width is 10: printf(" I Year count is %10d students", numStudents); Output: I Year count is 35123 students
  • 9. To left justify, use a negative number in the field width: printf("FSU has %-10d students", numStudents); // Output: // FSU has 35123 students
  • 10. If the field width is too small or left unspecified, it defaults to the minimum number of characters required to print the item: • printf("FSU has %2d students", numStudents); // Output: // FSU has 35123 students
  • 11. Printing Floating-point numbers Use the %f modifer to print floating point values in fixed notation: //example double cost = 123.45; printf("Your total is $%f todayn", cost); // Output: // Your total is $123.450000 today
  • 12. - control the decimal precision - the number of places after the decimal. - Output will round to the appropriate number of decimal places, if necessary: • printf("Your total is $%.2f todayn", cost); // Output: // Your total is $123.45 today
  • 13. -Controlling field width : • printf("Your total is $%9.2f todayn", cost); // Output: // Your total is $ 123.45 today
  • 14. Printing characters and strings •Use the formatting specifier %c for characters. •Default field size is 1 character: •char letter = 'Q'; printf("%c%c%cn", '*', letter, '*'); •// Output is: *Q* •Use %s for printing strings. •Field widths : •printf("%s%10s%-10sENDn", "Hello", "Alice", "Bob"); //Output ???????? assignment
  • 15. Scanf(): - read data in from standard input (keyboard) - Syntax: - scanf(format_string, list_of_variable_addresses); - White space is skipped by default in consecutive numeric reads. - But it is not skipped for character/string inputs.
  • 16. #include <stdio.h> int main() { int i; float f; char c; printf("Enter an integer and a float, then Y or Nn> "); scanf("%d%f%c", &i, &f, &c); printf("You entered:n"); printf("i = %d, f = %f, c = %cn", i, f, c); }
  • 17. Sample run #1 Enter an integer and a float, then Y or N > 34 45.6Y You entered: i = 34, f = 45.600, c = Y Sample Run #2 Enter an integer and a float, then Y or N  12 34.5678 N  You entered: i = 12, f = 34.568, c =
  • 18. • character that was read was NOT the letter 'N'. • It was the space. • (Remember, white space not skipped on character reads).
  • 19. • Consider if the scanf line looked like this: scanf("%d%f %c", &i, &f, &c); • There's a space between the %f and the %c in the format string. • This allows the user to type a space. Suppose this is the typed input: 12 34.5678 N • Then the character variable c will now contain the 'N'.
  • 20. Assignment : #include <stdio.h> int main() { int i; double f; char c; printf("Enter an integer and a float, then Y or Nn> "); scanf("%d%lf %c", &i, &f, &c); printf("You entered:n"); printf("i = %d, f = %f, c = %cn", i, f, c); }
  • 21. #include <stdio.h> int main() { int i; scanf("%1d",&i); printf("%d",i); return 0; } //Output: //?
  • 22. getchar() & putchar() functions: • getchar() -reads a character from the terminal - returns it as an integer - reads only single character at a time - to read more than one character – LOOP putchar() : - Displays char passed to it on the screen - returns the same character
  • 23. #include <stdio.h> void main( ) { int c; printf("Enter a character"); c = getchar(); putchar(c); }
  • 24. #include<stdio.h> void main() { char str[100]; printf("Enter a string"); gets( str ); puts( str ); getch(); }