Javatpoint Logo
Javatpoint Logo

Getchar() function in C

In this section, we will learn the getchar() function in the C programming language. A getchar() function is a non-standard function whose meaning is already defined in the stdin.h header file to accept a single input from the user. In other words, it is the C library function that gets a single character (unsigned char) from the stdin. However, the getchar() function is similar to the getc() function, but there is a small difference between the getchar() and getc() function of the C programming language. A getchar() reads a single character from standard input, while a getc() reads a single character from any input stream.

Getchar() function in C

Syntax

It does not have any parameters. However, it returns the read characters as an unsigned char in an int, and if there is an error on a file, it returns the EOF at the end of the file.

Now we write several getchar() function programs to accept single characters in C and print them using the putchar () function.

Read a single character using the getchar() function

Let's consider a program to take a single using the getchar() function in C.

Program.c

Output

Enter a character
A
 You have passed A

As we can see in the above program, it takes a single character at the run time from the user using the getchar() function. After getting the character, it prints the letter through the putchar() function.

Read n characters from the user using getchar() function

Let's consider a program to read n characters using the getchar() function in C.

Getchar.c

Output

Enter a character ( If we want to exit.. press #)
A

 We have entered the character: A
 We have entered the character:

B

 We have entered the character: B
 We have entered the character:
C

 We have entered the character: C
 We have entered the character:

As we can see in the above output, a while loop continuously accepts a character from the user until the user does not pass the # character. Here the getchar() function takes a single character from the standard input and assigns them to a ch variable. Whereas the putchar() function prints the read character.

Read a single character using the scanf() function

Let's consider a program to read a character using the scanf() library function in C.

Prog.c

Output

Enter the character
A
 You have entered A

As we can see, when we execute the above program, it takes a single character or group of characters using the scanf() library function instead of the getchar() function. But there is a small difference; a scanf() function can take a single or group of characters from the user, whereas the getchar() function can only accept the single character.

Here we again execute the above program, and this time, it shows the below results.

Read the characters using a do-while loop

Let's consider a program to read the characters using do while and getchar() function in C.

Dowhile1.c

Output

Enter the characters from the keyboard (Press Enter button to stop).
Well b47gvb come
Entered characters are Well b47gvb come

In the above program, a do-while loop continuously accepts the characters until the user passes the ENTER button to exit the loop.


Next Topicflowchart in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA