c语言getchar函数
C语言中的getchar()函数 (getchar() function in C)
The getchar() function is defined in the <stdio.h> header file.
getchar()函数在<stdio.h>头文件中定义。
Prototype:
原型:
int getchar(void);
Parameters: FILE *filename(for file handling), otherwise it will be void.
参数: FILE * filename(用于文件处理) ,否则为空。
Return type: int
返回类型: int
Use of function:
使用功能:
In the file handling, through the getchar() function we take the character from the input stream stdin. The prototype of the function getchar() is int getchar(void);
在文件处理中,通过getchar()函数,我们从输入流stdin中获取字符。 函数getchar()的原型是int getchar(void);。
The character which is read is an unsigned char which is converted to an integer value. In the case of file handling, it returns EOF when end-of-file is encountered. If there is an error then it also returns EOF.
读取的字符是无符号字符 ,该字符将转换为整数值。 在文件处理的情况下,遇到文件结尾时它将返回EOF 。 如果有错误,则它还会返回EOF 。
C语言中的getchar()示例 (getchar() example in C)
#include <stdio.h>
#include <stdlib.h>
int main(){
//Initialize the character array
char str[100];
int i=0,j=0;
printf("Enter the string into the file\n");
//takes all the characters until enter is pressed
while((str[i]=getchar())!='\n'){
//increment the index of the character array
i++;
}
//after taking all the character add null pointer
//at the end of the string
str[i]='\0';
printf("\nThe file content is - ");
//loop is break when null pointer is encountered
while(str[j]!='\0'){
//print the characters
putchar(str[j]);
j++;
}
return 0;
}
Output
输出量
翻译自: https://www.includehelp.com/c-programs/getchar-function-in-c-language-with-example.aspx
c语言getchar函数