putc()

Write a character to a stream.

  #include <stdio.h>
  
  int putc(int c, FILE *stream);    
Parameter

c

The character to write to a file.

stream

A pointer to a stream.

Remarks

The putc() function outputs c to stream and advances stream's file position indicator.

The putc() works identically to the fputc() function, except that it is written as a macro.

If the file is opened in update mode (+) the file cannot be written to and then read from unless the write operation and read operation are separated by an operation that flushes the stream's buffer. This can be done with the fflush() function or one of the file positioning operations ( fseek(), fsetpos() or rewind()).

putc() returns the character written when successful and return EOF when it fails.

Listing: Example of putc() Usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

static char filename[] = "checkputc";

static char test[] = "flying fish and quail eggs";

int i;

// create a new file for output

if (( f = fopen(filename, "w")) == NULL) {

printf("Can't open %s.\n", filename);

exit(1);

}

// output the test character array

// one character at a time using putc()

for (i = 0; test[i] > 0; i++)

putc(test[i], f);

// close the file

fclose(f);

return 0;

}

Output to file checkputc

flying fish and quail eggs