If you like my posts please leave a comment.

Monday, August 6, 2012

Library Functions: getchar( ), putchar() Functions


Character I/O Functions:

getchar( ):

getchar( ) function is defined in “stdio.h”. It is used to read one character at a time, from the standard input device (usually Keyboard). It takes no parameter, and returns the input character. Syntax: char_variable = getchar();

Example:
char ch;
ch = getchar();

in the above example function getchar() accepts a character and assigns it to the character variable ‘ch’

putchar( ):

the function putchar( ) can be considered as complimentary to the getchar( ) function as it prints single character at a time. putchar( ) function transmits a single character to a standard output device (usually a monitor). This function takes a single argument, which represents a single character to be displayed or to be sent to the standard output device. Syntax: putchar(char_variable);

Example:
/*Program to input a character in lowercase and print it is upper case using getchar and putchar functions */
#include<iostream.h>
#include<stdio.h>
int main()
{
char ch;
cout<<"\n Enter a character in lower case: ";
ch = getchar();
cout<<"\nThe entered character is ";
putchar(ch);
cout<<"\nCharacter in UPPER CASE: ";
putchar(ch - 32);
return 0;
}

1 comment: