Library function in C language are built-in function which are grouped together and placed in a common place called library function. Each library function in C performs specific operation. In order to use these library functions you need to import appropriate header files at the beginning of a program. Library function is also known as predefined function.
Note: All the library functions are declared in header files and its definition is stored in library file. Therefore, In order to use these library functions you need to include appropriate header files before writing the any program.
- Advantage of using C library function
- Example of library function: printf() and scanf()
- Example of library function: sqrt()
- Library Functions in Different Header Files
- Learn more about function
Quick links
If you want to use printf() and scanf() functions, the header
file #include <stdio.h>
#include <stdio.h>
int main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d,%d", &a, &b);
prinf("You have entered numbers %d and %d", a, b);
}
Output
Enter two numbers: 10 20
You have entered numbers 10 and 20
Note: If you try to use printf() and
scanf() functions without including the header file #include
<stdio.h>
Advantage of using C library functions:
- Simple and easy to use: These functions has gone through multiple rigorous testing and are easy to work.
- The functions are optimized for performance: Since these functions are standard library functions, a dedicated group of developers constantly work on it and make them better. In the process, the are able to create the most efficient code optimized for maximum performance.
- It saves considerable development time: Since the general functions like printing to a screen, calculating the square root, and many more are already written. You shouldn't worry about creating them. whenever you need these function you can call and use them in your program.
- The functions are portable: Whenever changing real-world needs, your application is expected to work every time, everywhere. And, these library function help you in that they do the same thing on every computer.
Example: Square root using sqrt() function
Suppose, you want to find the square root of a number, so you can use the sqrt() library function to compute the square root of a number. To use this function you need to include <math.h>
#include <stdio.h> //declaration of printf() and scanf()
#include <conio.h> //declaration of getch()
#include <math.h> //declaration of sqrt()
void main()
{
float num, root;
clrscr();
printf("Enter a umber: ");
scanf("%f", &num);
/* Calculate the square root of given number and stores in root */
root = sqrt(num);
printf("square root of %.2f = %.2f", num, root);
getch();
}
Output
Enter a number: 12
square root of 12.00 = 3.46
Note: As you can see, clrscr(), printf(), scanf(), sqrt(), and getch() these all are the example of library functions which are used in the program by calling it directly.
Library Functions in Different Header Files:
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. You need to include appropriate header file at the beginning of a program to supply the declarations you need to invoke
List of header file in C language.
Header file | Description |
---|---|
<assert.h> |
Program assertion functions |
<conio.h> |
console Input/output functions |
<ctype.h> |
Character type functions |
<locale.h> |
Localization functions |
<math.h> |
Mathematics functions |
<setjmp.h> |
Jump functions |
<signal.h> |
Signal handling functions |
<stdarg.h> |
Variable arguments handling functions |
<stdio.h> |
Standard Input/output functions |
<stdlib.h> |
Standard Utility functions |
<string.h> |
String handling functions |
<time.h> | Date time functions |
- Function in C
- Properties of function
- User defined function
- Types of user defined function
- Function call by value
- Function call by reference
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.