Standard Library Functions -------------------------- C programs have access to a standard, preprogrammed set of functions for handling input and output of data, computing mathematical quantities, and manipulating strings of characters. These preprogrammed functions are stored in a system library that contains the collection of standard functions available on your system. Before using a function available in the system library, you must know: 1. The name of the function. 2. The arguments required by the function. 3. The data type of the result (if any) returned by the function. 4. What the function does. For example, if the following function appears in a program: double sqrt(double sum); then we known the following about the function: 1. its name is "sqrt" 2. arguments are "sum" of type double 3. the return data type is double 4. no description is given, need to consult man pages Most library functions require bringing in a standard header file with the #include< > statement. This has been seen before. For example, OpenGL requires the GL/gl.h header file. Input/Output Library Functions ------------------------------ We have already talked about a set of these. These include: printf() scanf() getchar() putchar() The header for getchar() is: int getchar(void); The value returned is an integer (ascii value converted to char if assigned to char variable). There are no input parameters (the meaning of the void type). The header for putchar() would be something like: void putchar(char); This means it returns no value and accepts a char value as input. Mathematical Library Functions ------------------------------ Standard arithmetic operators such as +, -, * and / exist in C, but raising numbers to a power, finding the square root of a number and determining absolute values are not directly available. To access these routines, the math library is needed. Two additional actions are needed: 1. add #include to include the math library header file 2. use the -lm compile option on the compilation line like: cc program.c -lm -o program The functions available are: int abs(int num) Returns the absolute value of an integer. Ex: abs(-10) = 10 Ex: abs( 10) = 10 long labs(long num) Returns the absolute value of a long integer. Ex: labs(-100000) = 100000 double fabs(double num) Returns the absolute value of a double precision number. Ex: fabs(-1.23456) = 1.23456 double pow(double x, double y) Returns x raised to the power of y. Ex: pow(2.0, 3.0) = 8.0 int rand(void) Returns a pseudorandom number. double sin(double angle) Returns the sine of an angle. The angle must be in radians. Ex: sin(3.14159) = 0 double cos(double angle) Returns the cosine of an angle. The angle must be in radians. Ex: cos(3.14159) = -1; double sqrt(double num) Returns the square root of a number. Ex: sqrt(81.0) = 9.0 The arguments for an function may be expressions, such as: x - 10 * 3 / 4 * t . The expression will be evaluated first and then the resulting value will be the effective argument for the function. For example: sqrt(4.0 + 7 * 3) the same as sqrt(4.0 + 21) the same as sqrt(25.0) which equals 5.0 See page 221 for more examples. String Library Functions ------------------------ This library is valuable for performing string manipulation. If you wanted to determine whether two strings, such as char str1[] = "abc"; char str2[] = "abcd"; were equivalent, then you would have to check each element of the string with standard comparisons: str1[0] == str2[0] && str1[1] == str2[1] && ... This could get tedious if the strings were 1000 characters long. There are some built-in routines in the string library to assist you. To access the string library, all that is needed is add the line: #include Here are some sample functions: char *strcat(char *s1, char *s2) Concatenates string s2 to s1 and returns that string. Ex: char s[20], s1[]="hi", s2[]="there"; s = strcat(s1, s2); s is "hithere" char *strchr(char *s1, char c) Locates the first occurrence of the character in c within the string s1. It returns the string at that location. int strcmp(char *s1, char *s2) Compares string s1 to s2. It returns 0 if equal, else a number != 0. char *strcpy(char *s1, char *s2) Copies string s2 into s1. In other words, s1 = s2. It returns the string. int strlen(char *s) Determines the length of the string s. Ex: char s[]="hello"; int i; i=strlen(s); i has value 5 Miscellaneous Routines ---------------------- See the C Book, page 225.