C Function Getc
## C Library Function - getc()
The `getc()` function is a standard input/output function in C, defined in the `` header file. It reads the next character (an unsigned char) from a specified input stream and advances the associated file position indicator.
---
## Declaration
The prototype for the `getc()` function is as follows:
```c
int getc(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., `stdin` or a file pointer returned by `fopen`).
---
## 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).
* To distinguish between an actual read error and reaching the end of the file, you should use the `ferror()` and `feof()` functions.
---
## getc() vs. fgetc() vs. getchar()
While these three functions serve similar purposes, they have distinct differences:
| Function | Description | Key Characteristics |
| :--- | :--- | :--- |
| **`getc()`** | Reads a character from a specified stream. | Often implemented as a macro. Because of this, the `stream` argument should not be an expression with side effects (e.g., `getc(*f++)`). |
| **`fgetc()`** | Reads a character from a specified stream. | Guaranteed to be implemented as a function. It is safer than `getc()` when using arguments with side effects, but may have slightly more overhead. |
| **`getchar()`** | Reads a character from standard input (`stdin`). | Equivalent to calling `getc(stdin)`. |
---
## Code Examples
### Example 1: Reading from Standard Input (`stdin`)
The following example demonstrates how to use `getc()` to read a single character entered by the user in the console.
```c
#include
int main()
{
char c;
printf("Please enter a character: ");
// Read a character from standard input
c = getc(stdin);
printf("Character entered: ");
// Output the character to standard output
putc(c, stdout);
printf("\n");
return 0;
}
```
#### Output
```text
Please enter a character: a
Character entered: a
```
---
### Example 2: Reading an Entire File Character by Character
This practical example demonstrates how to open a text file and use `getc()` in a loop to read and print its contents until the End-of-File (`EOF`) is reached.
```c
#include
int main()
{
FILE *file;
int ch;
// Open the file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
perror("Error opening file");
return -1;
}
printf("File contents:\n");
// Read character by character until EOF is reached
while ((ch = getc(file)) != EOF) {
putchar(ch);
}
// Close the file stream
fclose(file);
return 0;
}
```
---
## Important Considerations
1. **Return Type is `int`, Not `char`**:
Always store the return value of `getc()` in an `int` variable (like `int ch`), not a `char`. This is because `EOF` typically has a value of `-1`. If you store the result in a `char`, it might not be able to distinguish between the character with the value `0xFF` (255) and `EOF` on systems where `char` is unsigned.
2. **Macro Side Effects**:
Since `getc()` is highly likely to be implemented as a preprocessor macro, avoid passing arguments with side effects (such as `getc(file_array[i++])`). Use `fgetc()` instead if you need to evaluate expressions inside the argument.
YouTip