Explain putc() and getc() functions of files in C language

The putc() and getc() functions in C are used for character-based file input/output operations. These functions allow you to read and write single characters to files, making them useful for processing text files character by character.

Syntax

int putc(int ch, FILE *fp);
int getc(FILE *fp);

The putc() Function

The putc() function writes a character to a file. It takes two parameters − the character to write and a file pointer. It returns the written character on success or EOF on failure.

The getc() Function

The getc() function reads a character from a file and returns it as an integer. It advances the file position indicator and returns EOF when the end of file is reached.

File on Disk C Program Console Output getc() putchar() putc() File I/O with putc() and getc()

Example 1: Writing and Reading Characters

This example demonstrates writing characters to a file using putc() and reading them back with getc() −

#include <stdio.h>

int main() {
    FILE *fp;
    char ch;
    
    /* Writing to file */
    fp = fopen("sample.txt", "w");
    if (fp == NULL) {
        printf("Error opening file for writing<br>");
        return 1;
    }
    
    printf("Writing 'Hello' to file...<br>");
    putc('H', fp);
    putc('e', fp);
    putc('l', fp);
    putc('l', fp);
    putc('o', fp);
    putc('<br>', fp);
    fclose(fp);
    
    /* Reading from file */
    fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        printf("Error opening file for reading<br>");
        return 1;
    }
    
    printf("Reading from file:<br>");
    while ((ch = getc(fp)) != EOF) {
        putchar(ch);
    }
    fclose(fp);
    
    return 0;
}
Writing 'Hello' to file...
Reading from file:
Hello

Example 2: Character Count in File

This example counts the number of characters in a file using getc() −

#include <stdio.h>

int main() {
    FILE *fp;
    int ch, count = 0;
    
    /* Create a sample file first */
    fp = fopen("count.txt", "w");
    if (fp == NULL) {
        printf("Error creating file<br>");
        return 1;
    }
    
    /* Write some text */
    char text[] = "TutorialsPoint C Programming";
    for (int i = 0; text[i] != '\0'; i++) {
        putc(text[i], fp);
    }
    fclose(fp);
    
    /* Now count characters */
    fp = fopen("count.txt", "r");
    if (fp == NULL) {
        printf("Error opening file<br>");
        return 1;
    }
    
    printf("Content: ");
    while ((ch = getc(fp)) != EOF) {
        putchar(ch);
        count++;
    }
    
    printf("\nTotal characters: %d<br>", count);
    fclose(fp);
    
    return 0;
}
Content: TutorialsPoint C Programming
Total characters: 28

Key Points

  • Return Values: Both functions return EOF (-1) on error or end of file.
  • File Modes: Use "r" for reading with getc() and "w" or "a" for writing with putc().
  • Error Handling: Always check if file operations succeed before proceeding.
  • Character Type: Both functions work with int type to accommodate EOF value.

Conclusion

The putc() and getc() functions provide efficient character-level file I/O operations in C. They are essential for text processing applications that need to handle files one character at a time.

Updated on: 2026-03-15T13:53:19+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements