0% found this document useful (0 votes)
70 views5 pages

Chap No 08 (Input Output Handling of C)

Uploaded by

kayaniwaheed721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views5 pages

Chap No 08 (Input Output Handling of C)

Uploaded by

kayaniwaheed721
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER # 8

INPUT AND OUTPUT STATEMENTS

In turbo c++, the input and output operations are carried out through built-in function. Through
these functions data can be read from keyboard or displayed on monitor. the execution of the
program can be interrupted by input / output calls. Hence data can be entered or output can be
taken during execution of program.

Unformatted input / output functions:


There are several standard library functions available for unformatted input and output. Some of
them deal with a single character and some deal with a string of characters.

Single character input:


Three most common input functions are: the original getchar ( ) function which was introduced by
Ritchie and Kernighan, and the new functions getch ( ) and getche ( ) which were added later to
suit the microcomputer interactive environment. The three functions are used without arguments,
but you must keep the empty parentheses.

The getchar( ) function:


The function getchar ( ) stands for “ get character “ and inputs a single character from the keyboard.
You can input the character as either a char or int type because getchar ( ) functions treat character
as integers and accept the ASCII value of character as a variable of type int.
Therefore, to input a character, use either of these formats:
int letter;
letter = getchar ( ) ;
OR
char letter ;
letter = getchar ( );

The getch ( ) and getche ( ) functions:


The getch( ) and getche( ) are two very similar function which serve this purpose. The “get”
Means it gets some input and “ch” means it gets a character. The difference b/w two functions is
that the function getche ( ) echoes (displays) the character that you typed to the screen ( the letter
“e” in getche ( ) stans for “echo”) where getch ( ) just returns the character that you typed without
echoing it on the screen.
Single character output:
C contains many functions that handle single character output. Two most popular output functions
are putchar ( ) and putch ( ).

The putchar ( ) and putch ( ) functions:


The function putchar ( ) stands for “put character “ and displays only a single char value on the
monitor. The char can be a character variable or the character itself contained in single quotes.
1. A char constant:
putchar ( ‘ T ‘ );
or
#define TRIANGLE ‘T’
void main (void)
{
Putchar( TRIANGLE );
}
2. A char variable:
void main (void )
{
char letter ;
letter = ‘L’;
putchar ( letter );
}
Only one character is allowed. The instructions
putchar (“ Hi” );
Would generate a compiler error.

String input:
C contains a number of library functions to input test strings. The most common method is the use
of gets ( ) function which is smaller and faster as compared to the formatted input functions.
The gets ( ) function:
The function gets ( ) stands for “get string”. It reads a string from the keyboard , adds the null
character ‘\0’ to it, and assigns it to the required variable , which comes as an argument of the
function. The newline character’\n’ (created by increasing the enter key) signals the end of the
string.

String output:
C contains a number of library functions to output text strings. The most common method is
the use of puts ( ) function which is smaller and faster as compare to the formatted input functions.

The puts ( ) functions:


The function puts ( ) stand for ”put string” and displays a string on the monitor. This function is
complement of input function gets ( ).
The argument must be one of these:
1. A string constant:
Puts (“ best wishes”);
Or
#define GREETING “best wishes”
void main (void)
{
Puts ( GREETING);
}
2. A string variable:
void main (void)
{
Char greeting [30];
Puts (“ enter greeting “);
Gets ( greeting );
Puts (“ Hi”);
Puts ( greeting );
}

Formatted input / output functions:


The scan ( ) and printf ( ) fall under the category of formatted input and output functions
respectively. These functions instruct the compiler how to interpret the various of input data and
let us obtain the output in the specified form. Let us discuss these functions one by one.

The scanf ( ) function:


The scanf ( ) is the most versatile input function because it can handle all of the different variables
and control their formatting. Think of scanf ( ) as meaning SCAN formatted character from the
keyboard.
In general terms, scanf ( ) function consist of two parts: a control string in closed in double
quotation marks followed by a comma, and then a list of variable names. The general form is
Scanf (“ contol string “, variable list );

Table of scanf format specifiers:

The printf ( ) function:


The puts and putchar functions are quite useful but they have several limitations. Neither can
output numeric data, and each can only display one argument. Both puts and putchar can only
display one thing.
The printf is a more versatile because it can display data of all types and can work with multiple
arguments. This function be used to output any combination of numerical values, single characters
and strings.

You might also like