示例代码:
// C program to illustate fgetc() function
#include <stdio.h>
int main ()
{
// open the file
FILE *fp = fopen("test.txt","r");
// Return if could not open file
if (fp == NULL)
return 0;
do
{
// Taking input single character at a time
char c = fgetc(fp); // WHY CAN WE DO THIS?
// Checking for end of file
if (feof(fp))
break ;
printf("%c", c);
} while(1);
fclose(fp);
return(0);
}
根据文档,fgetc()
返回类型为int
的值,那么为什么允许我们将char c
分配给返回值呢?它和ascii值有关吗?