Standard Library Functions in C

adplus-dvertising
fputc() Function
Previous Home Next

Description

The C library function, The fputc( ) function writes the character char to the specified stream at the current file position and then advances the file position indicator. Even though char is declared to be an int for historical reasons, it is converted by fputc( ) into an unsigned char. Because all character arguments are elevated to integers at the time of the call, you will generally see character values used as arguments. If an integer were used, the high-order byte(s) would simply be discarded.

Declaration

Following is the declaration for fputc() function.

int fputc(int char, FILE *stream)

Parameters

char - This is the character to be written. This is passed as its int promotion.

stream- The stream on which the character char is written.

Return Value

The fputc function On success, the character written is returned. If a writing error occurs, EOF is returned and the error indicator (ferror) is set.

Example

#include <stdio.h>

int main()
{
  int i;
  FILE *my_stream;
  char my_filename[] = "snazzyjazz.txt";

  my_stream = fopen (my_filename, "w");

  putc ('X', my_stream);
  putc (' ', my_stream);
  for (i=1; i<=10; i++)
    {
      putc ('!', my_stream);
    }
  putc ('\n', my_stream);

  /* Close stream; skip error-checking for brevity of example */
  fclose (my_stream);

  return 0;
}
Previous Home Next