Computer-ProgrammingChapter-4-Input-and-Output
Computer-ProgrammingChapter-4-Input-and-Output
Bishon Lamichhane
Topics
4.1 Introduction to data I/O in C
4.2 Unformatted I/O
4.2.1 Character I/O
4.2.2 String I/O
4.3 Formatted I/O
4.3.1 Control String(flags, field width, precision and specifier)
4.3.2 Formatted I/O(scanf(), printf())
Compiled by Bishon 2
Input/Output Operation
Ø Input means to provide the program with some data to be used in the
program and output means to display data on screen or write the data to
a printer or a file.
Ø C programming language provides many built-in functions to read
any given input and to display data on screen when there is a need to
output the result.
Ø All these built-in functions are present in C header files.
Compiled by Bishon 3
Fig.: Standard input/output
Compiled by Bishon 4
Types of Input/Output
Ø There are two types of input/output statements:
1. Formatted input/output
2. Unformatted input/output
Formatted Input/Output
Ø It allows us to specify the different formats for input and output.
Ø User can decide the format for reading different values and displaying
them in different formats.
Ø All types of data can be handled by formatted input/output.
Ø scanf( ) and printf( ) are the formatted input and output statements
respectively.
Compiled by Bishon 5
printf( ) – formatted output function used to display messages and
values on a screen
Syntax:
printf(“control string”,arg1,arg2,……);
Øwhere, arg1,arg2,...are the variables/expression whose values are to be
displayed and control string may contain only the message to be
displayed or the format specifier for different data types or
combination of both.
ØIt can also contain different escape sequences(\n,\t, etc).
Compiled by Bishon 6
Ø General form of control string can be written as:
Ø %<flag>w.pX
Ø where, flag is a character or symbol(-,0,+-, etc) that is used to change
the appearance of output.
Ø w is the width specifier and p is the precision specifier(used only for
float and string data).
Ø X is the conversion character which is different for various data
types:
Compiled by Bishon 7
Data type Conversion character(X) Format Specifier
char c %c
int d or i %d or %i
float f %f
double lf %lf
long int ld %ld
unsigned int u %u
string s %s
Compiled by Bishon 9
Illustrations:
a. Formatted I/O for integer data
i. Formatted output
Ø general form: %<flags> wd
Ø where w is field width and flags represent some characters (–, 0, +–,
etc.) that are used to change the appearance of output.
Compiled by Bishon 10
#include<stdio.h>
void main ( )
{
int a = 12345;
printf ("\ncase 1: %d", a); // w not specified
printf ("\ncase 2: %15d", a);
printf ("\ncase 3: %-15d", a);
printf ("\ncase 4: % 015d", a);
printf ("\ncase 5: % 3d", a);//w less than width of data to print
}
Compiled by Bishon 11
Output:
case 1: 12345
case 2: . . . . . . . . . .12345
case 3: 12345. . . . . . . . . .
case 4: 000000000012345
case 5: 12345
Compiled by Bishon 12
Formatted input
Ø general form: %wd
Ø where w is field width
e.g.,
#include<stdio.h>
void main ()
{
int a, b;
scanf ("% 3d % 4d", & a, &b);
printf ("\n a = % d \n b = %d", a, b);
}
Compiled by Bishon 13
Output 1:
12 1234
a = 12
b = 1234
Output 2:
12345 6789
a = 123
b = 45
Note: If w is not specified then it can read the integers of any width
within the range of int data type.
Compiled by Bishon 14
Formatted character I/O
Ø For printing character we use printf function and general form of
format specifier is : % wc
Ø where, w is field width of w column with right justification
#include<stdio.h>
void main ( )
{
char ch = 'C';
printf ("\n case 1: % c", ch) ;
printf ('\n case 2 : % 5c", ch) ;
printf ("\n case 3: % -5c", ch) ;
}
Compiled by Bishon 15
Output:
case 1 : C
case 2 : . . . . C
case 3 : C . . . .
ØFor inputing a character we use scanf function as,
Øscanf (" %c", &ch) ; //note the space before %c
ØSpace before %c is used to ignore the white space characters(tab,
space, enter) before entering a character.
Compiled by Bishon 16
Formatted I/O string data
Ø The formatted output string data has the specification as below:
Ø % w.ps
Ø Where w specifies the field for display and p instructs that only first p
characters of the string are to be displayed.
Ø The display is right-justified unless - flag is used.
Compiled by Bishon 17
#include<stdio.h>
void main ()
{
char a[ ]="kathmandu";
printf ("\ncase 1: %s", a);
printf ("\ncase 2: %15.4s", a);
printf ("\ncase 3: % -15.6s", a);
printf ("\ncase 4: %5.4s",a);
printf ("\ncase 5: %15s",a);
}
Compiled by Bishon 18
Output:
case 1: kathmandu
case 2: . . . . . . . . . . . kath
case 3: kathma . . . . . . . . .
case 4: . kath
case 5: . . . . . . kathmandu
Compiled by Bishon 19
Ø The formatted input string data has the specifications as below:
Ø % ws
Ø where w denotes maximum number of characters that can be input.
But reading is terminated after any white space is encountered.
Ø If w is not specified , it reads all the characters until a white space is
encountered. But the number of characters must be less than size of
string.
Compiled by Bishon 20
#include<stdio.h>
void main()
{
char a[ 20];
printf("Enter any string:");
scanf("%s",a); //Here, a itself denotes the address of string
printf("The string you entered = %s",a);
}
Compiled by Bishon 21
Output1:
Enter any string: ramesh
The string you entered = ramesh
Output2:
Enter any string: ramesh thapa
The string you entered = ramesh
[Here, reading is terminated after space is encountered]
Compiled by Bishon 22
#include<stdio.h>
void main()
{
char a[20];
printf("Enter any string:");
scanf("%5s",a);
printf("The string you entered = %s",a);
}
Output:
Enter any string: ramesh
The string you entered = rames
[Here, only first 5 characters are read]
Compiled by Bishon 23
Ø Another way to read strings using scanf function is to use the formats
%[characters] and %[^characters].
Ø They are known as defining search set to read strings. String with
white spaces can be read using this formats. %[characters] means
that only the characters defined inside the bracket can be read and
reading is terminated after encounter of any other characters.
%[^characters] means that reading is continued until the characters
defined after ^ is encounterd or reading is terminated after any of
those characters are encountered.
Compiled by Bishon 24
#include<stdio.h>
void main()
{
char a[20];
printf("Enter any string:");
scanf("%[a-z 0-9]",a);
printf("The string you entered is = %s",a);
}
Compiled by Bishon 25
Output1:
Enter any string:ramesh thapa123
The string you entered is = ramesh thapa123
[Here, all the lower case letters, space and digits are read]
Output2:
Enter any string:ramesh Thapa123
The string you entered is = Ramesh
[Here, reading is terminated after T is encountered as it is not defined in
the search set]
Compiled by Bishon 26
Example2:
#include<stdio.h>
void main()
{
char a[20];
printf("Enter any string:");
scanf("%[^b]",a);
printf("The string you entered is = %s",a);
}
Output:
Enter any string:Volleyball
The string you entered is = Volley
[Here, reading is terminated after
Compiled byb is encountered]
Bishon 27
Formatted I/O for float data
i. Formatted output
Ø general form: %<flags> w.pf
Ø where w is field width, flags represent some characters (–, 0, +–, etc.)
that are used to change the appearance of output, and p represents
precision specifier.
Compiled by Bishon 28
#include<stdio.h>
void main ( )
{
float a = 12.3456;
printf ("\ncase 1: %f", a); // w not specified
printf ("\ncase 2: %15.2f", a);
printf ("\ncase 3: %-15.1f", a);
printf ("\ncase 4: % 015.3f", a);
printf ("\ncase 5: %0.2f", a);
}
Compiled by Bishon 29
Output:
case 1: 12.345600
case 2: . . . . . . . . . .12.35
case 3: 12.3. . . . . . . . . . .
case 4: 00000000012.346
case 5: 12.35
Compiled by Bishon 30
Unformatted Input/Output
a. Character I/O
i. Input functions
getchar ( ) - reads a single character from user
Syntax: character_variable = getchar ( );
Compiled by Bishon 31
void main ( )
{
char ch;
printf ("\nEnter a character");
ch = getchar ( );
printf ("\nEntered character is : %c", ch);
}
Compiled by Bishon 32
Ø For reading characters we can also use getch ( ) and getche ( ).
Syntax:character_variable = getche ( ) ;
character_variable = getch ( ) ;
e.g.,ch = getchar ( ) ;
ch = getche ( ) ;
Compiled by Bishon 33
Ø getche( ) – reads a character from console and echoes to the screen. It
allows a character to be entered without having to press the Enter key
afterwards.
Syntax: ch=getche( ); where, ch is any character type variable.
Compiled by Bishon 34
e.g., void main( )
{
char ch;
printf(“enter any character:”);
ch=getche( );
printf(“entered character is %c”,ch);
}
Output:
enter any character:N
entered character is N
Compiled by Bishon 35
Ø getch( ) - reads a character from console but does not echo to the
screen. It also allows a character to be entered without having to press
the Enter key afterwards.
Ø Syntax: ch=getch( ); where, ch is any character type variable.
Compiled by Bishon 36
e.g., void main( )
{
char ch;
printf(“enter any character:”);
ch=getch( );
printf(“entered character is %c”,ch);
}
Output:
enter any character:
entered character is N
//Here, the entered character N is not echoed to the screen.
Compiled by Bishon 37
Difference between getch ( ), getchar ( ), and getche ( ):
Øgetchar ( ) – wait for a character followed by the Enter key
Øgetch ( ) – waits for a character and does not echo to screen
Øgetche ( ) – waits for a character and echoes to screen.
Compiled by Bishon 38
Output functions
putchar( )
Ø It is an unformatted output statement for printing a character.
Syntax: putchar(ch);
where 'ch' is any character variable.
Ø For printing a character we can also use putch ( ) .
Syntax: putch (character_variable)
Compiled by Bishon 39
void main ( )
{
char ch;
printf ("\nEnter a character");
ch = getchar ( );
printf ("\nEntered character is :");
putchar(ch);
}
Compiled by Bishon 40
String I/O
i. Input function
Ø gets( ) – It is an unformatted input function for reading strings.
Syntax: gets(str);
Ø where 'str' is any string variable.
Compiled by Bishon 41
Output function
Ø puts ( ) - It displays the string stored in string variable
Ø Syntax: puts (string_variable);
e.g.,
void main ( )
{
char str [20];
printf ("\n Enter a string");
gets (str);
printf ("\n Entered string is:");
puts (str);
} Compiled by Bishon 42
Chapter 4 Ends Here
Compiled by Bishon 43