Enter your email id to get regular update free
Login
Your Email
Pasword

Reading a character in #C (module 14)

In this section we will discuss on reading a character using many inbuilt #C functions like scanf, getchar() etc.

This is the 14th module on learning C with us. Earlier we discussed on the basic introduction and structure of C, preprocessor it's features, intermediate and executable codes. compilation and execution process of a C program. And keywords and identifiers, data types, variables and constants, scope of variables, operators and expressions in C. Type casting in #C.
In this section we will discuss on reading a character using inbuilt #C functions like scanf, getchar, getch,getche.
Reading a character :-
The simplest of all input/output operations is reading a character from the 'standard input unit (usual the keyboard) and writing it to the standard output unit (usually the screen). 
Reading a single character can be done by using the function getchar. This can also be done with the help of the scanf function. 
The getchar takes the following form: 
variable_name = getchar(); 
variable_name is a valid C name that has been declared as char type. When this statement encountered, the computer waits until a key is pressed and then assigns this character as a value to getchar function. Since getchar is used on the right-hand side of an assignment statement the character value of getchar is in turn assigned to the variable name on the left.
For example :
char name;
Input :
#include<stdio.h>
 main()
{ char answer;
  printf("Would you like to know my name?n");
 printf("Type Y for YES and N for NO: ");
 answer = getchar (): /* ..Reading a character... */
 if(answer == 'y' ||answer =='Y')
     printf("\n\nMy name is BUSY BEE n");
 else
    printf("\n\nYou are good for nothing\n");
}
Output :
 Would you like to know my name?  
 Type Y for YES and N for NO: Y
 My name is BUSY BEE
 Would you like to know my name?
 Type Y for YES and N for NO: n
 You are good for nothing 

In next module we will discuss on writing a character. For more information please be updated with us only on Www.newsandstory.
Reading a character
scanf
getch
getchar
getche
input
output
#C
Programming
coding
learning
Dalpat I
Content writer
Total 210 views