Introduction to C / C++ Programming
Libraries
Overview
- There are many libraries full of functions available in C/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 A of "Engineering Problem Solving with C++" 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 <cmath>" at the beginning of the program.
- For a C program ( as opposed to C++ ), remove the letter C from the beginning of the library name, and add a ".h" at the end. So to use the math library in C, you would need to "#include <math.h>"
<iostream>
- This library is used by essentially every C++ program.
- No exact equivalent in C, but most C programs #include <stdio.h> for their I/O needs.
- cin - Standard input, normally the keyboard.
- cout - Standard output, normally the screen, used for program results.
- cerr - Standard error, normally the screen, used for error messages and other non-results messages.
- Operators << and >>, defined for basic types.
- int get( char ); - Gets a single character from the input stream.
- getline( c_string var, int max, [ char delimiter ] ); - Gets a complete line.
- char peek( ); - Examines the next input char but does not remove it from the input stream.
- putback( char ); - Puts a character back on the input stream.
- put( char ); - Outputs a single char.
- flush( ); - Flushes the output buffer.
<cstdlib>
- The standard C library. Automatically #included by <iostream>, so normally does not need to be directly #included.
- 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.
<cmath>
- Required for most of the standard math functions
- sin, cos, tan, asin, acos, atan - Standard trig funtions, with angles in radians.
- 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>
- 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
<cstring>
- Functions for manipulating C-style strings.
- strcpy
- strlen
- many others
<climits>
- Defined limits for the current system, such as the maximum value of an int or a double.
<ctime>
- Functions related to time, such as time of day or timing things.