Tuesday 12 February 2013

Character - Based Input - Output Functions - C Programming Language


The following functions accepts a single character from the keyboard and displays it on the standard output device:

#include <stdio.h>

main()
{
char alph,
alph=getc(stdin);
fflush(stdin);
putc(alph,stdout);
}

The function main() first defines a memory location, alph, of char type, and then invokes the functions getc() and putc().

The function getc() accepts as parameter the reference of device, and allows one character to be read from this device during program execution. In this case the device is stdin. The character read is returned and assigned to alph. The function putc() accepts as parameters the character to be output, and the reference of the output device to which one character is to be output. The function fflush() clears the buffer associated with a specified input/output device. It is a good programming practice to clear the buffer even if there are no subsequent input operations in a function.

The #include statement includes the specified file as part of a function at the time of compilation. Hence the contents of the included file are compiled along with the function being compiled.

The statement #include <stdio.h> includes at the time of compilation, the contents of the standard input-output file, stdio.h which contains definitions of stdin, stdout and stderr. Whenever the references stdin, stdout and stderr are used in a function, this statement has to be used. The file stdio.h exists in the derectory /usr/include.

0 comments:

Post a Comment

Powered by Blogger.