C <stdion.h> library


The <stdio.h> header file in C is part of the C Standard Library and provides functionalities for input and output operations. It defines the macros, types, and functions needed for performing tasks such as reading from and writing to standard input and output streams, as well as handling files.

Key Features of <stdio.h>

  1. Standard Input/Output: Functions for reading from and writing to the console (standard input/output).
  2. File Handling: Functions for opening, closing, reading, and writing to files.
  3. Formatted Input/Output: Functions for formatted data input and output.

Commonly Used Functions in <stdio.h>

Here are some of the most commonly used functions provided by <stdio.h>:

1. Input and Output Functions

  • printf(): Used to print formatted output to the standard output (usually the console).

    Syntax:

    int printf(const char *format, ...);

    Example:

    printf("Hello, World!\n"); // Outputs: Hello, World!
  • scanf(): Used to read formatted input from the standard input.

    Syntax:

    int scanf(const char *format, ...);

    Example:

    int age; scanf("%d", &age); // Reads an integer from input

2. File Handling Functions

  • fopen(): Opens a file and returns a file pointer.

    Syntax:

    FILE *fopen(const char *filename, const char *mode);

    Example:

    FILE *file = fopen("example.txt", "r"); // Opens a file for reading
  • fclose(): Closes an open file.

    Syntax:

    int fclose(FILE *stream);

    Example:

    fclose(file); // Closes the file
  • fread(): Reads data from a file.

    Syntax:

    size_t fread(void *ptr, size_t size, size_t count, FILE *stream);

    Example:

    int buffer[10]; fread(buffer, sizeof(int), 10, file); // Reads 10 integers from file
  • fwrite(): Writes data to a file.

    Syntax:

    size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);

    Example:

    fwrite(buffer, sizeof(int), 10, file); // Writes 10 integers to file
  • fprintf(): Writes formatted output to a file.

    Syntax:

    int fprintf(FILE *stream, const char *format, ...);

    Example:

    fprintf(file, "Age: %d\n", age); // Writes formatted string to the file
  • fscanf(): Reads formatted input from a file.

    Syntax:

    int fscanf(FILE *stream, const char *format, ...);

    Example:

    fscanf(file, "%d", &age); // Reads an integer from the file

3. File Positioning Functions

  • fseek(): Moves the file position indicator to a specific location in the file.

    Syntax:

    int fseek(FILE *stream, long offset, int whence);

    Example:

    fseek(file, 0, SEEK_SET); // Move to the beginning of the file
  • ftell(): Returns the current position of the file position indicator.

    Syntax:

    long ftell(FILE *stream);

    Example:

    long position = ftell(file); // Get the current position in the file
  • rewind(): Sets the file position indicator to the beginning of the file.

    Syntax:

    void rewind(FILE *stream);

    Example:

    rewind(file); // Move back to the beginning of the file

Standard Streams

<stdio.h> defines three standard streams:

  • stdin: Standard input stream (typically the keyboard).
  • stdout: Standard output stream (typically the console).
  • stderr: Standard error stream (used for error messages).

Error Handling

When working with file operations, it's essential to check for errors. The fopen() function returns NULL if the file cannot be opened, and functions like fread(), fwrite(), and fprintf() return a value less than the number of items requested when an error occurs.

Example of Using <stdio.h>

Here’s a simple example that demonstrates various functionalities of <stdio.h>:

#include <stdio.h> int main() { FILE *file; int age; // Open a file for writing file = fopen("example.txt", "w"); if (file == NULL) { perror("Error opening file"); return 1; } // Write to the file printf("Enter your age: "); scanf("%d", &age); // Read age from standard input fprintf(file, "Age: %d\n", age); // Write age to the file // Close the file fclose(file); // Open the file for reading file = fopen("example.txt", "r"); if (file == NULL) { perror("Error opening file"); return 1; } // Read from the file and print to standard output char buffer[100]; while (fgets(buffer, sizeof(buffer), file) != NULL) { printf("%s", buffer); // Print each line from the file } // Close the file fclose(file); return 0; }

Summary

  • <stdio.h> is essential for handling input and output in C.
  • It provides functions for formatted I/O, file handling, and error reporting.
  • Understanding <stdio.h> is crucial for performing basic I/O operations in C programming.