Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library

The C++ <cstdio> putc() function writes a character ch to the output stream stream and advances the position indicator. Internally, the character is converted to unsigned char just before being written.

putc() and fputc() are equivalent, except that putc() may be implemented as a macro in some libraries.


int putc ( int ch, FILE * stream );


ch Specify a character to be written.
stream Specify a pointer to a FILE object that specifies an output stream.

Return Value

On success, the character ch is returned. On failure, returns EOF and sets the error indicator ferror().


In the below example, the file test.txt is opened in write mode.The program creates an empty file for output operations if the file does not exist. If the file already exists, its contents are discarded and the file is treated as a new empty file. Finally it writes ABCDEFGHIJKLMNOPQRSTUVWXYZ in the file before closing it.

#include <cstdio>
int main (){
  //open the file in write mode
  FILE *pFile = fopen("test.txt", "w");
  //writes character in the file
  for(char ch = 'A'; ch <= 'Z'; ch++)
    putc(ch, pFile);

  //close the file
  return 0;

❮ C++ <cstdio> Library