Library functions (2024)

Library functions (1) Library functions (2) Library functions (3)
Next: Data input and output Up: Scientific programming in C Previous: OperatorsThe C language is accompanied by a number of standard library functions whichcarry 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 appropriateheader file at the beginning of the program. The header fileinforms 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, followedby a list of arguments, which represent the information being passed tothe function. The arguments must be enclosed in parentheses, and separated by commas:they can be constants, variables, or more complex expressions. Notethat the parentheses must be present even when there are no arguments.

The C math library has the header file math.h, and contains thefollowing useful functions:

Function Type Purpose-------- ---- -------acos(d) double Return arc cosine of d (in range 0 to pi)asin(d) double Return arc sine of d (in range -pi/2 to pi/2)atan(d) double Return arc tangent of d (in range -pi/2 to pi/2)atan2(d1, d2) double Return arc tangent of d1/d2 (in range -pi to pi)cbrt(d) double Return cube root of dcos(d) double Return cosine of dcosh(d) double Return hyperbolic cosine of dexp(d) double Return exponential of dfabs(d) double Return absolute value of dhypot(d1, d2) double Return sqrt(d1 * d1 + d2 * d2)log(d) double Return natural logarithm of dlog10(d) double Return logarithm (base 10) of dpow(d1, d2) double Return d1 raised to the power d2sin(d) double Return sine of dsinh(d) double Return hyperbolic sine of dsqrt(d) double Return square root of dtan(d) double Return tangent of dtanh(d) double Return hyperbolic tangent of d
Here, Type refers to the data type of the quantity that is returned bythe function. Moreover, d, d1, etc. indicate arguments oftype double.

A program that makes use of the C math library would contain the statement

#include <math.h>
close to its start. In the body of the program,a statement like
x = cos(y);
would cause the variable x to be assigned a value which is the cosine ofthe value of the variable y (both x and y should be oftype double).

Note that math library functions tend to be extremely expensive in terms of CPU time, andshould, therefore, only be employed when absolutely necessary. The classic illustrationof 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 full-blown (andvery expensive) series expansion. Clearly, it is not computationally efficient to use this function tosquare 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 ofusing the pow() function: i.e., we should write x * x rather thanpow(x, 2), and x * x * x rather than pow(x, 3), etc.(Of course, a properly designed exponentiation function would realize that it is moreefficient 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 Description ---- ----------- M_PI Pi, the ratio of a circle's circumference to its diameter. M_PI_2 Pi divided by two.M_PI_4 Pi divided by four. M_1_PI The reciprocal of pi (1/pi).M_SQRT2 The square root of two.M_SQRT1_2 The reciprocal of the square root of two (also the square root of 1/2). M_E The base of natural logarithms.

The other library functions commonly used in C programs will be introduced,as appropriate, during the remainder of this discussion.

Library functions (4) Library functions (5) Library functions (6)
Next: Data input and output Up: Scientific programming in C Previous: Operators
Richard Fitzpatrick2006-03-29
Library functions (2024)

FAQs

What is the library function answer? ›

Library functions are built-in functions that are grouped together and placed in a common location called library. Each function here performs a specific operation. We can use these library functions to get the predefined output. All C standard library functions are declared by using many header files.

What are the 5 basic functions of the library? ›

Basic tasks in library management include planning acquisitions (which materials the library should acquire, by purchase or otherwise), classifying and preserving items (especially rare and fragile archival materials such as manuscripts), deaccessioning materials, patron borrowing, and developing and administering ...

What are library functions Why are they important? ›

Library functions in C are also inbuilt functions in C language. These inbuilt functions are located in some common location, and it is known as the library. All the functions are used to execute a particular operation. These library functions are generally preferred to obtain the predefined output.

How do the library functions work? ›

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.

What are the three functions of library? ›

A library's basic functions include the promotion of education, the dissemination of information and the promotion of culture. Promotion of Education: Libraries provide the means of individual self-development and facilitate all modes of education, including formal, non-formal, adult and lifelong education.

What are library functions and types? ›

Academic libraries serve colleges and universities. Public libraries serve cities and towns of all types. School libraries serve students from Kindergarten to grade 12. Special libraries are in specialized environments, such as hospitals, corporations, museums, the military, private business, and the government.

What are the four key functions of a library? ›

Across those four thousand years, librarians constructed libraries large and small that were designed to effectively collect, organize, preserve, and make accessible the graphic records of society.

What are the 7 sections of the library? ›

  • Different Sections in the Library. ...
  • Circulation Section. ...
  • Acquisition Section. ...
  • Classification Section. ...
  • Catalogue Section. ...
  • Periodicals Section. ...
  • Information Technology Section. ...
  • Reference Section.

Do libraries contain functions? ›

library files usually have the compiled definition of a function. The header files usually just have the prototype… What is the purpose of function prototype?

What is not a library function? ›

the correct answer is option (c). Explanation: pow(),printf() and sqrt() are all library function which are also called in built function and sum() is a function used in calc so, sum() is not a in built function.

What is a library function and user-defined function? ›

A programmer creates a function according to the requirement of a program, which is called a user-defined function. A function whose prototypes are already defined in the C library is called the library function. 2. A user-defined function is required to write the complete code before using the function in a program.

What is the difference between library functions and user-defined functions? ›

User-defined functions are not stored in library files. Library Functions are stored in a special library file. There is no such kind of requirement to add a particular library.

What is the standard library function? ›

C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.

What is difference between library and function? ›

Functions are meant to do a specific task. Libraries contain the predefined functions provided by the programming language.

What are the functions of library collection? ›

Collection management plays an important role in the field of the library by which the selection, acquisition, storage and maintenance of collections are involved. The forms of collection development include planning, selection, and construction of collections.

What is the library function in Python? ›

What is python Library? A library is a collection of code that makes everyday tasks more efficient. Using Seaborn, for example, you can generate visualisations with just one line of code. To create a chart from an object, you'd have to write a lot of code without a library like this.

What is a function library in Excel? ›

The Function Library Group allows the user to utilize all of the formulas in Excel. The function is automatically inserted in the cell that is selected.

What is a library function in C++? ›

Library Functions

These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc.

Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6381

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.