Inbuilt Functions in C
What is an Inbuilt Function?
For built-in functions, the compiler creates inline code during compilation. Every call to a built-in function replaces a runtime call to the dynamic library function with the same name.
Application code uses built-in functions while executing without referencing a dynamic library. Even if built-in functions marginally increase the size of a produced program, the enhanced efficiency that results from lowering the cost of the dynamic calls should more than make up for this.
C Library Functions
The built-in functions in C programming are known as C Standard library functions or simply C Library functions.
These functions' prototypes and data definitions may be found in the corresponding header files. We must include the header file in our program to utilize these functions.
The C Standard Function Library is an extensive collection of smaller libraries, each containing the source code for several functions. We must use header files to link each library to the more extensive library before we can utilize any of these libraries. These functions' definitions may be found in the corresponding header files. We must include the header file in the program to utilize these functions. Here are some of the C libraries, along with their description:
Header Library | Description |
stdio.h | Standard input/output operations. |
stdlib.h | General purpose functions, memory management, etc. |
math.h | Mathematical functions and constants. |
string.h | String manipulation functions. |
ctype.h | Character handling functions. |
time.h | Date and time functions. |
assert.h | Runtime assertions for debugging. |
errno.h | Error handling and reporting. |
limits.h | Constants specifying the limits of integral types. |
stdbool.h | Boolean data type and values. |
stdint.h | Fixed-size integer types. |
float.h | Constants specifying properties of floating-point types. |
stddef.h | Standard definitions and types. |
There are several other libraries in the C programming language; these are some of them.
Essential Inbuilt/Library Functions
Here are the important inbuilt functions in the C programming language:
Function | Description |
printf() | Writes formatted output to the standard output stream. |
scanf() | Reads formatted input from the standard input stream. |
malloc() | Allocates a block of memory dynamically. |
free() | Deallocates a previously allocated block of memory. |
strlen() | Returns the length of a string. |
strcpy() | Copies a string from source to destination. |
strcmp() | Compares two strings lexicographically. |
sprintf() | Writes formatted data to a string. |
fopen() | Opens a file. |
fclose() | Closes a file. |
fgets() | Reads a line from a file. |
fprintf() | Writes formatted output to a file. |
atoi() | Converts a string to an integer. |
atof() | Converts a string to a floating-point number. |
rand() | Generates a pseudo-random number. |
time() | Returns the current time as the number of seconds since the epoch. |
isdigit() | Checks if a character is a decimal digit. |
toupper() | Converts a character to uppercase. |
isalpha() | Checks if a character is an alphabetic character. |
exit() | Terminates the program execution. |
Implementing Inbuilt Functions
In this tutorial section, we will see the implementation of the inbuilt/library functions we have discussed so far.
- printf():
Code:
#include <stdio.h>
int main()
{
int num = 10;
float pi = 3.14159;
char* str = "Hello, World!";
printf("Number: %d\n", num);
printf("Pi: %.2f\n", pi);
printf("String: %s\n", str);
return 0;
}
Output:
![Inbuilt Functions in C](https://static.tutorialandexample.com/c/inbuilt-functions-in-c1.png)
- scanf():
Code:
#include <stdio.h>
int main()
{
int num;
char name[50];
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter your name: ");
scanf("%s", name);
printf("Number: %d\n", num);
printf("Name: %s\n", name);
return 0;
}
Output:
![Inbuilt Functions in C](https://static.tutorialandexample.com/c/inbuilt-functions-in-c2.png)
- malloc():
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* numbers;
int size, i;
printf("Enter the size of the array: ");
scanf("%d", &size);
// Allocate memory dynamically using malloc()
numbers = (int*)malloc(size * sizeof(int));
if (numbers == NULL) {
printf("Memory allocation failed. Exiting...\n");
return 1;
}
printf("Enter %d numbers:\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}
printf("Numbers entered:\n");
for (i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// Free the allocated memory using free()
free(numbers);
return 0;
}
Output:
![Inbuilt Functions in C](https://static.tutorialandexample.com/c/inbuilt-functions-in-c3.png)
- time():
Code:
#include <stdio.h>
#include <time.h>
int main()
{
// Get the current time using the time()
time_t currentTime = time(NULL);
// Convert the time to a string representation using ctime()
char* timeString = ctime(¤tTime);
printf("Current time: %s", timeString);
return 0;
}
Output:
![Inbuilt Functions in C](https://static.tutorialandexample.com/c/inbuilt-functions-in-c4.png)
- isalpha():
Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (isalpha(ch))
{
printf("The character is an alphabet.\n");
}
else
{
printf("The character is not an alphabet.\n");
}
return 0;
}
Output:
![Inbuilt Functions in C](https://static.tutorialandexample.com/c/inbuilt-functions-in-c5.png)
Conclusion
To perform everyday activities in C, such as memory allocation, input/output operations, string manipulation, and more, built-in functions are used. Some examples of regularly used ones include the built-in C methods print (), scanf(), malloc(), time(), and isalpha().
To utilize a built-in function in C, include the header file providing the function's prototype. For instance, using the printf() function to display a message on the screen requires that the header file be included at the beginning of the program.
Built-in functions are an essential part of the C programming language because they provide pre-built functionality that can be used to carry out everyday tasks.