In C programming language, writing a character to the standard output or to a file is a common operation. Just like reading a character, there are several ways
to write a character in C, including using functions such as putchar(), putc(), fputc(), and fprintf(). In this article, we will discuss the various methods of
writing a character in C, their syntax, parameters, and usage.
#include <stdio.h>
int main() {
char ch = 'A';
putchar(ch);
return 0;
}
Here, ch is the character to be written, and stream is the file stream to which the character is
written. The putc() function returns the written character as an unsigned char cast to an int or
EOF on error.
#include <stdio.h>
int main() {
char ch = 'B';
FILE *fptr;
fptr = fopen("test.txt", "w");
putc(ch, fptr);
fclose(fptr);
return 0;
}
#include <stdio.h>
int main() {
char ch = 'C';
FILE *fptr;
fptr = fopen("test.txt", "w");
fputc(ch, fptr);
fclose(fptr);
return 0;
}
#include >stdio.h>
int main() {
char ch = 'D';
FILE *fptr;
fptr = fopen("test.txt", "w");
fprintf(fptr, "%c", ch);
fclose(fptr);
return 0;
}