C - Input-Output Library Functions

The C Input-Output (I/O) library provides functions for performing input and output operations in C programs. These functions are part of the standard C library and are declared in the header file. Here are some commonly used C I/O library functions:

1. printf and fprintf:
  • 'printf': Used to print formatted output to the standard output (usually the console).
  • 'fprintf': Used to print formatted output to a specified file stream.

Example of printf:


#include <stdio.h>

int main() {
    printf("Hello, World!\n"); // Print to standard output
    return 0;
}

Output (console):


Hello, World!

2. scanf and fscanf:
  • 'scanf': Used to read formatted input from the standard input (usually the keyboard).
  • 'fscanf': Used to read formatted input from a specified file stream.

Example of scanf:


#include <stdio.h>

int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num); // Read from standard input
    printf("You entered: %d\n", num);
    return 0;
}

Output (console, user input):


Enter an integer: 42
You entered: 42
3. getchar and fgetc:
  • 'getchar': Used to read a single character from the standard input.
  • 'fgetc': Used to read a single character from a specified file stream.

Example of getchar:


#include <stdio.h>

int main() {
    char ch;
    printf("Enter a character: ");
    ch = getchar(); // Read a character from standard input
    printf("You entered: %c\n", ch);
    return 0;
}

Output (console, user input):


Enter a character: A
You entered: A
4. putchar and fputc:
  • 'putchar': Used to write a single character to the standard output.
  • 'fputc': Used to write a single character to a specified file stream.

Example of putchar:


#include <stdio.h>

int main() {
    char ch = 'B';
    putchar(ch); // Write a character to standard output
    return 0;
}

Output (console):


B
5. gets and fgets:
  • 'gets': Used to read a line of text from the standard input.
  • 'fgets': Used to read a line of text from a specified file stream.

Example of fgets:


#include <stdio.h>

int main() {
    char buffer[100];
    printf("Enter a string: ");
    gets(buffer); // Read a string from standard input
    printf("You entered: %s\n", buffer);
    return 0;
}

Output (console, user input):


Enter a string: Hello, World!
You entered: Hello, World!
6. puts and fputs:
  • 'puts': Used to write a string followed by a newline character to the standard output.
  • 'fputs': Used to write a string to a specified file stream.

Example of puts:


#include <stdio.h>

int main() {
    puts("Hello, World!"); // Write a string with a newline to standard output
    return 0;
}

Output (console):


Hello, World!
7. scanf Format Specifiers:
  • Format specifiers in scanf and related functions specify the type of data to be read and the corresponding variables where the data should be stored. Common format specifiers include %d for integers, %f for floating-point numbers, %s for strings, etc.
8. printf Format Specifiers:
  • Format specifiers in printf and related functions specify how data should be formatted when printed. For example, %d is used to print integers, %f for floating-point numbers, %s for strings, and so on.
9. File Operations:
  • C provides functions like fopen, fclose, fwrite, and fread for file handling. These functions allow you to open files, read from them, write to them, and close them.
10. Error Handling:
  • The perror function is used to print an error message based on the last error that occurred during I/O operations.
  • The errno variable stores the error code for the most recent error.

These examples cover all the key points related to C Input-Output (I/O) library functions, including reading from and writing to standard input/output and files, formatting input and output, and handling characters and strings.