YouTip LogoYouTip

C Function Fgetc

## C Library Function - fgetc() The `fgetc()` function is a standard library function in C (defined in the `` header) used to read a single character from a specified input stream. After reading the character, it automatically advances the internal file position indicator by one character. --- ## Syntax ```c int fgetc(FILE *stream); ``` ### Parameters * **`stream`**: A pointer to a `FILE` object that identifies the input stream from which the character is to be read (e.g., a file pointer returned by `fopen()`, or standard input `stdin`). ### Return Value * **On Success**: Returns the character read as an unsigned `char` cast to an `int`. * **On Failure or EOF**: Returns the constant `EOF` (End-of-File). This occurs when the end of the file is reached or if a read error occurs. > **Note**: The return type is `int` rather than `char`. This is crucial because the function needs to be able to return all possible character values *plus* the special `EOF` value (which is typically defined as `-1`). --- ## Code Example The following example demonstrates how to use `fgetc()` to read a text file character by character and print its contents to the console. ### Example Code ```c #include int main() { FILE *fp; int c; // Open the file in read-only mode fp = fopen("file.txt", "r"); if (fp == NULL) { perror("Error opening file"); return -1; } // Read character by character until the end of the file (EOF) is reached while ((c = fgetc(fp)) != EOF) { printf("%c", c); } // Close the file stream fclose(fp); return 0; } ``` ### Input File (`file.txt`) Suppose we have a text file named `file.txt` in the same directory with the following content: ```text We are in 2014 ``` ### Output When you compile and run the program above, it will produce the following output: ```text We are in 2014 ``` --- ## Key Considerations ### 1. Why `fgetc()` Returns `int` Instead of `char` A common mistake for beginners is storing the return value of `fgetc()` in a variable of type `char`: ```c char c; while ((c = fgetc(fp)) != EOF) { ... } // Incorrect! ``` If `char` is unsigned on your system, it can never equal `EOF` (which is usually `-1`), resulting in an infinite loop. If `char` is signed, a valid character in the file with the value `0xFF` (like `ΓΏ` in Latin-1) might be mistakenly interpreted as `EOF`, terminating the loop prematurely. Always store the return value in an **`int`**. ### 2. Distinguishing Between EOF and Read Errors Because `fgetc()` returns `EOF` both when the end of the file is reached and when a hardware/read error occurs, you should use helper functions to determine the exact cause if necessary: * **`feof(stream)`**: Returns non-zero if the end-of-file indicator has been set. * **`ferror(stream)`**: Returns non-zero if an error occurred during the read operation. ### 3. `fgetc()` vs. `getc()` * `fgetc()` is guaranteed to be implemented as a function. * `getc()` is highly similar but can be implemented as a macro in some libraries. While `getc()` is sometimes faster, it should not be used with arguments that have side effects (e.g., `getc(*fp++)`) because the macro might evaluate the argument more than once. For safe, standard usage, `fgetc()` is preferred.
← C Function FgetsC Function Vsprintf β†’