C fputc() Function

The fputc() function in C is a standard library function that writes a character to the specified output stream. It is part of the C standard library (stdio.h) and is commonly used for output operations where characters need to be written to files or standard output.

Table of Contents

  1. Introduction
  2. fputc() Function Syntax
  3. Examples
    • Writing Characters to a File
    • Writing Characters to Standard Output
  4. Real-World Use Case
  5. Conclusion

Introduction

The fputc() function is useful for writing individual characters to an output stream. It is often used in scenarios where you need to output data character by character, such as writing to a file or displaying characters on the screen.

fputc() Function Syntax

The syntax for the fputc() function is as follows:

int fputc(int char, FILE *stream);

Parameters:

  • char: The character to be written. It is passed as an int but internally treated as an unsigned char.
  • stream: A pointer to a FILE object that specifies the output stream.

Returns:

  • The function returns the written character as an unsigned char cast to an int. If an error occurs, EOF is returned.

Examples

Writing Characters to a File

To demonstrate how to use fputc() to write characters to a file, we will write a simple program.

Example

#include <stdio.h>

int main() {
    FILE *file;
    const char *text = "Hello, World!";
    int i = 0;

    // Open the file for writing
    file = fopen("output.txt", "w");
    if (file == NULL) {
        printf("Error: Could not open file for writing.\n");
        return 1;
    }

    // Write characters to the file
    while (text[i] != '\0') {
        if (fputc(text[i], file) == EOF) {
            printf("Error: Could not write character to file.\n");
            fclose(file);
            return 1;
        }
        i++;
    }

    // Close the file
    fclose(file);

    return 0;
}

Output (content of output.txt):

Hello, World!

Writing Characters to Standard Output

This example shows how to use fputc() to write characters to the standard output.

Example

#include <stdio.h>

int main() {
    const char *text = "Hello, World!";
    int i = 0;

    // Write characters to the standard output
    while (text[i] != '\0') {
        if (fputc(text[i], stdout) == EOF) {
            printf("Error: Could not write character to standard output.\n");
            return 1;
        }
        i++;
    }

    // Print a newline for clarity
    fputc('\n', stdout);

    return 0;
}

Output:

Hello, World!

Real-World Use Case

Creating a Simple Logging Function

In real-world applications, the fputc() function can be used to create a simple logging function that writes characters to a log file.

Example

#include <stdio.h>
#include <time.h>

void log_message(const char *message) {
    FILE *logfile = fopen("log.txt", "a");
    if (logfile == NULL) {
        printf("Error: Could not open log file.\n");
        return;
    }

    time_t now = time(NULL);
    fprintf(logfile, "%s: ", ctime(&now));

    int i = 0;
    while (message[i] != '\0') {
        if (fputc(message[i], logfile) == EOF) {
            printf("Error: Could not write character to log file.\n");
            fclose(logfile);
            return;
        }
        i++;
    }

    fputc('\n', logfile);
    fclose(logfile);
}

int main() {
    log_message("Application started.");
    log_message("An event occurred.");

    return 0;
}

Output (in log.txt):

Wed Jul  4 12:34:56 2023: Application started.
Wed Jul  4 12:34:57 2023: An event occurred.

Conclusion

The fputc() function is used for writing individual characters to an output stream in C. It allows you to output data character by character, making it useful for tasks such as writing to files or displaying characters on the screen. By understanding and using this function, you can efficiently manage character-based output operations in your C programs. Always ensure to handle the return value properly to check for any errors during the writing process.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top