FPUTC()
this function is used to write a single character to a file or output stream.
int fputc(int character, FILE *stream);
* character: The character to write (as an int, typically a char)
* stream: Pointer to a FILE object (e.g., stdout, a file pointer from fopen())
* Writes the character to the given output stream.
* Returns the character written (as unsigned char cast to int) on success.
* Returns EOF on failure.
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
perror("File opening failed");
return 1;
}
fputc('H', fp);
fputc('i', fp);
fputc('\n', fp);
fclose(fp);
return 0;
}
* COMMON USE CASE
char *text = "Hello, world!";
for (int i = 0; text[i] != '\0'; i++) {
fputc(text[i], stdout); // prints to console one character at a time
}
Last updated