w3resource

C putc() function

C library function - putc()

The putc() function is used to convert character to unsigned character and then writes character to the output stream at the current position.

Syntax:

int putc(int char, FILE *stream)

Parameters:

Name Description Required /Optional
char Character write to the output stream. Required
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream. Required

Return value

  • Upon successful completion, putc() shall return the value it has written.
  • Otherwise, it shall return EOF, the error indicator for the stream shall be set, and errno shall be set to indicate the error.

Example: putc() function

In this example, the contents of a buffer are written to a data stream

#include <stdio.h>
#include <string.h>
 
#define  LEN_STR 80
 
int main(void)
{
   FILE *stream = stdout;
   int i, C;
   char buffer[LEN_STR + 1] = "C Programming.";
 
   for ( i = 0;
        (i < strlen(buffer)) && ((C = putc(buffer[i], stream)) != EOF);
         ++i);
}

Output:

C Programming.

C Programming Code Editor:

Previous C Programming: C getc()
Next C Programming: C putchar()



Follow us on Facebook and Twitter for latest update.