fputc()

Updated: April 19, 2023

Write a character to a stream

Synopsis:

#include <stdio.h>

int fputc( int c, 
           FILE* fp );

Arguments:

c
The character you want to write.
fp
The stream you want to write the character to.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fputc() function writes the character specified by c, cast as (int)(unsigned char), to the stream specified by fp.

Returns:

The character written, cast as (int)(unsigned char), or EOF if an error occurred (errno is set).

Errors:

EAGAIN
The O_NONBLOCK flag is set for the file descriptor underlying fp, and the process would be delayed in the write operation.
EBADF
The file descriptor underlying fp isn't a valid file descriptor that's open for writing.
EFBIG
One of the following:
  • An attempt was made to write a file that exceeds the maximum file size.
  • The file is a regular file, and an attempt was made to write at or beyond the offset maximum associated with the corresponding stream.
EINTR
The write operation was terminated due to the receipt of a signal, and no data was transferred.
EIO
One of the following:
  • A physical I/O error occurred.
  • The process is in a background process group attempting to write to its controlling terminal, and either the process is ignoring or blocking the SIGTTIN signal or the process group is orphaned.
  • (QNX Neutrino extension) The filesystem resides on a removable media device, and the media has been forcibly removed.
ENOMEM
Insufficient space is available.
ENXIO
A request was made of a nonexistent device, or the request was outside the capabilities of the device.
ENOSPC
There was no free space remaining on the device containing the file.
EPIPE
An attempt was made to write to a pipe or FIFO that wasn't open for reading by any process. A SIGPIPE signal is also sent to the thread.

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    FILE *fp;
    int c;

    fp = fopen( "file", "r" );
    if( fp != NULL ) {
        while( (c = fgetc( fp )) != EOF ) {
            fputc( c, stdout );
        }
        fclose( fp );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

ANSI, POSIX 1003.1

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

Caveats:

If c is negative, the value returned by this function isn't equal to c—unless c is -1 and an error occurred :-)