Embellished sample of the stdio library interface

This library contains various input and output functions.  Almost all C programs use this library.

// File stdio.h.
// Interface of the stdio library.
///////////////////////////////////////////////////////////

#ifndef _STDIO_H
#define _STDIO_H

#define NULL 0
#define EOF  (-1)


///////////////////////////////////////////////////////////
// Section 1 -- Input and output of bytes (i.e., chars)
///////////////////////////////////////////////////////////
                                                            
// The function fgetc reads the next byte (if such exists)
// of file fl. The byte is interpreted as an unsigned char,
// converted to an int, and then returned by the function.
// If fl is at its end (and therefore has no next byte)
// the function returns EOF. Usage: i = fgetc (stdin).

int fgetc (FILE *fl);

                                                            
// This function has the same behavior as fgetc but is
// implemented as a macro. Usage: i = getc (stdin).

int getc (FILE *fl); 

#define getchar ()  getc (stdin)


// This function converts c into a byte and writes the byte
// to the file fl. Returns the byte written (converted into
// an int), or EOF in case of some error. Usage:
// fputc (c, stdout).

int fputc (int c, FILE *fl);


// Same behavior as fputc, but implemented as a macro.
// Usage: putc (c, stdout).

int putc (int c, FILE *fl);

#define putchar (x) putc ((x), stdout)



///////////////////////////////////////////////////////////
// Section 2 -- Input and output of strings
///////////////////////////////////////////////////////////

// Reads a line of text from the file fl. More precisely,
// reads at most n-1 bytes and stores them in the array s.
// (If one of the bytes read is \0, the result is
// unpredictable.) The reading is interrupted if the file
// ends or a byte \n is found; in the latter case, the byte
// \n is stored in the array s. Unless there is an error,
// the function stores a byte \0 in s after the last byte
// read from fl. The function returns s unless the file
// ends or some error occurs, in which case it returns
// NULL. Usage: fgets (s, n, stdin);

char *fgets (char *s, int n, FILE *fl);


// Receives a string str (with a \0 at the end) and saves
// this string to the file fl. The byte \0 is not saved.
// Returns EOF if some error occurs and returns a positive
// integer otherwise. Usage: fputs (s, stdout);

int fputs (char *str, FILE *fl);



///////////////////////////////////////////////////////////
// Section 3 -- Formated input and output
///////////////////////////////////////////////////////////

int printf (char *, ...);
int fprintf (FILE *, char *, ...);
int sprintf (char *, char *, ...);

int scanf (char *, ...);
int fscanf (FILE *, char *, ...);
int sscanf (char *, char *, ...);



///////////////////////////////////////////////////////////
// Section 4 -- Files
///////////////////////////////////////////////////////////

typedef struct {
   int            _cnt;  // available characters in buffer
   unsigned char *_ptr;  // next char from/to in buffer
   unsigned char *_base; // the buffer
   unsigned char  _flag; // the state of the stream
   unsigned char  _file; // UNIX System file descriptor
   . . .
} FILE;


// Usage: fl = fopen ("dir/myfile.txt", "r").

FILE *fopen (char *path, char *mode);


// Usage: fclose (fl).

int fclose (FILE *fl);

#endif