← GCC/C Index

📄 <stdio.h> — Standard Input / Output

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

📋 Functions & Macros

NamePrototype / ValueDescription
printfint printf(const char *fmt, ...)Print formatted output to stdout
scanfint scanf(const char *fmt, ...)Read formatted input from stdin
fprintfint fprintf(FILE *stream, const char *fmt, ...)Print formatted output to a FILE stream
fscanfint fscanf(FILE *stream, const char *fmt, ...)Read formatted input from a FILE stream
sprintfint sprintf(char *buf, const char *fmt, ...)Write formatted output into a string buffer
snprintfint snprintf(char *buf, size_t n, const char *fmt, ...)Bounded version of sprintf
sscanfint sscanf(const char *str, const char *fmt, ...)Read formatted data from a string
fopenFILE *fopen(const char *path, const char *mode)Open a file; returns FILE pointer or NULL
fcloseint fclose(FILE *fp)Close a FILE stream
freadsize_t fread(void *ptr, size_t size, size_t n, FILE *fp)Read binary data from a file
fwritesize_t fwrite(const void *ptr, size_t size, size_t n, FILE *fp)Write binary data to a file
fgetschar *fgets(char *s, int n, FILE *fp)Read a line from a file stream (safe)
fputsint fputs(const char *s, FILE *fp)Write a string to a file stream
fgetcint fgetc(FILE *fp)Read one character from a stream
fputcint fputc(int c, FILE *fp)Write one character to a stream
getcharint getchar(void)Read one character from stdin
putcharint putchar(int c)Write one character to stdout
putsint puts(const char *s)Write a string + newline to stdout
getschar *gets(char *s)⚠️ Unsafe — use fgets() instead
feofint feof(FILE *fp)Non-zero if end-of-file reached
ferrorint ferror(FILE *fp)Non-zero if a file error occurred
rewindvoid rewind(FILE *fp)Reset file position to beginning
fseekint fseek(FILE *fp, long offset, int whence)Move file position pointer
ftelllong ftell(FILE *fp)Return current file position
removeint remove(const char *path)Delete a file
renameint rename(const char *old, const char *new)Rename / move a file
tmpfileFILE *tmpfile(void)Create a temporary binary file
perrorvoid perror(const char *s)Print error message based on errno

💡 Example Program

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 */

🏠 Index  |  <math.h> →