×

Library Functions in C

What are library functions?

Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are grouped together and placed.

Each function in the library performs a specific task(operation) and to make things easy for the programmer.

The library functions are prototyped and defined at the time of designing the compiler and are present in their respective header files.

Eg: The main() function, printf() function etc. are some of the commonly used functions that are the library functions in C.

* The header files are implemented in the C program using the following command,

#include<filename.h>

For eg: #include<stdio.h>, #include<conio.h> …

* If we do not use the header files, the program will throw an error.

For eg: In order to use the printf() function, <stdio.h> should be included in order to print the desired output.

Significance/ Advantages of library function

The major reason to use library functions is that they make the work of a programmer easy, the programmer can simply use the pre-existing codes available in the compiler and need not worry about declaring and defining his own code in order to perform certain basic functions.

The other advantages of using library functions are as follows:

  • Flexibility
  • Performance optimized
  • Time-saving
  • Portability and reliability

Header File Functions

Most of us are aware of various header files or library functions as they are often used while programming in C. Following is a list of commonly used library functions and the library files in which they are included:

Header FileMeaningLibrary Functions
<stdio.h>Standard input-output headerprintf(),scanf(),getchar(c), putchar(s) etc.
<conio.h>Console input-output headerclrscr() , getch() etc.
<stdlib.h>Standard library headercalloc() and malloc()
<math.h>Math header (carry out mathematical calculations)sqrt(d),pow(d1,d2) etc.
<string.h>String header (performs operations on strings)strcpy,strlen,strrev etc.
<ctype.h>Character type header (carry out operations on character)isdigit(), tolower() , toupper() etc.

Accessing Library Function

A library function can be accessed by following the simple steps;

  1. Writing the function name
  2. Writing the list of arguments
  • The arguments must be enclosed in brackets. They must be separated by commas.
  • There may be no arguments (in some cases)

Eg: pow (p1,p2) → arguments enclosed in brackets and separated by commas

         ↓

    Function name

Program 1: use of <stdio.h> header files and use of printf()  and scanf() library functions

#include<stdio.h>    // Use of stdio.h header

#include<stdio.h>    // Use of stdio.h header


int main()


{
char name[30];


printf ("Enter your name here : "); // Use of printf() library function
scanf ("%s", name); // Use of scanf() library function


printf("The name entered was: %s\n", name);


return 0;


}

Output of the program:

Library Functions in C

Program 2: use of <math.h> header file and use of sqrt() library functions

#include <stdio.h>
#include <math.h>


int main()
{
   float num, sq_root;
   printf("Enter a number: ");
   scanf("%f", &num);


   sq_root = sqrt(num);  // computes the square root of num and stores in root.




   printf("Square root of %.2f = %.2f", num, sq_root);
   return 0;


}

Output of the program:

Library Functions in C

Related Topics

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.