INPUT / OUTPUT

OVERVIEW

               In previous, we have studied the basics of C programs. This covers basic input and output features of C language. Usually input and output from an important part of any program. To be more interactive, a program needs to be able to accept data and show results.

              In C, standard input/output library provides functions to perform input and output operations. By standard input and output, we mean the keyboard and monitor respectively. In C, these input/output operations are performed by two standard input/output functions, these are print f() and scan f(). These functions can be accessed by including the standard input/output library (stdio.h) in the program. Let us have an overview of standard input/output functions.


Print f Function:

               "Print f Function is a function which use to display the output on the screen". 
To see results of program execution, we must have a way to specify what variables values should be displayed. The standard library function print f (pronounced as print-eff) is used for formatted output. It takes as arguments a format string and an optional list of variables to output. The values of variables are displayed according to the specifications in the format string.

The print f () function will take the form:
                print f(format string, var 1, var 2, var 3,.....);
                       print f(format string);

The format string is a character string - nothing more and the variable are optional. The easiest way to understand this is by example.

The signature of a function describes he number and type of its arguments, and the return type of the function. 

Example:

Write a program to calculate and print the area of a square.

#include <stdio.h>
void  main ()
{
    int    height, width, area;
    height   = 5;
    width    = 4;
    area  = height   *    width;
    print f ("Area of Square =  %d",  area);
 }
Here's the output of the program:
Area of square = 20
In the above program, the first line is the variable declaration statement. In second third lines, values are assigned to the variables height and width. fourth line of code describes the arithmetic expression for calculating the area of the square and the result is assigned to the variable area of the square and the result is assigned to the variable area. Fifth and the last line of code is the print f() statement, which displays result on the screen. In case of printf, the first parameter is always a string (e.g., "Area of Square"), which should be enclosed in double quotes. this string is called the format string. Format string may include any number of format specifiers such as %d. The last line of variables separated by commas, whose values are to be displayed in the result, will follow the format string.

Format Specifier:

                 Format specifiers specify the format in which the value of a variable should be displayed on the screen, Forma specifiers are specified in the format string.
For instance, in the above program the print f() statement contains the symbol %d, which is format specifier for the variable area. For different types of variables, different format specifiers are used. 

        Symbol                          Data Type
           %d                              int, short    
           %f                               float
           %If                             double
           %e                              float,double(Exponential Notation)
           %g                              Floating point(%f or %e, whichever is shorter)    
           %c                              Char
           %s                              Character string
           %u                              unsigned short, unsigned int
           %x                              Unsigned hexadecimal integers
           %o                              Unsigned octal integer
           %i                               Integers
           %Id                            long integer

Field-Width Specifier

In a C program, the number of columns used to display a value on the screen is referred to as field-width. Field-width specifiers describe the number of columns that should be used to print a value.






Comments

Popular posts from this blog

Data Base Design Process

Data Base

VLOOKUP