Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

fputc — C23 stream function

`fputc(c, stream)` from `<stdio.h>` writes the character corresponding to `c` converted to `unsigned char`. On success it returns that value as `unsigned char` converted to `int`; on failure it returns `EOF` and sets the stream error indicator.

Version: C23

Syntax

int fputc(int c, FILE *stream);

fputc(c, stream) from <stdio.h> writes the character corresponding to c converted to unsigned char. On success it returns that value as unsigned char converted to int; on failure it returns EOF and sets the stream error indicator.

The example writes A and a newline through fputc to a tmpfile(), rewinds, and reads both bytes. Output 1 65 10 shows the successful first-write status and the values actually recovered from the stream.

Check the return after every critical write; buffering can defer a later failure until fflush or fclose. An argument outside the unsigned char range is converted first, so this is not a way to write an entire int. In text mode the host can translate the newline representation.

Examples

#include <stdio.h>
int main(void){FILE*s=tmpfile();if(!s)return 1;int ok=fputc('A',s)=='A';if(fputc('\n',s)==EOF){fclose(s);return 1;}rewind(s);int a=fgetc(s),nl=fgetc(s);printf("%d %d %d\n",ok,a,nl);return fclose(s)==0?0:1;}

Result

1 65 10

The example writes `A` and a newline through `fputc` to a `tmpfile()`, rewinds, and reads both bytes. Output `1 65 10` shows the successful first-write status and the values actually recovered from the stream.

Sources