MANAGING INPUT
AND OUTPUT OPERATIONS
All input/output operations are
carried out through functions called as printf and
scanf. There exist
several functions that have become standard for input and output
operations in C. These functions
are collectively known as standard i/o library. Each
program that uses standard I/O
function must contain the statement
#include<stdio.h>
The file name stdio.h is an abbreviation of standard
input-output header file.
READING A CHARACTER
Reading a single character can
be done by using the function getchar. The getchar
takes the following form:
variable_name = getchar();
Program::
/*Reading a character from
terminal*/
#include<stdio.h>
main()
{
char ans;
printf(“Would you like to know
my name? \n”);
printf(“Type Y for yes and N for
no”);
ans=getchar();
if(ans ==’Y’ || ans = =’y’)
printf(“\n\n My name is India
\n”);
else
printf(“\n\n You are good for
nothing \n”);
}
OUTPUT
Would you like to know my name?
Type Y for yes and N for no:Y
My name is India
Would you like to know my name?
Type Y for yes and N for no:n
You are good for nothing
WRITING A
CHARACTER::
Like getchar, there is an
analogous function putchar for writing characters one at a
time to the terminal. It takes
the form as shown below:
putchar (variable_name);
Program
/*A program to read a character
from keyboard and then prints it in reverse case*/
/*This program uses three new
functions:islower,toupper,and tolower.
#include<stdio.h>
#include<ctype.h>
main()
{
char alphabet;
printf(“Enter an alphabet”);
putchar(‘\n’);
alphabet = ghetchar();
if(islower(alphabet))
putchar(toupper(alphabet));
else
putchar(tolower(alphabet));
}
OUTPUT
Enter An alphabet
a
A
Enter An alphabet
Q
q
Enter An alphabet
z
Z
Reading Mixed
Data Types
It is possible to use one scanf
statement to input a data line containing mixed mode
data. In such cases, it should be
ensured that the input data items match the control
specifications in order and
type.
Eg: scanf(“%d %c %f
%s”,&count, &code, &ratio, &name);
Scanf Format
Codes
Code Meaning
%c Read a single character
%d Read a decimal integer
%e Read a floating point value
%f Read a floating point value
%g Read a floating point value
%h Read a short integer
%i Read a decimal, hexadecimal, or octal
integer
%o Read an octal integer
%s Read a string
%u Read an unsigned decimal integer
%x Read a hexa decimal integer
%[..] Read a string of word(s)
FORMATTED OUTPUT
The printf statement
provides certain features that can be effectively exploited to control
the alignment and spacing of
print-outs on the terminals.The general form of printf statement
is
printf(“control
string”,arg1, arg2…argn);
Mixed Data
Output
It is permitted to mix data types
in one printf statements.
Eg: printf(“%d %f %s %c”,a, b, c, d);
No comments:
Post a Comment
Please comment bellow. Share your knowledge