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.
#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
#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.
#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.
#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()
| Feature | putc() | fputc() |
|---|---|---|
| Header File | <stdio.h> | <stdio.h> |
| Purpose | Writes one character to a stream | Writes one character to a stream |
| Stream | Any output stream | Any output stream |
| Return Value | Written character or EOF | Written character or EOF |
| Implementation | May be implemented as a macro | Always 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.