Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - getchar()

C Library Function - getchar()

C Standard Library - <stdio.h> C Standard Library -

Description

C Library Function int getchar(void) Get a character (an unsigned char) from standard input stdin. This is equivalent to getc with stdin as the argument.

Declaration

Below is the declaration of the getchar() function.



int getchar(void)

Parameters

Return Value

The function returns the character read as an unsigned char cast to an int, or EOF if the end of the file is reached or a read error occurs.

Example

The following example demonstrates the use of the getchar() function.

Example

#include

int

main

(

)

{

   

char

c

;

 

   

printf

(

"Please enter a character: "

)

;

   c

=

getchar

(

)

;

 

   

printf

(

"Character entered: "

)

;

   

putchar

(

c

)

;

   

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



请输入字符:a

输入的字符:a

C Standard Library - <stdio.h> C Standard Library -