C – stdio.h library functions

      All C inbuilt functions which are declared in stdio.h header file are given below. The source code for stdio.h header file is also given below for your reference.


List of inbuilt C functions in stdio.h file:

1 printf() This function is used to print the character, string, float, integer, octal and hexadecimal values onto the output screen
2 scanf() This function is used to read a character, string, numeric data from keyboard.
3 getc() It reads character from file
4 gets() It reads line from keyboard
5 getchar() It reads character from keyboard
6 puts() It writes line to o/p screen
7 putchar() It writes a character to screen
8 clearerr() This function clears the error indicators
9 f open() All file handling functions are defined in stdio.h header file
10 f close() closes an opened file
11 getw() reads an integer from file
12 putw() writes an integer to file
13 f getc() reads a character from file
14 putc() writes a character to file
15 f putc() writes a character to file
16 f gets() reads string from a file, one line at a time
17 f puts() writes string to a file
18 f eof() finds end of file
19 f getchar reads a character from keyboard
20 f getc() reads a character from file
21 f printf() writes formatted data to a file
22 f scanf() reads formatted data from a file
23 f getchar reads a character from keyboard
24 f putchar writes a character from keyboard
25 f seek() moves file pointer position to given location
26 SEEK_SET moves file pointer position to the beginning of the file
27 SEEK_CUR moves file pointer position to given location
28 SEEK_END moves file pointer position to the end of file.
29 f tell() gives current position of file pointer
30 rewind() moves file pointer position to the beginning of the file
31 putc() writes a character to file
32 sprint() writes formatted output to string
33 sscanf() Reads formatted input from a string
34 remove() deletes a file
35 fflush() flushes a file

Source code for stdio.h header file:

      Please find the source code for stdio.h header file below. This code is taken from DevC++ compiler files for your reference.

 * stdio.h
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is a part of the mingw-runtime package.
 * No warranty is given; refer to the file DISCLAIMER within the package.
 * Definitions of types and prototypes of functions for standard input and
 * output.
 * NOTE: The file manipulation functions provided by Microsoft seem to
 * work with either slash (/) or backslash () as the directory separator.

#ifndef _STDIO_H_
#define	_STDIO_H_

/* All the headers include this file. */
#include <_mingw.h>

#ifndef RC_INVOKED
#define __need_size_t
#define __need_NULL
#define __need_wchar_t
#define	__need_wint_t
#include <stddef.h>
#define __need___va_list
#include <stdarg.h>
#endif	/* Not RC_INVOKED */

/* Flags for the iobuf structure  */
#define	_IOREAD	1 /* currently reading */
#define	_IOWRT	2 /* currently writing */
#define	_IORW	0x0080 /* opened as "r+w" */

 * The three standard file pointers provided by the run time library.
 * NOTE: These will go to the bit-bucket silently in GUI applications!
#define	STDIN_FILENO	0

/* Returned by various functions on end of file condition or error. */
#define	EOF	(-1)

 * The maximum length of a file name. You should use GetVolumeInformation
 * instead of this constant. But hey, this works.
 * Also defined in io.h.
#define	FILENAME_MAX	(260)

 * The maximum number of files that may be open at once. I have set this to
 * a conservative number. The actual value may be higher.
#define FOPEN_MAX	(20)

/* After creating this many names, tmpnam and tmpfile return NULL */
#define TMP_MAX	32767
 * Tmpnam, tmpfile and, sometimes, _tempnam try to create
 * temp files in the root directory of the current drive
 * (not in pwd, as suggested by some older MS doc's).
 * Redefining these macros does not effect the CRT functions.
#define _P_tmpdir   "\"
#ifndef __STRICT_ANSI__
#define P_tmpdir _P_tmpdir
#define _wP_tmpdir  L"\"

 * The maximum size of name (including NUL) that will be put in the user
 * supplied buffer caName for tmpnam.
 * Inferred from the size of the static buffer returned by tmpnam
 * when passed a NULL argument. May actually be smaller.
#define L_tmpnam (16)

#define _IOFBF    0x0000  /* full buffered */
#define _IOLBF    0x0040  /* line buffered */
#define _IONBF    0x0004  /* not buffered */

#define _IOMYBUF  0x0008  /* stdio malloc()'d buffer */
#define _IOEOF    0x0010  /* EOF reached on read */
#define _IOERR    0x0020  /* I/O error from system */
#define _IOSTRG   0x0040  /* Strange or no file descriptor */
# define _IOAPPEND 0x0200
 * The buffer size as used by setbuf such that it is equivalent to
 * (void) setvbuf(fileSetBuffer, caBuffer, _IOFBF, BUFSIZ).
#define	BUFSIZ	512

/* Constants for nOrigin indicating the position relative to which fseek
 * sets the file position. Enclosed in ifdefs because io.h could also
 * define them. (Though not anymore since io.h includes this file now.) */
#ifndef	SEEK_SET
#define SEEK_SET	(0)

#ifndef	SEEK_CUR
#define	SEEK_CUR	(1)

#ifndef	SEEK_END
#define SEEK_END	(2)

#ifndef	RC_INVOKED

#ifndef __VALIST
#ifdef __GNUC__
#define __VALIST __gnuc_va_list
#define __VALIST char*
#endif /* defined __VALIST  */

 * The structure underlying the FILE type.
 * Some believe that nobody in their right mind should make use of the
 * internals of this structure. Provided by Pedro A. Aranda Gutiirrez
 * <[email protected]>.
typedef struct _iobuf
	char*	_ptr;
	int	_cnt;
	char*	_base;
	int	_flag;
	int	_file;
	int	_charbuf;
	int	_bufsiz;
	char*	_tmpfname;
#endif	/* Not _FILE_DEFINED */

 * The standard file handles

extern FILE (*_imp___iob)[];	/* A pointer to an array of FILE */

#define _iob	(*_imp___iob)	/* An array of FILE */


__MINGW_IMPORT FILE _iob[];	/* An array of FILE imported from DLL. */


#define stdin	(&_iob[STDIN_FILENO])
#define stdout	(&_iob[STDOUT_FILENO])
#define stderr	(&_iob[STDERR_FILENO])

#ifdef __cplusplus
extern "C" {

 * File Operations
_CRTIMP FILE* __cdecl f open (const char*, const char*);
_CRTIMP FILE* __cdecl	f reopen (const char*, const char*, FILE*);
_CRTIMP int __cdecl	f flush (FILE*);
_CRTIMP int __cdecl	f close (FILE*);
/* MS puts remove & rename (but not wide versions) in io.h  also */
_CRTIMP int __cdecl	remove (const char*);
_CRTIMP int __cdecl	rename (const char*, const char*);
_CRTIMP FILE* __cdecl	tmpfile (void);
_CRTIMP char* __cdecl	tmpnam (char*);

#ifndef __STRICT_ANSI__
_CRTIMP char* __cdecl	_tempnam (const char*, const char*);
_CRTIMP int  __cdecl    _rmtmp(void);

_CRTIMP char* __cdecl	tempnam (const char*, const char*);
_CRTIMP int __cdecl     rmtmp(void);
#endif /* __STRICT_ANSI__ */

_CRTIMP int __cdecl	setvbuf (FILE*, char*, int, size_t);

_CRTIMP void __cdecl	setbuf (FILE*, char*);

 * Formatted Output

_CRTIMP int __cdecl	f printf (FILE*, const char*, ...);
_CRTIMP int __cdecl	printf (const char*, ...);
_CRTIMP int __cdecl	sprintf (char*, const char*, ...);
_CRTIMP int __cdecl	_snprintf (char*, size_t, const char*, ...);
_CRTIMP int __cdecl	vfprintf (FILE*, const char*, __VALIST);
_CRTIMP int __cdecl	vprintf (const char*, __VALIST);
_CRTIMP int __cdecl	vsprintf (char*, const char*, __VALIST);
_CRTIMP int __cdecl	_vsnprintf (char*, size_t, const char*, __VALIST);

#ifndef __NO_ISOCEXT  /* externs in libmingwex.a */
int __cdecl snprintf(char* s, size_t n, const char*  format, ...);
__CRT_INLINE int __cdecl
vsnprintf (char* s, size_t n, const char* format, __VALIST arg)
  { return _vsnprintf ( s, n, format, arg); }
int __cdecl vscanf (const char * __restrict__, __VALIST);
int __cdecl vf scanf (FILE * __restrict__, const char * __restrict__,
int __cdecl vsscanf (const char * __restrict__,
		     const char * __restrict__, __VALIST);

 * Formatted Input

_CRTIMP int __cdecl	f scanf (FILE*, const char*, ...);
_CRTIMP int __cdecl	scanf (const char*, ...);
_CRTIMP int __cdecl	sscanf (const char*, const char*, ...);
 * Character Input and Output Functions

_CRTIMP int __cdecl	f getc (FILE*);
_CRTIMP char* __cdecl	f gets (char*, int, FILE*);
_CRTIMP int __cdecl	f putc (int, FILE*);
_CRTIMP int __cdecl	f puts (const char*, FILE*);
_CRTIMP char* __cdecl	gets (char*);
_CRTIMP int __cdecl	puts (const char*);
_CRTIMP int __cdecl	ungetc (int, FILE*);

/* Traditionally, getc and putc are defined as macros. but the
   standard doesn't say that they must be macros.
   We use inline functions here to allow the fast versions
   to be used in C++ with namespace qualification, eg., ::getc.

   _filbuf and _flsbuf  are not thread-safe. */
_CRTIMP int __cdecl	_filbuf (FILE*);
_CRTIMP int __cdecl	_flsbuf (int, FILE*);

#if !defined _MT

__CRT_INLINE int __cdecl getc (FILE* __F)
  return (--__F->_cnt >= 0)
    ?  (int) (unsigned char) *__F->_ptr++
    : _filbuf (__F);

__CRT_INLINE int __cdecl putc (int __c, FILE* __F)
  return (--__F->_cnt >= 0)
    ?  (int) (unsigned char) (*__F->_ptr++ = (char)__c)
    :  _flsbuf (__c, __F);

__CRT_INLINE int __cdecl getchar (void)
  return (--stdin->_cnt >= 0)
    ?  (int) (unsigned char) *stdin->_ptr++
    : _filbuf (stdin);

__CRT_INLINE int __cdecl putchar(int __c)
  return (--stdout->_cnt >= 0)
    ?  (int) (unsigned char) (*stdout->_ptr++ = (char)__c)
    :  _flsbuf (__c, stdout);}

#else  /* Use library functions.  */

_CRTIMP int __cdecl	getc (FILE*);
_CRTIMP int __cdecl	putc (int, FILE*);
_CRTIMP int __cdecl	getchar (void);
_CRTIMP int __cdecl	putchar (int);


 * Direct Input and Output Functions

_CRTIMP size_t __cdecl	f read (void*, size_t, size_t, FILE*);
_CRTIMP size_t __cdecl	f write (const void*, size_t, size_t, FILE*);

 * File Positioning Functions

_CRTIMP int __cdecl	f seek (FILE*, long, int);
_CRTIMP long __cdecl	f tell (FILE*);
_CRTIMP void __cdecl	rewind (FILE*);

#ifdef __USE_MINGW_FSEEK  /* These are in libmingwex.a */
 * Workaround for limitations on win9x where a file contents are
 * not zero'd out if you seek past the end and then write.

int __cdecl __mingw_f seek (FILE *, long, int);
int __cdecl __mingw_f write (const void*, size_t, size_t, FILE*);
#define f seek(fp, offset, whence)  __mingw_f seek(fp, offset, whence)
#define f write(buffer, size, count, fp)  __mingw_f write(buffer, size, count, fp)
#endif /* __USE_MINGW_FSEEK */

 * An opaque data type used for storing file positions... The contents of
 * this type are unknown, but we (the compiler) need to know the size
 * because the programmer using f getpos and f setpos will be setting aside
 * storage for f pos_t structres. Actually I tested using a byte array and
 * it is fairly evident that the fpos_t type is a long (in CRTDLL.DLL).
 * Perhaps an unsigned long? TODO? It's definitely a 64-bit number in
 * MSVCRT however, and for now `long long' will do.
#ifdef __MSVCRT__
typedef long long f pos_t;
typedef long	f pos_t;

_CRTIMP int __cdecl	f getpos	(FILE*, fpos_t*);
_CRTIMP int __cdecl	f setpos (FILE*, const fpos_t*);

 * Error Functions

_CRTIMP int __cdecl	f eof (FILE*);
_CRTIMP int __cdecl	f error (FILE*);

#ifdef __cplusplus
inline int __cdecl f eof (FILE* __F)
  { return __F->_flag & _IOEOF; }
inline int __cdecl f error (FILE* __F)
  { return __F->_flag & _IOERR; }
#define f eof(__F)     ((__F)->_flag & _IOEOF)
#define f error(__F)   ((__F)->_flag & _IOERR)

_CRTIMP void __cdecl	clearerr (FILE*);
_CRTIMP void __cdecl	perror (const char*);

#ifndef __STRICT_ANSI__
 * Pipes
_CRTIMP FILE* __cdecl	_popen (const char*, const char*);
_CRTIMP int __cdecl	_pclose (FILE*);

_CRTIMP FILE* __cdecl	popen (const char*, const char*);
_CRTIMP int __cdecl	pclose (FILE*);

 * Other Non ANSI functions
_CRTIMP int __cdecl	_flushall (void);
_CRTIMP int __cdecl	_f getchar (void);
_CRTIMP int __cdecl	_f putchar (int);
_CRTIMP FILE* __cdecl	_f dopen (int, const char*);
_CRTIMP int __cdecl	_fileno (FILE*);
_CRTIMP int __cdecl	_f closeall(void);
_CRTIMP FILE* __cdecl	_f sopen(const char*, const char*, int);
#ifdef __MSVCRT__
_CRTIMP int __cdecl	_getmaxstdio(void);
_CRTIMP int __cdecl	_setmaxstdio(int);

#ifndef _NO_OLDNAMES
_CRTIMP int __cdecl	f getchar (void);
_CRTIMP int __cdecl	f putchar (int);
_CRTIMP FILE* __cdecl	f dopen (int, const char*);
_CRTIMP int __cdecl	fileno (FILE*);
#endif	/* Not _NO_OLDNAMES */

#define _fileno(__F) ((__F)->_file)
#ifndef _NO_OLDNAMES
#define fileno(__F) ((__F)->_file)

#if defined (__MSVCRT__) && !defined (__NO_MINGW_LFS)
#include <sys/types.h>
__CRT_INLINE FILE* __cdecl fopen64 (const char* filename, const char* mode)
  return f open (filename, mode); 

int __cdecl f seeko64 (FILE*, off64_t, int);

int __cdecl __mingw_f seeko64 (FILE *, off64_t, int);
#define f seeko64(fp, offset, whence)  __mingw_fseeko64(fp, offset, whence)

__CRT_INLINE off64_t __cdecl f tello64 (FILE * stream)
  f pos_t pos;
  if (f getpos(stream, &pos))
    return  -1LL;
   return ((off64_t) pos);
#endif /* __NO_MINGW_LFS */

#endif	/* Not __STRICT_ANSI__ */

/* Wide  versions */

/*  also in wchar.h - keep in sync */
_CRTIMP int __cdecl	f wprintf (FILE*, const wchar_t*, ...);
_CRTIMP int __cdecl	wprintf (const wchar_t*, ...);
_CRTIMP int __cdecl	swprintf (wchar_t*, const wchar_t*, ...);
_CRTIMP int __cdecl	_snwprintf (wchar_t*, size_t, const wchar_t*, ...);
_CRTIMP int __cdecl	vf wprintf (FILE*, const wchar_t*, __VALIST);
_CRTIMP int __cdecl	vwprintf (const wchar_t*, __VALIST);
_CRTIMP int __cdecl	vswprintf (wchar_t*, const wchar_t*, __VALIST);
_CRTIMP int __cdecl	_vsnwprintf (wchar_t*, size_t, const wchar_t*, __VALIST);
_CRTIMP int __cdecl	f wscanf (FILE*, const wchar_t*, ...);
_CRTIMP int __cdecl	wscanf (const wchar_t*, ...);
_CRTIMP int __cdecl	swscanf (const wchar_t*, const wchar_t*, ...);
_CRTIMP wint_t __cdecl	f getwc (FILE*);
_CRTIMP wint_t __cdecl	f putwc (wchar_t, FILE*);
_CRTIMP wint_t __cdecl	ungetwc (wchar_t, FILE*);

#ifdef __MSVCRT__ 
_CRTIMP wchar_t* __cdecl f getws (wchar_t*, int, FILE*);
_CRTIMP int __cdecl	f putws (const wchar_t*, FILE*);
_CRTIMP wint_t __cdecl	getwc (FILE*);
_CRTIMP wint_t __cdecl	getwchar (void);
_CRTIMP wchar_t* __cdecl _getws (wchar_t*);
_CRTIMP wint_t __cdecl	putwc (wint_t, FILE*);
_CRTIMP int __cdecl	_putws (const wchar_t*);
_CRTIMP wint_t __cdecl	putwchar (wint_t);
_CRTIMP FILE* __cdecl	_wf dopen(int, wchar_t *);
_CRTIMP FILE* __cdecl	_wf open (const wchar_t*, const wchar_t*);
_CRTIMP FILE* __cdecl	_wf reopen (const wchar_t*, const wchar_t*, FILE*);
_CRTIMP FILE* __cdecl	_wf sopen (const wchar_t*, const wchar_t*, int);
_CRTIMP wchar_t* __cdecl _wtmpnam (wchar_t*);
_CRTIMP wchar_t* __cdecl _wtempnam (const wchar_t*, const wchar_t*);
_CRTIMP int __cdecl	_wrename (const wchar_t*, const wchar_t*);
_CRTIMP int __cdecl	_wr emove (const wchar_t*);
_CRTIMP void __cdecl	_wp error (const wchar_t*);
_CRTIMP FILE* __cdecl	_wp open (const wchar_t*, const wchar_t*);
#endif	/* __MSVCRT__ */

#ifndef __NO_ISOCEXT  /* externs in libmingwex.a */
int __cdecl snwprintf (wchar_t* s, size_t n, const wchar_t*  format, ...);
__CRT_INLINE int __cdecl
vsnwprintf (wchar_t* s, size_t n, const wchar_t* format, __VALIST arg)
  { return _vsnwprintf ( s, n, format, arg);}
int __cdecl vwscanf (const wchar_t * __restrict__, __VALIST);
int __cdecl vf wscanf (FILE * __restrict__,
		       const wchar_t * __restrict__, __VALIST);
int __cdecl vswscanf (const wchar_t * __restrict__,
		       const wchar_t * __restrict__, __VALIST);

#endif /* _WSTDIO_DEFINED */

#ifndef __STRICT_ANSI__
#ifdef __MSVCRT__
_CRTIMP FILE* __cdecl	wp open (const wchar_t*, const wchar_t*);
#endif /* not NO_OLDNAMES */
#endif /* MSVCRT runtime */

 * Other Non ANSI wide functions
_CRTIMP wint_t __cdecl	_f getwchar (void);
_CRTIMP wint_t __cdecl	_f putwchar (wint_t);
_CRTIMP int __cdecl	_getw (FILE*);
_CRTIMP int __cdecl	_putw (int, FILE*);

#ifndef _NO_OLDNAMES
_CRTIMP wint_t __cdecl	f getwchar (void);
_CRTIMP wint_t __cdecl	f putwchar (wint_t);
_CRTIMP int __cdecl	getw (FILE*);
_CRTIMP int __cdecl	putw (int, FILE*);
#endif	/* Not _NO_OLDNAMES */

#endif /* __STRICT_ANSI */

#ifdef __cplusplus

#endif	/* Not RC_INVOKED */

#endif /* _STDIO_H_ */