Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

The Standard Library Functions

The C language includes many standard library functions that are commonly used to perform tasks like input/output, mathematics operations, string manipulation, and more. These functions are organized into header files that must be included at the top of a program. Popular header files include stdio.h for input/output functions like printf and scanf, string.h for string functions like strcat and strcmp, and math.h for mathematics functions like sin, cos, sqrt. Library functions make common operations more convenient but can be less efficient than implementing them directly in code. Proper use of library functions requires including the correct header and ensuring arguments match the expected types.

Uploaded by

Jag Desh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

The Standard Library Functions

The C language includes many standard library functions that are commonly used to perform tasks like input/output, mathematics operations, string manipulation, and more. These functions are organized into header files that must be included at the top of a program. Popular header files include stdio.h for input/output functions like printf and scanf, string.h for string functions like strcat and strcmp, and math.h for mathematics functions like sin, cos, sqrt. Library functions make common operations more convenient but can be less efficient than implementing them directly in code. Proper use of library functions requires including the correct header and ensuring arguments match the expected types.

Uploaded by

Jag Desh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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()

Library functions
The C language is accompanied by a number of standard library functions which carry out various useful tasks. In particular, all input and output operations ( e.g., writing to the terminal) and all math operations (e.g., evaluation of sines and cosines) are implemented by library functions. In order to use a library function, it is necessary to call the appropriate header file at the beginning of the program. The header file informs the program of the name, type, and number and type of arguments, of all of the functions contained in the library in question. A header file is called via the preprocessor statement
#include <filename>

where filename represents the name of the header file. A library function is accessed by simply writing the function name, followed by a list of arguments, which represent the information being passed to the function. The arguments must be enclosed in parentheses, and separated by commas: they can be constants, variables, or more complex expressions. Note that the parentheses must be present even when there are no arguments. The C math library has the header file math.h, and contains the following useful functions:
Function -------acos(d) asin(d) atan(d) atan2(d1, d2) cbrt(d) cos(d) cosh(d) exp(d) fabs(d) hypot(d1, d2) Type ---double double double double double double double double double double Purpose ------Return Return Return Return Return Return Return Return Return Return arc cosine of d (in range 0 to pi) arc sine of d (in range -pi/2 to pi/2) arc tangent of d (in range -pi/2 to pi/2) arc tangent of d1/d2 (in range -pi to pi) cube root of d cosine of d hyperbolic cosine of d exponential of d absolute value of d sqrt(d1 * d1 + d2 * d2)

log(d) log10(d) pow(d1, d2) sin(d) sinh(d) sqrt(d) tan(d) tanh(d)

double double double double double double double double

Return Return Return Return Return Return Return Return

natural logarithm of d logarithm (base 10) of d d1 raised to the power d2 sine of d hyperbolic sine of d square root of d tangent of d hyperbolic tangent of d

Here, Type refers to the data type of the quantity that is returned by the function. Moreover, d, d1, etc. indicate arguments of type double. A program that makes use of the C math library would contain the statement
#include x = cos(y); <math.h>

close to its start. In the body of the program, a statement like would cause the variable x to be assigned a value which is the cosine of the value of the variable y (both x and y should be of type double). Note that math library functions tend to be extremely expensive in terms of CPU time, and should, therefore, only be employed when absolutely necessary. The classic illustration of this point is the use of the pow() function. This function assumes that, in general, it will be called with a fractional power, and, therefore, implements a fullblown (and very expensive) series expansion. Clearly, it is not computationally efficient to use this function to square or cube a quantity. In other words, if a quantity needs to be raised to a small, positive integer power then this should be implemented directly, instead of using the pow() function: i.e., we should write x * x rather than pow(x, 2), and x * x * x rather than pow(x, 3), etc. (Of course, a properly designed exponentiation function would realize that it is more efficient to evaluate small positive integer powers by the direct method. Unfortunately, the pow() function was written by computer scientists!) The C math library comes with a useful set of predefined mathematical constants:
Name ---M_PI M_PI_2 M_PI_4 M_1_PI M_SQRT2 M_SQRT1_2 M_E Description ----------Pi, the ratio of a circle's circumference to its diameter. Pi divided by two. Pi divided by four. The reciprocal of pi (1/pi). The square root of two. The reciprocal of the square root of two (also the square root of 1/2). The base of natural logarithms.

Use of Library Functions


To use a function, ensure that you have made the required #includes in your C file. Then the function can be called as though you had defined it yourself. It is important to ensure that your arguments have the expected types, otherwise the function will probably produce strange results. lint is quite good at checking such things. Some libraries require extra options before the compiler can support their use. For example, to compile a program including functions from the math.h library the command might be
cc mathprog.c -o mathprog -lm

The final -lm is an instruction to link the maths library with the program. The manual page for each function will usually inform you if any special compiler flags are required.

You might also like