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.


int putc(int char, FILE *stream)


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);


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.

C Programming: Tips of the Day

Reading a string with scanf :

An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.

Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.

Ref : https://bit.ly/3pdEk6f