Tuesday, 1 October 2013

Libraries in C Language

Overview

  • There are many libraries full of functions available in C, either standard libraries that come with most systems or special libraries that can either be purchased or obtained for free from third parties. ( You can even create your own libraries. )
  • This page lists some of the most commonly used libraries and some of the most commonly used functions in each one.
  • This is not intended to be a complete listing.
  • See Appendix D of "C Programming, A Modern Approach", Second Edition, by K.N. King, or most any other good C book for more details.

#include

  • In order to use library functions, you must #include a header file that contains definitions for the functions ( and sometimes important constants ) in the library.
  • The subtopics listed below correspond to the #include required to use the libarary in a C program. So to use the math library, you would need to include a line that said "#include <math.h>" at the beginning of the program.

<stdlib.h>

  • The standard C library. Often automatially included when including other headers, but that may be implementation dependent.
  • RAND_MAX - The maximum value produced by the rand( ) function.
  • int rand( void ); - Returns a ( pseudo ) random integer from 0 to RAND_MAX
    • Requires the use of srand ( see below )
    • ( double ) rand( ) / RAND_MAX will yield a random double precison number from 0.0 to 1.0 inclusive.
  • void srand( unsigned int );
    • Initializes the random number generator.
    • Should be called exactly once in a program, usually in main, and before any calls to rand( );
    • Ex: srand( time( NULL )); - Initializes the random number generator using the time as a seed.
  • void exit( int ); - Immediate normal program termination.
  • void abort( void ); - Immediate abnormal program termination.
  • int abs( int ); - Absolute value
    • long labs( long ); - Absolute value
    • See also fabs under <cmath> below.

  • double atof( const char[ ] ); - Converts a string of numeric characters ( e.g."3.14159E17" ) into a double precision number
    • atoi and atol convert strings to ints and longs respectively.
  • NULL - Zero, formatted properly for use as a pointer.

<stdio.h>

  • The standard C input/output library. Almost every C program ever written needs this header file
  • printf( ) and scanf( ) - See Formatted I/O
  • fprintf( ) and fscanf( ) - File versions of printf( ) and scanf( )
  • sscanf( ) and sprintf( ) - Buffer ( array ) versions of printf( ) and scanf( )
  • gets( ) and fgets( ) - Reads an entire line of text, including spaces.
    • ( printf( "%s", ... ) stops scanning when it encounters a space. )
  • fopen( ) and fclose( ) - Used to open and close files
  • fread( ) and fwrite( ) Low-level file I/O ( binary, unformatted )
  • ftell( ) and fseek( ) - Used to determine and change position in a file.
  • getc( ), putc( ), fgetc( ) and fputc( ) - character-by-character I/O

<math.h>

  • Required for most of the standard math functions
  • sin, cos, tan, asin, acos, atan - Standard trig funtions, with angles in radians. ( doubles )
    • sinf, cosf, etc. - Float data type versions.
    • sinl,cosl, etc. - Long double data type versions.
    • sinh, cosh, etc. - Hyperbolic trig functions.
  • int ceil( double ); - rounds down to the next integer towards infinity.
    • int floor( double ); - rounds to next integer towards minus infinity.
  • double exp( double ); - exponential - e raised to a power.
    • double log( double ); - Natural logarithm, base e.
    • double log10( double ); - Common logarithm, base 10.
  • double sqrt( double ); - Square root.
  • double fabs( double ); - Absolute value for doubles.
  • double pow( double x, double y ); - Raises x to the y power.
    • If y is either 2 or 3, then it is faster to multiply out the terms than to call pow.
    • I.e. use X * X or X * X * X instead of pow( X, 2 ) or pow( X, 3 ).

<cctype.h>

  • Functions for examining and manipulating character types
  • boolean isdigit( char ); - Determines if a character is a numeric digit ( 0 - 9 )
  • char toupper( char ); - Converts a character to upper case.
  • many others

<string.h>

  • Functions for manipulating C-style strings.
  • strcpy
  • strlen
  • many others

<limits.h>

  • Defined limits for the current system, such as the maximum value of an int or a double.

<time.h>

  • Functions related to time, such as time of day or timing things.