C Programming - Managing Input and Output Operations
C Programming - Managing Input and Output Operations
Output Operations
In this tutorial you will learn about Single character input output, String input and output,
Formatted Input For Scanf, Input specifications for real number, Input specifications for a
character, Printing One Line, Conversion Strings and Specifiers, Specifier Meaning
Introduction
at the beginning.
The basic operation done in input output is to read a characters from the standard input
device such as the keyboard and to output or writing it to the output unit usually the
screen. The getchar function can be used to read a character from the standard input
device. The scanf can also be used to achieve the function. The getchar has the following
form.
Variable name is a valid ‘C’ variable, that has been declared already and that possess the
type char.
Example program :
# include < stdio.h > // assigns stdio-h header file to your program
void main ( ) // Indicates the starting point of the program.
{
char C, // variable declaration
printf (“Type one character:”) ; // message to user
C = getchar () ; // get a character from key board and
Stores it in variable C.
Printf (” The character you typed is = %c”, C) ; // output
} // Statement which displays value of C on
// Standard screen.
The putchar function which in analogus to getchar function can be used for writing
characters one at a time to the output terminal. The general form is
Where variable is a valid C type variable that has already been declared Ex:-
Putchar ( );
#include < stdio.h > // Inserts stdio.h header file into the Pgm
void main ( ) // Beginning of main function.
{
char in; // character declaration of variable in.
printf (” please enter one character”); // message to user
in = getchar ( ) ; // assign the keyboard input value to in.
putchar (in); // out put ‘in’ value to standard screen.
}
The gets function relieves the string from standard input device while put S outputs the
string to the standard output device. A strong is an array or set of characters.
The function gets accepts the name of the string as a parameter, and fills the string with
characters that are input from the keyboard till newline character is encountered. (That is
till we press the enter key). All the end function gets appends a null terminator as must be
done to any string and returns.
The puts function displays the contents stored in its parameter on the standard screen.
gets (str)
puts (str)
The formatted input refers to input data that has been arranged in a particular format.
Input values are generally taken by using the scanf function. The scanf function has the
general form.
The control string specifies the field format which includes format specifications and
optional number specifying field width and the conversion character % and also blanks,
tabs and newlines.
The Blanks tabs and newlines are ignored by compiler. The conversion character % is
followed by the type of data that is to be assigned to variable of the assignment. The field
width specifier is optional.
% x d
Here percent sign (%) denotes that a specifier for conversion follows and x is an integer
number which specifies the width of the field of the number that is being read. The data
type character d indicates that the number should be read in integer mode.
Example :
If the values input are 175 and 1342 here value 175 is assigned to sum1 and 1342 to sum
2. Suppose the input data was follows 1342 and 175.
The number 134 will be assigned to sum1 and sum2 has the value 2 because of %3d the
number 1342 will be cut to 134 and the remaining part is assigned to second variable
sum2. If floating point numbers are assigned then the decimal or fractional part is skipped
by the computer.
To read the long integer data type we can use conversion specifier % ld & % hd for short
integer.
Field specifications are not to be use while representing a real number therefore real
numbers are specified in a straight forward manner using % f specifier.
Example:
If the number input is a double data type then the format specifier should be % lf instead
of %f.
% xc or %xs
Where C and S represents character and string respectively and x represents the field
width.
The address operator need not be specified while we input strings.
Example :
Here suppose the input given is a, Robert then a is assigned to ch and name will be
assigned to Robert.
Printing One Line:
printf();
The most simple output statement can be produced in C’ Language by using printf
statement. It allows you to display information required to the user and also prints the
variables we can also format the output and provide text labels. The simple statement
such as
Output:
Both the messages appear in the output as if a single statement. If you wish to print the
second message to the beginning of next line, a new line character must be placed inside
the quotation marks.
For Example :
printf (“Hello!\n);
OR
The printf ( ) function is quite flexible. It allows a variable number of arguments, labels
and sophisticated formatting of output. The general form of the printf ( ) function is
Syntax
The conversion string includes all the text labels, escape character and conversion
specifiers required for the desired output. The variable includes all the variable to be
printed in order they are to be printed. There must be a conversion specifies after each
variable.
Specifier Meaning
%c – Print a character
%d – Print a Integer
%i – Print a Integer
%e – Print float value in exponential form.
%f – Print float value
%g – Print using %e or %f whichever is smaller
%o – Print actual value
%s – Print a string
%x – Print a hexadecimal integer (Unsigned) using lower case a – F
%X – Print a hexadecimal integer (Unsigned) using upper case A – F
%a – Print a unsigned integer.
%p – Print a pointer value
%hx – hex short
%lo – octal long
%ld – long