Home Contact About

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.

  1. putchar() function:
    The putchar() function is used to write a single character to the standard output. Its syntax is:
    int putchar(int ch);
    Here, ch is the character to be written. The putchar() function returns the written character as
    an unsigned char cast to an int or EOF on error.
    Example:
    #include <stdio.h>
    int main() {
    char ch = 'A';
    putchar(ch);
    return 0;
    }

    Output
    A
  2. putc() function:
    The putc() function is similar to putchar() in that it writes a single character to the output.
    However, putc() also allows you to write to a specific file stream. Its syntax is:
    int putc(int ch, FILE *stream);

    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.

    Example:
    #include <stdio.h>
    int main() {
    char ch = 'B';
    FILE *fptr;
    fptr = fopen("test.txt", "w");
    putc(ch, fptr);
    fclose(fptr);
    return 0;
    }
    In this example, we are writing the character 'B' to the file "test.txt" using the putc() function.
  3. fputc() Function
    The fputc() function is similar to putc() in that it writes a single character to the output. However,
    fputc() takes a file pointer as its first argument rather than a file stream. Its syntax is:
    int fputc(int ch, FILE *stream);
    Here, ch is the character to be written, and stream is the file pointer to which the character is
    written. The fputc() function returns the written character as an unsigned char cast to an int or
    EOF on error.
    Example
    #include <stdio.h>
    int main() {
    char ch = 'C';
    FILE *fptr;
    fptr = fopen("test.txt", "w");
    fputc(ch, fptr);
    fclose(fptr);
    return 0;
    }
    In this example, we are writing the character 'C' to the file "test.txt" using the fputc() function.
  4. fprintf() Function:
    The fprintf() function is used to write a formatted string to a file. It can be used to write characters,
    numbers, and other types of data to a file. Its syntax is:
    int fprintf(FILE *stream, const char *format, ...);
    Here, stream is the file pointer to which the string is written, format is the format string, and ... are the
    optional parameters that are used in the format string. The fprintf() function returns the number of
    characters written to the file or a negative value if an error occurs.
    Example:
    #include >stdio.h> int main() {
    char ch = 'D';
    FILE *fptr;
    fptr = fopen("test.txt", "w");
    fprintf(fptr, "%c", ch);
    fclose(fptr);
    return 0;
    }
    In this example, we are writing the character 'D' to the file "