Pointer to the file stream from which a character is to be read
Read more about parameters of getc in parameters section
The getcfunction in C language reads the next character from the specified stream.
The getc function obtains the next character (if present) as an unsigned char converted to an int, from the input stream pointed to by argument, and advances the associated file position indicator for the stream (if defined). getc is pretty similar to fgetc except the fact that it may be implemented as a macro. Repeated calls to getc read consecutive characters from the stream until EOF or an error occurs. getc function can be used with any input stream, not just files. This includes standard input (stdin) and other streams like those associated with pipes, sockets, or other I/O sources, as long as they are represented by a FILE* pointer. This function is often used with loops in C, especially when you need to read multiple characters from a stream (like a file or standard input) until the end of the stream is reached. This is because getc reads only one character at a time, so a loop is necessary to process an entire stream.
To Summarize the Workflow:
1.
Function receives a pointer to a FILE stream (getc(FILE *stream))
2.
Attempts to read the next character from the provided stream
3.
On success: Reads character, converts to unsigned char, returns as integer (0-255)
4.
On EOF (end of file): Returns EOF (-1) and sets EOF indicator for stream
5.
On read error: Returns EOF (-1) and sets error indicator for stream
6.
Stream position advances one character on success, remains unchanged on failure
Read more about return type and value ofgetc function in return section.
The getcfunction takes 1
parameter:
•
FILE * `stream`: Pointer to the file stream from which a character is to be read
The getc function return value :
On success, getc returns the next character from the specified stream as an unsigned char cast to an int
On failure or end-of-file (EOF), it returns EOF
To distinguish between an error and EOF, you can use the feof or ferror functions
Output
This example demonstrates basic usage of getc to read characters from a file and print them to the console. It also shows how to check for errors and end-of-file conditions.