Reading A Character in C lang
Posted by
Ravi Kumar at Friday, September 2, 2011
Share this post:
|
The simplest of all input and output operations is
reading a character from the standard input unit and writing
it to the output unit. Reading a character can be done by the
function 'getchar'.
The getchar take the form:
variable_name=getchar();
variable_name a valid c name that has been declared
as char type. When encountered,the computer waits until a key
is pressed and then assign this character as a value to getchar
function.
Example: char name;
name=getchar();
Program:
/* Reading a character from terminal*/
#include'stdio.h'
main()
{
char answer;
printf(" would u like to know my name?\n ");
printf(" type y for yes and n for no: ");
answer=get char();
if(answer=='y' || answer=='Y')
printf("\n\n my name is busy bee \n ");
else
printf(" \n\n u are good for nothing \n ");
}
Output: would u like to know my name?
type y for yes and n for no:y
my name is busy bee
Along with the getchar() function,there are two
functions which receive a character typed from the keyboard
and tests whether it is a letter or digit and print out a
message accordingly.
They are:
isalpha(character)
isdigit(character)
The function isalpha a value non-zero(true)if the
argument character contains an alphabet otherwise it assumes
zero(false)similar is the case with the function isdigit.
Program:
/*Testing character type*/
#include'stdio.h'
#include'ctype.h'
main()
{
char character;
printf("press any key\n");
character=getchar();
if(isalpha(character)>0)
printf("the character is a letter");
else
if(isdigit(character)>0)
printf("the character is a digit");
else
printf("the character is not alphanumeric");
}
output: press any key
f
the character is a letter
press any key
3
the character is a digit
press any key
*
the character is not alphanumeric
The function in character test include:
function test
1.isalpha(c) - is c an alphabetic character?
2.isalphanum(c) - is c an alphanumeric character?
3.sdigit(c) - is c a digit?
4.islower(c) - is c a lower case letter?
5.isprint(c) - is c a printable character?
6.ispunct(c) - is c a punctuation mark?
7.isspace(c) - is c a white pace character?
8.isupper(c) - is c a uppercase letter?