YouTip LogoYouTip

C Function Fputc

# C Library Function - fputc() The `fputc()` function is a standard C library function defined in the `` header. It is used to write a single character to a specified output stream and advances the file position indicator forward by one character. --- ## Description The C library function `int fputc(int character, FILE *stream)` writes the character specified by the argument `character` (internally converted to an unsigned char) to the specified output stream and advances the associated file position indicator. --- ## Syntax and Declaration Below is the standard declaration for the `fputc()` function: ```c int fputc(int character, FILE *stream); ``` ### Parameters * **`character`**: This is the character to be written. Although it is passed as an `int` value, the function internally converts it to an `unsigned char` before writing it to the stream. * **`stream`**: This is a pointer to a `FILE` object that identifies the output stream where the character will be written. This can be a file stream or standard output streams like `stdout` or `stderr`. ### Return Value * **On Success**: The function returns the character that was written. * **On Failure**: If a writing error occurs, the function returns `EOF` (End of File) and sets the error indicator for the stream. --- ## Code Examples ### Example 1: Writing Characters to a File The following example demonstrates how to use `fputc()` to write a sequence of ASCII characters (from ASCII value 33 to 100) into a file named `file.txt`. ```c #include int main() { FILE *fp; int ch; // Open file for writing and reading fp = fopen("file.txt", "w+"); if (fp == NULL) { perror("Error opening file"); return -1; } // Write ASCII characters from 33 ('!') to 100 ('d') to the file for(ch = 33; ch <= 100; ch++) { fputc(ch, fp); } // Close the file stream fclose(fp); return 0; } ``` **Output:** After compiling and running the program above, a file named `file.txt` will be created in the current directory with the following content: ```text !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd ``` --- ### Example 2: Reading and Displaying the File Content To verify the contents of the file created in the previous example, you can use the following program to read characters from `file.txt` using `fgetc()` and print them to the standard output: ```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 characters one by one until the end of the file is reached while(1) { c = fgetc(fp); if(feof(fp)) { break; } printf("%c", c); } // Close the file stream fclose(fp); return 0; } ``` **Output:** ```text !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd ``` --- ## Key Considerations 1. **`fputc` vs `putc`**: * `fputc()` is guaranteed to be implemented as a function. * `putc()` is highly similar to `fputc()`, but it may be implemented as a macro in some libraries. This means `putc()` can evaluate its arguments more than once, making `fputc()` a safer choice when passing expressions with side effects as arguments. 2. **Error Handling**: Always check the return value of `fputc()`. If it returns `EOF`, you can use `ferror()` to determine if an actual write error occurred, or check `errno` for system-level error details. 3. **Buffering**: Characters written with `fputc()` are typically buffered. If you need the character to appear immediately on the physical device or file, you may need to call `fflush(stream)`.
← C Function FputsC Function Fgets β†’