Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

C Library Function – putc()

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In C, the putc() function is used to write a character to a given stream. It is a standard library function defined in the <stdio.h> header file. The function first converts the character to unsigned char, then writes it to the given stream at the position indicated by the file pointer, and finally increments the file pointer by one.

Syntax:

int putc(int ch, FILE *stream);

Parameters:

  • ch – This is the character to be written.
  • stream – This is a pointer to a FILE object that identifies the stream where the character is to be written.

Return Value:

  • If the operation is successful, the function returns the character written.
  • If an error occurs or the end of the file is reached, it returns EOF.

Example 1: In this example, we will write a single character to a file using putc(). See the following code:

C




// C program to demonstrate the putc() function
  
#include <stdio.h>
#include <stdlib.h>
  
int main()
{
    FILE* fp = NULL;
  
    fp = fopen("C:\\Users\\General\\Desktop\\file.txt",
               "w");
  
    if (fp == NULL) {
        printf("The file can't be opened!\n");
        exit(1);
    }
  
    char ch = 'A';
  
    putc(ch, fp);
  
    printf("File has been modified !\n");
  
    fclose(fp);
  
    fp = NULL;
  
    return 0;
}

Output

The file has been modified !

If we open the generated file.txt, it will print the following content:

C




// C program to display the content of above txt file
#include <stdio.h>
  
int main()
{
    FILE* fptr;
    int temp;
  
    fptr = fopen("C:\\Users\\General\\Desktop\\file.txt",
                 "r");
  
    while (1) {
        temp = fgetc(fptr);
        if (feof(fptr)) {
            break;
        }
        printf("%c", temp);
    }
    fclose(fptr);
    return (0);
}

Output

A

Example 2: In this example, we will use a for loop to write all the characters between A to Z to a file using putc(). See the following code:

C




// C program to demonstrate the putc() function
  
#include <stdio.h>
#include <stdlib.h>
  
int main()
{
    FILE* fp = NULL;
  
    fp = fopen("C:\\Users\\General\\Desktop\\file.txt",
               "w");
  
    if (fp == NULL) {
        printf("The file can't be opened!\n");
        exit(1);
    }
  
    for (int ch = 65; ch <= 90; ch++) {
        putc(ch, fp);
    }
    printf("File has been modified !\n");
  
    fclose(fp);
  
    fp = NULL;
  
    return 0;
}

Output

File has been modified !

If we open the generated file.txt, it will print the following content:

C




// C program to display the content of above txt file
#include <stdio.h>
  
int main()
{
    FILE* fptr;
    int temp;
  
    fptr = fopen("C:\\Users\\General\\Desktop\\file.txt",
                 "r");
  
    while (1) {
        temp = fgetc(fptr);
        if (feof(fptr)) {
            break;
        }
        printf("%c", temp);
    }
    fclose(fptr);
    return (0);
}

Output

ABCDEFGHIJKLMNOPQRSTUVWXYZ

My Personal Notes arrow_drop_up
Last Updated : 02 Nov, 2022
Like Article
Save Article
Similar Reads