C Standard Library Reference

Syntax Description
#include <stdio.h> standard input and output functions
#include <stdlib.h> conversions & allocations functions
#include <ctype.h> characters testing functions
#include <string.h> string manipulation functions
#include <math.h> mathematical functions
#include <time.h> date & time functions
#include <limits.h> data types range constants
#include <float.h> floating point data types range constants

 


 

stdio.h

 
Syntax Description

FILE  *fopen(const char *name,

             const char *mode)

file open
FILE  *freopen(const char *name,

               const char *mode,

               FILE *fp)

file reopen
int    fclose(FILE *fp) file close
size_t fread(void  *ptr,

             size_t size,

             size_t n,

             FILE  *fp)

file read
size_t fwrite(const void *ptr,

              size_t size,

              size_t n,

              FILE *fp)

file write
int    feof(FILE *fp) check file pointer for end of file
int    fseek(FILE *fp,

             long offset,

             int  mode)

set file position
int    fflush(FILE *fp) file flush
long   ftell(FILE *fp) get file position
void   rewind(FILE *fp) set file position to start of file
int    fgetpos(FILE *fp,

               fpos_t *ptr)

write file position in *ptr
int    fsetpos(FILE *fp,

               const fpos_t *ptr)

set file position from *ptr
int    rename(const char *oldname,

           const char *newname)

rename file
int    remove(const char *name) delete file
FILE  *tmpfile(void) creates temporary binary file - will be deleted after program ends
char  *tmpnam(char *str) get temporary (not opened) filename string
void   setbuf(FILE *fp, char *buf) set stream buffering
int    setvbuf(FILE *fp,

               char *buf,

               int mode, size_t size)

set stream buffering 
int    printf(const char *format,

              ...)

formatted output to stdout
int    sprintf(const char *str,

               const char *format,

               ...)

formatted output to string
int    fprintf(FILE *fp,

               const char *format,

               ...)

formatted output to file
int    scanf(const char *format, ...) formatted input from stdin
int    fscanf(FILE *fp,

              const char *format, ...)

formatted input from file
int    sscanf(const char *str,

              const char *format, ...)

formatted input from string
int    getchar(void) read character from stdin
int    getc(FILE *fp) read character from file stream
int    fgetc(FILE *fp) read character from file stream
int    putchar(int c) write character to stdout
int    putc(int c, FILE *fp) write character from file stream
int    fputc(int c, FILE *fp) write character from file stream
int    ungetc(int c, FILE *fp)  
char  *gets(char *str) read line from stdin
char  *fgets(char *str, int n,

            FILE *fp)

read line from file stream
int    puts(const char *str) write line to stdout
int    fputs(const char *str,

             FILE *fp)

write line to file stream
void   clearerr(FILE *fp) clear file eof and error flags
int    ferror(FILE *fp) check file pointer for file error
void   perror(const char *str) print error message to stderr
   

stdlib.h

 
Syntax Description
void  *malloc(size_t size) allocate memory
void  *realloc(void *ptr, size_t size) reallocate memory
void  *calloc(size_t objnum,

              size_t objnum)

allocate memory of objnum*objnum
void   free(void *ptr) free allocated memory
double atof(const char *str) numeral string conversion to double
int    atoi(const char *str) numeral string conversion to integer
long   atol(const char *str) numeral string conversion to long
double strtod(const char *str,

              char **endptr)

numeral string conversion to double
long   strtol(const char *str,

              char **endptr, int base)

numeral string conversion  to long
unsigned long

       strtoul(const char *str,

               char **endptr,

               int base)

numeral string conversion to unsigned long
void   exit(int status) normal program termination
void   abort(void) abnormal program termination
char  *getenv(const char *name) get environment variable
int    atexit(void (*funcptr)(void)) register function call at program exit
int    system(const char *command) executes system shell command
int    rand(void) random integer in 0..RAND_MAX
void   srand(unsigned int seed) set random sequence seed
int    abs(int x) absolute value: |x|
long   labs(long x) absolute value: |x|
div_t  div(int x, int y) calculates quotient and reminder of x/y
ldiv_t ldiv(long x, long y) calculates quotient and reminder of x/y
void   bsearch(const void *key,

         const void *base,

         size_t nobj,

         size_t size,

         int (*comp)

         (const void *,const void *))

binary search of a sorted array
void   qsort(void *base, size_t nobj,

         size_t size,

         int (*comp)

         (const void *,const void *))

quick sort
   

string.h

 
Syntax Description
void  *memcpy(void *psrc,

              const void *pdest,

              size_t n)

memory copy of n bytes from psrc to pdest
int    memcmp(const void *p1,

              const void *p2,

              size_t n)

memory compare of n bytes in p1 and p2
void  *memmove(void *p1,

               const void *p2,

               size_t n)

memory copy of n bytes from p2 to p1
void  *memset(const void *p,

              int c, size_t n)

memory character set of c in p..p+n-1 addresss range
void  *memchr(const void *p,

              int c, size_t n)

memory character search for c in p..p+n-1 addresss range
size_t strlen(const char *str) string length (without '\0')
char  *strcpy(char *dest,

              const char *src)

copy string src to dest
char  *strncpy(char *dest,

               const char *src,

               size_t n)

copy up to n characters from src to dest
char  *strcat(char *str1,

              const char *str2)

concatenate str1 and str2 and store in str1
char  *strncat(char *str1,

               const char *str2,

               size_t n)

concatenate str1 and up to n characters from str2 and store in str1
int    strcmp(const char *str1,

              const char *str2)

compare str1 and str2 strings
int    strncmp(const char *str1,

               const char *str2,

               size_t n)

compares up to n characters of str1 and str2 strings
char  *strchr(const char *str,

              int c)

look for first char c in str
char  *strrchr(const char *str,

               int c)

look for last char c in str
size_t strspn(const char *str1,

              const char *str2)

find the intersection string of str1 and str2
size_t strcspn(const char *str1,

               const char *str2)

find the not intersection string of str1 and str2
char  *strpbrk(const char *str,

               const char *brk)

search for the first occurrence in str of  any character in brk
char  *strstr(const char *str,

              const char *substr)

search for the first occurrence in str of  string substr
char  *strerror(int errnum) get the error description for the error number
char  *strtok(char *str,

              const char *delimit)

parse tokens from str delimited by delimit
   

ctype.h

 
Syntax Description
int isdigit(int c) is digit
int isalpha(int c) is letter
int isalnum(int c) is letter or digit
int iscntrl(int c) is control character
int isgraph(int c) is any character except space
int isprint(int c) is any character or space
int ispunct(int c) is punctuation character
int isspace(int c) is space (space/tab/CR/newline...)
int isxdigit(int c) is hex digit - a..f or A..F
int islower(int c) is lower case letter
int isupper(int c) is upper case letter
int tolower(int c) convert to lower case
int toupper(int c) convert to upper case
   

math.h

 
Syntax Description
double pow(double x, double y) power:  x y
double sqrt(double x) square root
double log(double x) natural logarithm: ln(x)
double log10(double x) base 10 logarithm: log10x
double exp(double x) exponent:  ex
double ldexp(double x, int exp) return x*2exp
double frexp(double x, int *exp) converts x to frac*2(*exp)
double modf(double x, double *intptr) convert to integer and fraction
double fmod(double x, double y) reminder of x/y
double floor(double x) largest integer that is smaller than x
double ceil(double x) smallest integer that is greater than x
double fabs(double x) float absolute: |x|
double sin(double x) sine of angle [radian]
double cos(double x) cosine of angle [radian]
double tan(double x) tangent of angle [radian]
double asin(double x) arcsine
double acos(double x) arccosine
double atan(double x) arctangent
double atan2(double y, double x) arctangent of y/x
double sinh(double x) hyperbolic sine
double cosh(double x) hyperbolic cosine
double tanh(double x) hyperbolic tangent

 


fopen mode

mode description
"r" open text file for reading
"w" open/create empty text file for writing
"a" open/create text file for writing from the end
"r+" open text file for reading and writing
"w+" open/create empty text file for reading and writing
"a+" open/create empty text file for reading and writing from the end
"rb" open binary file for reading
"wb" open/create empty binary file for writing
"ab" open/create binary file for writing from the end
"r+b" open binary file for reading and writing
"w+b" open/create empty binary file for reading and writing
"a+b" open/create empty binary file for reading and writing from the end

Printf format:

Syntax:

%[flags][width][.percision][length]type

 

Flags (optional):

flag description
- align left
+ print sign
0 extra zeros padding
space print space before number, when no sign
# other format

 

Width (optional):

Minimal output characters width.

 

Percision (optional):

Set the number of digits after the decimal point for number print.

Set the maximum number of characters to be printed for a string print.

 

Length (optional):

length description
h short / unsigned short
l long / unsigned long
L long double

 

Type:

type description variable type print format
c unsigned character unsigned char c
d i signed decimal integer int [-]dddddd
u unsigned decimal integer unsigned int dddddd
x unsigned hexadecimal integer - lower case a..f unsigned int hhhhhh
X unsigned hexadecimal integer - upper case A..F unsigned int HHHHHH
o unsigned octal integer unsigned int oooooo
f floating point number double [-]ddd.ddd
e floating point number double [-]ddd.dddedd
E floating point number double [-]ddd.dddEdd
g minimal length format selection: %f or %e double  
G minimal length format selection: %f or %E double  
p address of pointer void * hhhhhhhh
s print string char *  
n argument get the number of characters written by printf int *  
% print %   %

scanf format

Syntax:

%[*][width][length]type

 

* (optional):

Suppress assignment. don't store the current input.

 

Width (optional):

Minimal output characters width.

 

Length (optional):

length description
h short / unsigned short
l long / unsigned long
L long double

 

Type:

type description variable type scan format
c character char * c
d signed decimal integer int * [-]dddddd
i signed integer (dec/hex/oct) int *  
u unsigned decimal integer unsigned int * dddddd
x unsigned hexadecimal integer int * hhhhhh
o octal integer int * oooooo
f/e/E/g/G floating point number float * [-]ddd.ddd

[-]ddd.dddedd

p address of pointer void * hhhhhhhh
s get string char *  
[...] get string until char not in set char *  
[^...] get string until char in set char *  
n argument get the number of characters read by scanf int *  
% % no assignment occures    

 

 

Write how to improve this page

C REFERENCE
RAPID TABLES