You are on page 1of 29

Chapter 5

INPUT & OUTPUT

2 HOURS
I/O OPERATION 2

 The operations that deal with interaction of console related scenario


for the sake of getting input from user or displaying output to the user
are known as I/O operations

 In simple words, console input and output means to read from or


write to a screen.
I/O OPERATION 3
 Standard I/O related library functions are listed under <stdio.h> header file.

 Console related I/O library functions are listed under <conio.h> header file.

 Some of the example are:


a) printf()  formatted output
b) scanf()  formatted input
c) getchar() get a single character from entered value for storage
d) putchar() put a single character for display
e) gets() get a string of characters from entered value for storage
f) puts()  put a string of characters for display
4

Formatted I/O
FORMATTED INPUT 5
 printf() and scanf() allow conversion between ASCII representations and internal data types.

 Formatted string contains text to be read/written, and formatting characters that describe how data is to be
read/written.

 For formatted input, scanf() function is used.

 Syntax:
scanf (“datatype_format”, &variable_name);

The function scan() reads and converts characters from the standards input depending on the format specification string
and stores the input in memory location of the variable

 For Example:
scanf(“%d”,&Name);
6

FORMAT SPECIFIERS

Conversion Character Meaning


d The data is converted to decimal (integer)
c The data is taken as a character.
The data is a string and character from the string , are
s
printed until a NULL, character is reached.
The data is output as float or double with a default
f
Precision 6.
E.g. of formatted input 7
#include<stdio.h>
#include<conio.h>
Output
Int main()
{ Enter value of a: 5↵
int a; The value you entered is: 5
clrscr();
printf(“enter value of a:”);
scanf(“%d”,&a);
printf(“\n The value you entered is: %d”,a);
getch();
return 0;
}
FORMATTED OUTPUT 8
 The function printf() is used for formatted output to standard output based on a format
specification.

 The format specification string, along with the data to be output, are the parameters to the printf()
function.

 Syntax:
printf(“text + datatype_format”, variable_name);

 For Example:
printf(“my roll number is %d”,roll);
Datatype formats 9

Conversion Character Meaning


d The data is converted to decimal (integer)
c The data is taken as a character.
The data is a string and character from the string , are
s
printed until a NULL, character is reached.
The data is output as float or double with a default
f
Precision 6.
FORMATTED I/O environments 10
 Multiple variables can be simultaneously taken as input or output as per programmer’s ease.

 E.g.
1. scanf(“%d %d %f %c”, &sn, &roll, &percent, &gpa);
2. Printf(“My roll is %d and my grade is %c”, roll_no, grade);
11
UNFORMATTED INPUT 12
 These are used to read a single input from user at console

 Unlike formatted input, it doesn’t require the specification of format or conversion characters.
 It can directly transfer the value to memory

 E.g. are
1. Gets()  Take a string as input
2. Getchar  take a character as input
3. Getche() take a character as input (it also echoes or displays the entered char]
UNFORMATTED OUTPUT 13
 These are used to display output to user at console

 Unlike formatted output, it doesn’t require the specification of format or conversion characters.
 It can directly transfer the value from memory

 E.g. are
1. puts()  Display a string (collection of characters and numbers)
2. putch()  display a single character
3. putchar() display a single character
Getch() 14

 getch() reads a single character from the keyboard.

 This code pauses the output console until a key is pressed.


 Because of this reason, it is also used to hold the console till all of the output messages
are displayed.
Example 15

#include <stdio.h>
#include <conio.h>
int main()
{
printf(" Passed value by the User is: %c", getch()); // print a character entered by the user
return 0;
}
16
17
Putch() 18

 The putch() function is used for printing character to a screen at current


cursor location.

 It displays only one character at a time.

putch(character);
OR
putch(character_variable);
Example 19
#include<stdio.h>
#include<conio.h>

int main()
{
char ch;
clrscr();
printf(“press any character: ");
scanf("%c", &ch);
printf("\n Thank you for entering");
printf("\n Pressed character is:");
putch(ch); //print the value of "ch" variable in screen
getch(); /* Holding output */
return 1;
}
Getche() 20

[Get Character with Echo]


 Apart from reading, it will also echo the input character on to the output screen.

 It reads a single character from the keyboard and displays immediately on output
screen without waiting for enter key.
21

#include <stdio.h>
#include <conio.h>
// Example for getche() in C
int main()
{
printf("%c", getche());
return 0;
}
22

#include<stdio.h>
#include<conio.h>

int main()
{
char ch;
printf("Press any character: ");
ch = getche();
printf("\n Pressed character is: %c", ch);
return 0;
}
getchar() and putchar() 23
 The getchar() function reads a character from a standard input device.
 Syntax is: character_variable = getchar();
 The getchar() function makes wait until a key is pressed and then assigns this
character to character_variable.
 The putchar() function displays a character to the standard output device.
 Syntax is: putchar(character_variable);
24
example:
int main()
{
char ch1;
printf(“enter a character=”);
ch1= getchar();
printf(“\nthe entered character is:”);
putchar(ch1);
getch();
return 0;
}
25

Output:
Enter a character= A
The entered character is: A
gets() and puts()
26
gets():
 The gets() function is used to read a string of text, containing whitespaces,
until a newline character is encountered.
 It can be used as alternative function for scanf() function for reading strings.
 Unlike scanf() function, it does not skip whitespaces (i.e. It can be used to
read multi-words string).
 Syntax: gets(string_variable);
puts():
 The puts() function is used to display the string on the screen.
 Syntax: puts(string_variable);
Examplee 27
28

Output:
Enter your name: RISHI RAI
Your name is: RISHI RAI
Important Questions 29

 Explain formatted and unformatted output functions with suitable examples.


 Explain formatted i/o functions with suitable examples
 Differentiate the use of gets(),getchar(), getch() and getche() with suitable
examples.
 Differentiate the use of puts(),putchar(), and putch() with suitable examples.

You might also like