Detailed Explanation and Examples of Input and Output Stream Operations in C Language

Detailed Explanation and Examples of Input and Output Stream Operations in C Language

In C language, input and output (I/O) is the primary way for programs to interact with the external environment. Through the standard input-output library <span><stdio.h></span>, we can easily read and write data. This article will provide a detailed introduction to input and output stream operations in C language, including basic concepts, commonly used functions, and their usage examples.

1. Standard Input and Output Streams

C language provides three standard streams:

  1. Standard Input (stdin): Used to receive user input.
  2. Standard Output (stdout): Used to display information to the user.
  3. Standard Error (stderr): Used to display error messages.

These streams are automatically opened by the system, and we can directly use them for data processing.

2. Common I/O Functions

1. <span>printf()</span>

<span>printf()</span> function is used for formatted output to standard output. Its basic syntax is as follows:

int printf(const char *format, ...);
  • <span>format</span> is a format string that can contain text and format specifiers.
  • The return value is the number of characters successfully written.

Example Code:

#include <stdio.h>
int main() {
    int age = 25;
    float height = 175.5;
    printf("I am %d years old, and my height is %.1f cm.\n", age, height);
    return 0;
}

2. <span>scanf()</span>

<span>scanf()</span> function is used to read data from standard input, and its basic syntax is as follows:

int scanf(const char *format, ...);
  • <span>format</span> is a format string used to specify the data type to be read.
  • The return value is the number of variables successfully assigned.

Example Code:

#include <stdio.h>
int main() {
    int age;
    float height;
    printf("Please enter your age: ");
    scanf("%d", &age);
    printf("Please enter your height: ");
    scanf("%f", &height);
    printf("You are %d years old, and your height is %.1f cm.\n", age, height);
    return 0;
}

3. <span>getchar()</span>

<span>getchar()</span> function reads the next character from standard input, and its basic syntax is as follows:

int getchar(void);
  • The return value is the character read; if the end of the file is encountered, it returns EOF.

Example Code:

#include <stdio.h>
int main() {
    char c;
    printf("Please enter a character: ");
    c = getchar(); // Get a single character
    printf("The character you entered is: %c\n", c);
    return 0;
}

4. <span>putchar()</span>

<span>putchar()</span> function writes a character to standard output, and its basic syntax is as follows:

int putchar(int character);
  • The parameter is the character to be written, and the return value is the character itself or EOF to indicate an error.

Example Code:

#include <stdio.h>
int main() {
    char c = 'A';
    printf("Printing letter: ");
    putchar(c); // Output a single character
    putchar('\n'); // New line
    return 0;
}

3. File I/O Operations

In addition to console I/O, C provides support for file operations. We can perform file read and write operations through the following steps:

  1. Open the file: Use the <span>fopen()</span> function.
  2. Operate on the file: Use functions like <span>fprintf()</span> and <span>fscanf()</span>. 3. Close the file: Use the <span>fclose()</span> function.

File Open Modes

Mode Description
“r” Open in read-only mode
“w” Open in write mode, create if it does not exist
“a” Open in append mode, add content at the end
“rb”/”wb”/”ab” Binary mode

Example Code – Writing to a File

#include <stdio.h>
int main() {
    FILE *file = fopen("output.txt", "w"); // Create and open output.txt file for writing
    if (file == NULL) {
        perror("Cannot open file");
        return -1;
    }
    fprintf(file, "Hello, World!\n"); // Write content to the file
    fclose(file); // Close the file
    return 0;
}

Example Code – Reading from a File

#include <stdio.h>
int main() {
    FILE *file = fopen("output.txt", "r"); // Open output.txt file for reading
    if (file == NULL) {
        perror("Cannot open file");
        return -1;
    }
    char buffer[100];
    while (fgets(buffer, sizeof(buffer), file)) { // Read content line by line from the file
        printf("%s", buffer);
    }
    fclose(file); // Close the file
    return 0;
}

4. Conclusion

This article introduced the basic I/O stream operations in C programming language, including how to use common functions like <span>printf</span>, <span>scanf</span>, <span>getchar</span>, and file-related functions to achieve more complex data processing. This knowledge is crucial for beginners to understand the interaction between programs and the external world. In practical development, these skills will help you better handle user interactions and data storage issues. I hope this article can assist you in your learning!

Leave a Comment