Wednesday, 27 February 2013

Explain the different types of input and output statements used in C.

C support many input and output statements. It also supports formatted input and output.
Basic Character Input & Output:

getchar() function: It reads one character from the standard input. If there is no more characters available, the special value EOF will be return.
Example:       char c;
                        ….      
                        c=getchar();
putchar() function: It writes one character to the standard output (Monitor).
Example:       char c;
                        c=”A”;
                        putchar(c);


Formatted Input:

Input data can be entered into the computer from a standard input device by means of the standard C library function scanf(). This function can be used to enter any combination of numerical values, single character and strings. The function returns the number of data items that have been entered successfully.
Syntax:  scanf( control string, arg1, arg2,….argn)
The control string consist of control characters, whitespace characters and nor-whitespace characters. The control characters are preceded by a % sign and are listed below.

Control Character
Explanation
%c
A single character
%d
A decimal integer
%i
An integer
%e, %f, %g
A floating-point number
%o
An octal number
%s
A string
%x
A hexadecimal number
%p
A pointer
%n
An integer equal to the number of characters read so far
%u
An unsigned integer
%[]
A set of characters
%%
A percent sign


Example:       int i;
                        float f;
                        char c;
                        char str[10];
                        scanf(“%d %f %c %s”,&I,&f,&c,str);

Formatted Output:

Output data can be written from the computer onto a standard output device using the library function printf(). This function can be used to output any combination of numerical values, single characters and strings. It similar to the input function scanf(), except that its purpose is to display data rather than enter into the computer.
Syntax: printf(control string, arg1, arg2, ….argn)
The control string characters are listed above (same as input control string)
Example: printf(“%d %o %x\n”, 100,100,100);
            Will print : 100  144  64
            Printf(“%c %d %f %e %s %d%%\n”,’3’,4,3.24,66000000,”nine”,8);
Will print : 3  4  3.240000  6.600000e+7  nine 8%