The <stdio.h> header file is the most commonly used library in the C programming language. It stands for Standard Input Output. It contains the core functions needed to read input from the user (like a keyboard) and print output to the screen or a file.
printf(): Prints formatted data to the standard output (usually the console screen). It is the most heavily used function for displaying text and variables.puts(): Prints a string to the console, automatically appending a newline character (\n) at the end.putchar(): Prints a single character to the console.#include <stdio.h>int main() { printf("Formatted output: %d, %s\n", 100, "Hello"); puts("This prints a string with a newline automatically."); putchar('A'); // Prints a single char return 0; }
scanf(): Reads formatted data from the standard input (usually the keyboard). It requires pointers (using the & address-of operator) to store the retrieved values in variables.gets() / fgets(): Reads a line of text (string) from the user. fgets() is preferred because it prevents buffer overflow vulnerabilities by limiting the number of characters read.getchar(): Reads a single character from the user input.#include <stdio.h>int main() { int age; printf("Enter your age: "); scanf("%d", &age); // Reading an integer printf("You are %d years old.\n", age); return 0; }
<stdio.h> is also responsible for all file handling operations in C:
fopen(): Opens a file.fclose(): Closes a file.fprintf(): Writes formatted data to a file.fscanf(): Reads formatted data from a file.Which function from <stdio.h> is best suited for safely reading a string of text with spaces?