Thursday, July 14, 2011

INPUT/OUTPUT : Programming

INPUT/OUTPUT
Usually i/o, input and output, form an important part of any program. To do anything useful your program needs to be able to accept input data and report back your results. In C, the standard library provides routines for input and output. The standard library has functions for i/o that handle input, output, and character and string manipulation. In this lesson, all the input functions described read from standard input and all the output functions described write to standard output. Standard input is usually the keyboard. Standard output is usually the monitor.

Formatted Output

The standard library function printf is used for formatted output. It takes as arguments a format string and an optional list of variables or literals to output. The variables and literals are output according to the specifications in the format string. Here is the prototype for printf.
int printf(const char *format, arg1, arg2, arg3, ......);
Example: Adding two number
#include
#include
void main()
{
    int a,b,c;
    printf("enter the value for a and b");
    scanf("%d %d", &a, &b);

    c= a + b;

    printf("%d + %d = %d" ,a ,b ,c);
    getch();
}
Here are the more common conversion specifiers.
Specifier
Argument Type
%d
int
%f
float or double
%e
float or double, output in scientific notation.
%c
character
%s
character string (char *)

Formatted Input Output

The standard library function scanf is used for formatted input. It takes as its arguments a format string and a list of pointers to variables to store the input values. Similar to its use in printf, the format string can contain literals and conversion specifiers. In C, to return a value from a function to a calling routine, a pointer to a variable is passed into the function. A pointer stores the memory address of another variable. The methods of passing and returning values from functions are described in Passing Arguments to Functions. Don't worry if you don't understand pointers completely. For now, all this means is a slightly different notation in the call to scanf. In later lessons, pointers and functions will be fully explained. Here is the prototype of scanf and a program illustrating its use.
Scanf returns an integer, either the number of values read in, or EOF if an end of file is reached. EOF is a special termination character, specified in stdio.h, which designates the end of a file. If no values are successfully read, scanf returns 0. To use scanf in a program, the file stdio.h must be included.

WAP to find out simple interest (SI) and Amount (A), given that SI=PRT/100 and A=P+SI.
#include<stdio.h>
#include<conio.h>
void main()
{
   float p,t,r,A,SI;
   printf("Enter value p, t, r ");
   scanf("%f %f %f",&p,&t,&r);

   //calculating simple interest
   SI=(p*t*r)/100;

   //calculating Amount
   A=SI+p;

   printf("Simple interest=%f, Amount=%f", SI, A);
   getch();
}

Unformatted Input / Output

Unformatted functions do not allow the user to read or display data in desired format. These library functions basically deal with a single character or a string of characters. The functions getchar(), putchar(), gets(), puts(), getch(),gerche () , putch() are considered as unformatted functions.

getchar and putchar

getchar reads a single character from standard input.
putchar writes a single character to standard output.
Example:
#include<stdio.h>
void main()
{
    char c;

    c = getchar(); /* to take input form keyboard */
    putchar(c);    /* to display value of 'c' in screen */
}

gets and puts

gets reads a line of input into a character array (String).
puts writes a line of output to standard output .
Example:
#include<stdio.h>
void main()
{
     char str[120]; /* Holds input and output strings */
     gets(str); /* to take string as input form keyboard */
     puts(str); /* to display value of 'str' string in screen */
}

0 comments:

Post a Comment