The most commonly used C header. Provides functions for reading from and writing to files, the console, and strings. Every beginner C program starts with #include <stdio.h>.
Include: #include <stdio.h> | Compile: gcc -Wall -o prog prog.c
| Name | Prototype / Value | Description |
|---|---|---|
| printf | int printf(const char *fmt, ...) | Print formatted output to stdout |
| scanf | int scanf(const char *fmt, ...) | Read formatted input from stdin |
| fprintf | int fprintf(FILE *stream, const char *fmt, ...) | Print formatted output to a FILE stream |
| fscanf | int fscanf(FILE *stream, const char *fmt, ...) | Read formatted input from a FILE stream |
| sprintf | int sprintf(char *buf, const char *fmt, ...) | Write formatted output into a string buffer |
| snprintf | int snprintf(char *buf, size_t n, const char *fmt, ...) | Bounded version of sprintf |
| sscanf | int sscanf(const char *str, const char *fmt, ...) | Read formatted data from a string |
| fopen | FILE *fopen(const char *path, const char *mode) | Open a file; returns FILE pointer or NULL |
| fclose | int fclose(FILE *fp) | Close a FILE stream |
| fread | size_t fread(void *ptr, size_t size, size_t n, FILE *fp) | Read binary data from a file |
| fwrite | size_t fwrite(const void *ptr, size_t size, size_t n, FILE *fp) | Write binary data to a file |
| fgets | char *fgets(char *s, int n, FILE *fp) | Read a line from a file stream (safe) |
| fputs | int fputs(const char *s, FILE *fp) | Write a string to a file stream |
| fgetc | int fgetc(FILE *fp) | Read one character from a stream |
| fputc | int fputc(int c, FILE *fp) | Write one character to a stream |
| getchar | int getchar(void) | Read one character from stdin |
| putchar | int putchar(int c) | Write one character to stdout |
| puts | int puts(const char *s) | Write a string + newline to stdout |
| gets | char *gets(char *s) | ⚠️ Unsafe — use fgets() instead |
| feof | int feof(FILE *fp) | Non-zero if end-of-file reached |
| ferror | int ferror(FILE *fp) | Non-zero if a file error occurred |
| rewind | void rewind(FILE *fp) | Reset file position to beginning |
| fseek | int fseek(FILE *fp, long offset, int whence) | Move file position pointer |
| ftell | long ftell(FILE *fp) | Return current file position |
| remove | int remove(const char *path) | Delete a file |
| rename | int rename(const char *old, const char *new) | Rename / move a file |
| tmpfile | FILE *tmpfile(void) | Create a temporary binary file |
| perror | void perror(const char *s) | Print error message based on errno |
Copy, save as demo.c, and compile with the command shown at the bottom.
#include <stdio.h>
int main(void) {
int age;
float gpa;
char name[64];
printf("Enter your name: ");
scanf("%63s", name);
printf("Enter age and GPA: ");
scanf("%d %f", &age, &gpa);
printf("Hello, %s! Age=%d GPA=%.2f\n", name, age, gpa);
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) { perror("fopen"); return 1; }
fprintf(fp, "Name: %s\nAge: %d\n", name, age);
fclose(fp);
fp = fopen("data.txt", "r");
char line[128];
while (fgets(line, sizeof(line), fp))
printf("%s", line);
fclose(fp);
return 0;
}
/* Compile: gcc -Wall -o demo demo.c */