Skip to main content

The Standard Library Functions

Some of the "commands" in C are not really "commands" at all but are functions. For example, we have been using printf and scanf to do input and output, and we have used rand to generate random numbers - all three are functions.

There are a great many standard functions that are included with C compilers and while these are not really part of the language, in the sense that you can re-write them if you really want to, most C programmers think of them as fixtures and fittings. Later in the course we will look into the mysteries of how C gains access to these standard functions and how we can extend the range of the standard library. But for now a list of the most common libraries and a brief description of the most useful functions they contain follows:

  1. stdio.h: I/O functions:
    1. getchar() returns the next character typed on the keyboard.
    2. putchar() outputs a single character to the screen.
    3. printf() as previously described
    4. scanf() as previously described
  2. string.h: String functions
    1. strcat() concatenates a copy of str2 to str1
    2. strcmp() compares two strings
    3. strcpy() copys contents of str2 to str1
  3. ctype.h: Character functions
    1. isdigit() returns non-0 if arg is digit 0 to 9
    2. isalpha() returns non-0 if arg is a letter of the alphabet
    3. isalnum() returns non-0 if arg is a letter or digit
    4. islower() returns non-0 if arg is lowercase letter
    5. isupper() returns non-0 if arg is uppercase letter
  4. math.h: Mathematics functions
    1. acos() returns arc cosine of arg
    2. asin() returns arc sine of arg
    3. atan() returns arc tangent of arg
    4. cos() returns cosine of arg
    5. exp() returns natural logarithim e
    6. fabs() returns absolute value of num
    7. sqrt() returns square root of num
  5. time.h: Time and Date functions
    1. time() returns current calender time of system
    2. difftime() returns difference in secs between two times
    3. clock() returns number of system clock cycles since program execution
  6. stdlib.h:Miscellaneous functions
    1. malloc() provides dynamic memory allocation, covered in future sections
    2. rand() as already described previously
    3. srand() used to set the starting point for rand()