Lesson 5: Managing Input and Output Functions.
We have oftenly used two functions printf and scanf for writing of data and reading of data from terminal. Each program that uses a standard input/output function must contain the statement
#include<stdio.h>
However, this is not necessary for printf and scanf, stdio.h stands for standard input-output header file.
Reading a Character:
Reading a single character from terminal can be done by using the function getchar().
ex. char name;
name=getchar();
When this statement is encountered, the computer waits until a key is pressed and then assigns this character as a value to getchar function. In the above example if key H on the keyboard is pressed while execution of progra,, it will be stored in the variable name.
Program:-
#include<stdio.h>
main()
{
char answer;
printf("Would U like to know mu name?\n");
printf("Type Y for yes and N for no");
answer=getchar();
if(answer=='y' || answer=='Y')
printf("\nMy name is Computer");
else
printf("U are good for nothing");
}
Program:- For testing whether entered character is numeric/alphanumeric or alphabetic using character type function.
#include<stdio.h>
#include<ctype.h>
main()
{
char character;
printf("Press any key\n");
character=getchar();
if(isalpha(character)>0)
printf("The character is a letter");
else if(isdigit(character)>0)
printf("The character is a digit");
else
printf("The character is alphanumeric");
}
Writing Character:
Like getchar(), there is a function putchar() for writing characters one at a time.
ex. answer='Y';
putchar(answer);
this will display Y on the screen.
Program:- getchar() example.
#include<stdio.h>
#include<ctype.h>
main()
{
char alphabet;
printf("\nEnter an alphabet");
putchar("\n);
alphabet=getchar();
if(islower(alphabet))
putchar(toupper(alphabet));
else
putchar(tolower(alphabet);
}
Formatted Input:
Formatted input refers to an input data that has been arranged in a particular format.
Consider the following data:
15.57 123 Radha
These are three types of data. First should read into a variable float, the second into int and the third into char. This is possible in C using scanf function.
scanf("control string",arg1,arg2,...argn);
Control string: It must contain a format specification consisiting a conversion character % and a type specifier and an optional number specifing fieldwidth.
%wd -- Field Specification for reading an integer number.
Consider following examples:
1) scanf("%2d%5d",&num1,&num2)
Here you can assign maximum 2 digit integer number to variable num1 and 5 digit to variable num2.
2) scanf("%2f",&amt);
Here you can read a number with maximum two decimal places and assign it to variable amt.
Formatted Output:
printf function can be effectively used to print formatted output on the screen.
printf("control string",arg1,arg2,...argn);
Control string consists of three types of items
1) Characters that will be printed on screen as they appear
ex. printf("Hello! How R U?");
2) Format specifications that define the output format for display of each item.
ex. printf("%5d",x);
3) Escape sequence characters.
ex. printf("\n");
Output of real numbers:-
ex:-y=98.7654
Format | Output |
printf("%7.4f",y); | 98.7654 |
printf("%7.2f",y); | 98.77 |
printf("%-7.2f",y); | 98.77 |
printf("%f",y); | 98.7654 |
Printing of Strings:
The effect of a variety of specifications in printing a string "NEW DELHI - 111001" containing 16 characters is as below
Specification | Output |
%s | NEW DELHI _ 111001 |
%20.10s | NEW DELHI |
%.5s | NEW D |
%-20.10s | NEW DELHI |