standard C lib:
int fputc(int c , FILE *stream);
And such behaviors occured many times, e.g:
int putc(int c, FILE *stream);
int putchar(int c);
why not use CHAR as it really is? If use INT is necessary, when should I use INT instead of CHAR?
The fgetc function gets the next character converted to an int , and uses a special marker value EOF to indicate the end of the stream. To do that, they needed the wider int type since a char isn't quite large enough to hold all possible characters plus one more thing.
Description. The fputc() function converts c to an unsigned char and then writes c to the output stream at the current position and advances the file position appropriately. If the stream is opened with one of the append modes, the character is appended to the end of the stream.
In line 7, a structure pointer variable fp of type struct FILE is declared. In line 8, fopen() function is called with two arguments namely "myfile.
The function fgetc() is used to read the character from the file. It returns the character pointed by file pointer, if successful otherwise, returns EOF. Here is the syntax of fgetc() in C language, int fgetc(FILE *stream)
Most likely (in my opinion, since much of the rationale behind early C is lost in the depths of time), it it was simply to mirror the types used in the fgetc
type functions which must be able to return any real character plus the EOF
special character. The fgetc
function gets the next character converted to an int
, and uses a special marker value EOF
to indicate the end of the stream.
To do that, they needed the wider int
type since a char
isn't quite large enough to hold all possible characters plus one more thing.
And, since the developers of C seemed to prefer a rather minimalist approach to code, it makes sense that they would use the same type, to allow for code such as:
filecopy(ifp, ofp)
FILE *ifp;
FILE *ofp;
{
int c;
while ((c = fgetc (ifp)) != EOF)
fputc (c, ofp);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With