Site Tools


wiki:h-stdio

Table of Contents

<stdio.h>

<stdio.h> provides buffered file and console I/O: printf, scanf, fopen, fclose, and stream operations. Use snprintf instead of sprintf to avoid buffer overflows; always check return values.

Compile with -lm if using math functions; I/O functions link with the C library automatically.

Example

This example opens a file, writes formatted data, seeks, and reads it back.

// compile: gcc -o studioexample studioexample.c
// run: ./studioexample
// description: write to file, seek to start, read back line by line
 
#include <stdio.h>
 
int main() {
    FILE* f = fopen("/tmp/stdio.txt", "w+");
    if (!f) { perror("fopen"); return 1; }
 
    fprintf(f, "line 1\n");
    fprintf(f, "line 2\n");
    fprintf(f, "line 3\n");
 
    rewind(f);
 
    char buf[64];
    while (fgets(buf, sizeof(buf), f))
        printf("read: %s", buf);
 
    fclose(f);
    return 0;
}
wiki/h-stdio.md · Last modified: by 127.0.0.1