C Library Function - putc()

Last Updated : 24 Jul, 2026

The putc() function writes a single character to the specified output stream. It is a standard library function declared in the <stdio.h> header file.

  • Writes one character to a file or output stream.
  • Advances the file position after writing the character.
  • Returns the written character on success or EOF on failure.

Example: The following program writes a single character to the standard output.

C++
#include <stdio.h>

int main()
{
    putc('A', stdout);
    return 0;
}

Output
A

Explanation: The putc() function writes the character 'A' to the standard output stream (stdout).

Syntax

int putc(int ch, FILE *stream);

Parameters

  • ch: Character to be written.
  • stream: Pointer to the output stream where the character will be written.

Return Value

  • Returns the written character on success.
  • Returns EOF if an error occurs.

How putc() Works

The putc() function writes one character to the specified stream and automatically advances the file position.

  • Converts the character to an unsigned char before writing.
  • Writes the character at the current file position.
  • Moves the file pointer to the next position after writing.

Example: Writing a Single Character to a File

C
#include <stdio.h>

int main()
{
    FILE *fp = fopen("file.txt", "w");

    if (fp == NULL) {
        printf("Unable to open file.\n");
        return 1;
    }

    putc('A', fp);

    fclose(fp);

    return 0;
}

Output

Contents of file.txt

A

Explanation: The program opens file.txt in write mode and writes the character 'A' to it using putc(). After writing, the file is closed.

Example: Writing Multiple Characters to a File

The following example writes all uppercase letters from A to Z into a file.

C++
#include <stdio.h>

int main()
{
    FILE *fp = fopen("file.txt", "w");

    if (fp == NULL) {
        printf("Unable to open file.\n");
        return 1;
    }

    for (char ch = 'A'; ch <= 'Z'; ch++)
        putc(ch, fp);

    fclose(fp);

    return 0;
}

Output

Contents of file.txt

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Explanation: The program repeatedly calls putc() inside a loop to write the characters A through Z into the file.

Example: Writing a String Using putc()

Although putc() writes only one character at a time, it can be used inside a loop to write an entire string.

C++
#include <stdio.h>

int main()
{
    FILE *fp = fopen("file.txt", "w");

    if (fp == NULL) {
        printf("Unable to open file.\n");
        return 1;
    }

    char str[] = "GeeksforGeeks";

    for (int i = 0; str[i] != '\0'; i++)
        putc(str[i], fp);

    fclose(fp);

    return 0;
}

Output

Contents of file.txt

GeeksforGeeks

Explanation: The program writes the string to the file by calling putc() once for each character until the null terminator is reached.

putc() Vs fputc()

Featureputc()fputc()
Header File<stdio.h><stdio.h>
PurposeWrites one character to a streamWrites one character to a stream
StreamAny output streamAny output stream
Return ValueWritten character or EOFWritten character or EOF
ImplementationMay be implemented as a macroAlways implemented as a function

Note: In most applications, putc() and fputc() behave identically. The primary difference is that putc() may be implemented as a macro, whereas fputc() is guaranteed to be a function.

Comment