How can you read a character from keyboard and writing a character to the screen?

we can use getchar function to read a single character from the keyboard. We will try to take one sample character and display by using printf function. Once the program encounters the getchar() function it will wait for the user to enter data by keyboard. Once it is entered it will be stored in a variable and we can display by using printf function.

#include <stdio.h>
int main(void){
char var;
var = getchar();

printf(” Welcome to %c”,var);
return 0;
}

Using getchar function we can interact with user and execute some conditional statements. WE will ask user if C programming is easy or difficult. Based on the input data we will display a message using one if condition checking.

#include <stdio.h>
int main(void){
char var;
printf( “Do you like C programming ? Press Y or N “);
var = getchar();
if(var == ‘Y’)
{
printf(” Yes C is very interesting language”);
}
else
{
printf (” Why ? It is easy to learn here, need practice only”);
}

return 0;
}

Leave a comment