Introduction to C / C++ Programming
- 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.
- 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>"
- 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.
- 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.
- 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 ).
- double fmod( double numerator, double denominator ); - Floating pont equivalent to the mod ( % ) function, it returns the remainder when the numerator is divided by the denominator. ( Note: results may vary from system to system when the denominator is zero. )
Float, Double, and Long Double Library Functions
By default the math library functions for sine, cosine, logarithms, etc. take doubles as their arguments and return doubles for their results. If you pass them different data types, they will automatically and silently convert to and from doubles.
Starting with the C99 standard, however, additional functions are defined for floats and long doubles. For example, in addition to “double cos( double );”, there is now “float cosf( float );” and “long double cosl( long double );” For further details go to http://www.cplusplus.com/reference/cmath/ . If you click on an individual function, such as cos( ), there are tabs to select the C99 standard.
In order to use the C99 standard, you may need to tell your development environment. In Dev C++ the way to do this is to go to the Project menu and select “project options”, and then select the “Parameters” tab. In the column labeled “C++ compiler” enter the string “-std=c99”.
- 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
- Functions for manipulating C-style strings.
- many others
- Defined limits for the current system, such as the maximum value of an int or a double.
- Functions related to time, such as time of day or timing things.