Standard Libraries – C

Library is a collection of functionality grouped together – a collection of functions. Libraries are made to serve a purpose of reusing particular functionality in multiple projects.

C language supports:

  • Standard libraries
  • User defined libraries

Standard Libraries

C language has multiple standard libraries available. To use one of the libraries one has to include them in the project, this can be done with the following sytax #include <libraryname.h> notice how the library header file is surrounded by ‘<‘ and ‘>’, this is used with standard libraries. This means that it is a standard library and the compiler will look for those header files in specific directories e.g. in linux /usr/include.

User defined libraries

It is possible to create your own libraries, in which case the library header file will be surrounded by double quotation marks e.g.  #include "libraryname.h" in this case it is necessary to specify the path to the library. If no path is specified, compiler will look for the files in the same directory as the source file.

List of standard headers (C11 standard)

# Header Description
1 <assert.h> Diagnostics
2 <complex.h> Complex arithmetic
3 <ctype.h> Character handling
4 <errno.h> Errors
5 <fenv.h> Floating-point envrionment
6 <float.h> Characteristics of floating types
7 <inttypes.h> Format conversion of integer types
8 <iso646.h> Alternative spellings
9 <limits.h> Sizes of integer types
10 <locale.h> Localization
11 <math.h> Mathematics
12 <setjmp.h> Nonlocal jumps
13 <signal.h> Signal handling
14 <stdalign.h> Alignment
15 <stdarg.h> Variable arguments
16 <stdatomic.h> Atomics
17 <stdbool.h> Boolean type and values
18 <stddef.h> Common definitions
19 <stdint.h> Integer types
20 <stdio.h> Input/output
21 <stdlib.h> General utilities
22 <stdnoreturn.h> _Noreturn
23 <string.h> String handling
24 <tgmath.h> Type-generic math
25 <threads.h> Threads
26 <time.h> Date and time
27 <uchar.h> Unicode utilities
28 <wchar.h> Extended multibyte and wide character utilities
29 <wctype.h> Wide character classification and mapping utilities

Headers and functions

This section describes most commonly used headers (in Programming I and Programming II courses) and the functions provided by them. Hover for function prototypes.

stdio.h

This header defines several macros, types and many functions performing input and output operations.

Types include:

  • FILE
  • fpos_t
  • size_t

Macros include:

  • BUFSIZ
  • EOF
  • FILENAME_MAX
  • FOPEN_MAX
  • L_tmpnam
  • NULL
  • TMP_MAX
# Function Description
File operations
1 remove Remove file
2 rename Rename file
3 tmpfile Open a temporary file
4 tmpnam Generate temporary filename
File access
1 fclose Close file
2 fflush Flush stream
3 fopen Open file
4 freopen Reopen stream with different file or mode
5 setbuf Set stream buffer
6 setvbuf Change stream buffering
Formatted input/output
1 fprintf Write formatted data to stream
2 fscanf Read formatted data from stream
3 printf Print formatted data to stdout
4 scanf Read formatted data from stdin
5 snprintf Write formatted output to sized buffer
6 sprintf Write formatted data to string
7 sscanf Read formatted data from string
8 vfprintf Write formatted data from variable argument list to stream
9 vfscanf Read formatted data from stream into variable argument list
10 vprintf Print formatted data from variable argument list to stdout
11 vscanf Read formatted data into variable argument list
12 vsnprintf Write formatted data from variable argument list to sized buffer
13 vsprintf Write formatted data from variable argument list to string
14 vsscanf Read formatted data from string into variable argument list
Character input/output
1 fgetc Get character from stream
2 fgets Get string from stream
3 fputc Write character to stream
4 fputs Write string to stream
5 getc Get character from stream
6 getchar Get character from stdin
7 gets Get string from stdin
8 putc Write character to stream
9 putchar Write character to stdout
10 puts Write string to stdout
11 ungetc Unget character from stream
Direct input/output
1 fread Read block of data from stream
2 fwrite Write block of data to stream
File positioning
1 fgetpos Get current position in stream
2 fseek Reposition stream position indicator
3 fsetpos Set position indicator of stream
4 ftell Get current position in stream
5 rewind Set position of stream to the beginning
Error-handling
1 clearerr Clear error indicators
2 feof Check end-of-file indicator
3 ferror Check error indicator
4 perror Print error message

stdlib.h

This header declares five types and several macros and functions of general utility.

Types include:

  • div_t
  • ldiv_t
  • lldiv_t
  • size_t

Macros include:

  • EXIT_FAILURE
  • EXIT_SUCCESS
  • MB_CUR_MAX
  • NULL
  • RAND_MAX
# Function Description
String conversion
1 atof Convert string to double
2 atoi Convert string to integer
3 atol Convert string to long integer
4 atoll Convert string to long long integer
5 strtod Convert string to double
6 strtof Convert string to float
7 strtol Convert string to long integer
8 strtold Convert string to long double
9 strtoll Convert string to long long integer
10 strtoul Convert string to unsigned long integer
11 strtoull Convert string to unsigned long long integer
Pseudo-random sequence generation
1 rand Generate random number
2 srand Initialize random number generator
Dynamic memory management
1 calloc Allocate and zero-initialize array
2 free Deallocate memory block
3 malloc Allocate memory block
4 realloc Reallocate memory block
Environment
1 abort Abort current process
2 atexit Set function to be executed on exit
3 at_quick_exit Set function to be executed on quick exit
4 exit Terminate calling process
5 getenv Get environment string
6 quick_exit Terminate calling process quick
7 system Execute system command
8 _Exit Terminate calling process
Searching and sorting
1 bsearch Binary search in array
2 qsort Sort elements of array
Integer arithmetics
1 abs Absolute value
2 div Integral division
3 labs Absolute value
4 ldiv Integral division
5 llabs Absolute value
6 lldiv Integral division
Multibyte characters
1 mblen Get length of multibyte character
2 mbtowc Convert multibyte sequence to wide character
3 wctomb Convert wide character to multibyte sequence
Multibyte strings
1 mbstowcs Convert multibyte string to wide-character string
2 wcstombs Convert wide-character string to multibyte string

math.h

This header declares two types, many mathematical functions and several macros. To use this library one must also link math library with compiler flag -lm.

Types include:

  • double_t
  • float_t

Macros include:

  • fpclassify – Classify floating-point value
  • isfinite – Is finite value
  • isinf – Is infinity
  • isnan – is Not-A-Number
  • isnormal – Is normal
  • signbit – Sign bit
  • isgreater
  • isgreaterequal
  • isless
  • islessequal
  • islessgreater
  • isunordered
  • math_errhandling – Error handling
  • INFINITY
  • NAN
  • HUGE_VAL
  • HUGE_VALF
  • HUGE_VALL
# Function Description
Trigonometric functions
1 cos Compute cosine
2 sin Compute sine
3 tan Compute tangent
4 acos Compute arc cosine
5 asin Compute arc sine
6 atan Compute arc tangent
7 atan2 Compute arc tangent with two parameters
Hyperbolic functions
1 cosh Compute hyperbolic cosine
2 sinh Compute hyperbolic sine
3 tanh Compute hyperbolic tangent
4 acosh Compute area hyperbolic cosine
5 asinh Compute area hyperbolic sine
6 atanh Compute area hyperbolic tangent
Exponential and logarithmic functions
1 exp Compute exponential function
2 frexp Get significand and exponent
3 ldexp Generate value from significand and exponent
4 log Compute natural logarithm
5 log10 Compute common logarithm
6 modf Break into fractional and integral parts
7 exp2 Compute binary exponential function
8 expm1 Compute exponential minus one
9 ilogb Integer binary logarithm
10 log1p Compute logarithm plus one
11 log2 Compute binary logarithm
12 logb Compute floating-point base logarithm
13 scalbn Scale significand using floating-point base exponent
14 scalbln Scale significand using floating-point base exponent (long)
Power functions
1 pow Raise to power
2 sqrt Compute square root
3 cbrt Compute cubic root
4 hypot Compute hypotenuse
Error and gamma functions
1 erf Compute error function
2 erfc Compute complementary error function
3 tgamma Compute gamma function
4 lgamma Compute log-gamma function
Rounding and remainder functions
1 ceil Round up value
2 floor Round down value
3 fmod Compute remainder of division
4 trunc Truncate value
5 round Round to nearest
6 lround Round to nearest and cast to long integer
7 llround Round to nearest and cast to long long integer
8 rint Round to integral value
9 lrint Round and cast to long integer
10 llrint Round and cast to long long integer
11 nearbyint Round to nearby integral value
12 remainder Compute remainder (IEC 60559)
13 remquo Compute remainder and quotient
Floating-point manipulation functions
1 copysign Copy sign
2 nan Generate quiet NaN
3 nextafter Next representable value
4 nexttoward Next representable value toward precise value
Minimum, maximum, difference functions
1 fdim Positive difference
2 fmax Maximum value
3 fmin Minimum value
Other functions
1 fabs Compute absolute value
2 abs Compute absolute value. NB! Defined in stdlib.h
3 fma Multiply-add

string.h

Thhis header file defines several functions to manipulate C strings and arrays. It also defines macro NULL and type  size_t .

# Function Description
Copying
1 memcpy Copy block of memory
2 memmove Move block of memory
3 strcpy Copy string
4 strncpy Copy characters from string
Concatenation
1 strcat Concatenate strings
2 strncat Append characters from string
Comparsion
1 memcmp Compare two blocks of memory
2 strcmp Compare two strings
3 strcoll Compare two strings using locale
4 strncmp Compare characters of two strings
5 strxfrm Transform string using locale
Searching
1 memchr Locate character in block of memory
2 strchr Locate first occurence of character in string
3 strcspn Get span until character in string
4 strpbrk Locate characters in string
5 strrchr Locate last occurence of character in string
6 strspn Get span of character set in string
7 strstr Locate substring
8 strtok Split string into tokens
Other
1 memset Fill block of memory
2 strerror Get pointer to error message string
3 strlen Get string length

ctype.h

This header declares several functions useful for classifying and transforming characters. See more about which characters are affected by which functions in the table [ 3 ]

# Function Description
Character classification
1 isalnum Check if character is alphanumeric
2 isalpha Check if character is alphabetic
3 isblank Check if character is blank
4 iscntrl Check if character is a control character
5 isdigit Check if character is decimal digit
6 isgraph Check if character has graphical representation
7 islower Check if character is lowercase letter
8 isupper Check if character is uppercase letter
9 isprint Check if character is printable
10 ispunct Check if character is punctuation character
11 isspace Check if character is a white-space
12 isxdigit Check if character is hexadecimal digit
Character conversion
1 tolower Convert uppercase letter to lowercase
2 toupper Convert lowercase letter to uppercase

assert.h

This header defines one macro function which can be used as a standard debugging tool.

void assert(int expression);

All assertions will be disabled when NDEBUG is defined.

time.h

This header defines several functions, macros and types for time manipulation.

Types include:

  • clock_t
  • size_t
  • time_t
  • struct tm

Macro constants include:

  • CLOCKS_PER_SEC
  • NULL
# Function Description
Time manipulation
1 clock Clock program
2 difftime Return difference between two times
3 mktime Convert tm structure to time_t
4 time Get current time
Conversion
1 asctime Convert tm structure to string
2 ctime Convert time_t value to string
3 gmtime Convert time_t to tm as UTC time
4 localtime Convert time_t to tm as local time
5 strftime Format time as string

Resources

  1. C library [ Link ]
  2. C11 standard [ ISO/IEC 9899:201x ]
  3. ctype.h [ Link ]

5 thoughts on “Standard Libraries – C

Comments are closed.