BCA Sem 1 | Unit 3 | Computer Concepts & C Programming
Introduction to Library Functions in C — BKNMU Junagadh
Predefined Functions, Standard Header Files, Built-in Utilities, and User-Defined vs Library Function Differences
C groups its standard library functions into specialized header files based on their operations:
1. I/O Functions
Handles standard input and output streams. Examples include printf(), scanf(), getchar(), and putchar().
2. Math Functions
Performs mathematical operations like square roots, powers, and trigonometry. Examples include sqrt(), pow(), and abs().
3. String Functions
Manipulates character arrays and text strings. Examples include strlen(), strcpy(), strcat(), and strcmp().
4. Character Functions
Tests and converts individual characters. Examples include isalpha(), isdigit(), toupper(), and tolower().
Here is a detailed comparative summary showing how library functions differ from functions written by developers:
| Comparison Feature | Library Functions (Built-in) | User-Defined Functions (UDF) |
|---|---|---|
Definition & Source | Pre-compiled and built into the C standard library system. | Written and defined explicitly by the programmer in source code. |
Header File Dependency | Requires inclusion of specific header files using #include <...>. | Does not require external header files unless declared in custom .h files. |
Execution Performance | Highly optimized by compiler designers for maximum speed and efficiency. | Depends entirely on the coding skill and logic written by the developer. |
Function Name Freedom | Function names are fixed keywords (e.g., cannot rename printf). | Programmer can choose any custom valid identifier as the function name. |
#include <header.h> preprocessor directive.
1. Using Math Library Functions (<math.h>):
#include <stdio.h>
#include <math.h> // Required for sqrt() and pow()
int main() {
double num = 25.0, base = 2.0, exp = 3.0;
// Using predefined square root and power functions
printf("Square Root of %.1f = %.1f\n", num, sqrt(num));
printf("2 raised to power 3 = %.1f\n", pow(base, exp));
return 0;
}
2. Using Character Testing Functions (<ctype.h>):
#include <stdio.h>
#include <ctype.h> // Required for isdigit() and toupper()
int main() {
char ch = 'a';
if (isalpha(ch)) {
printf("'%c' is an Alphabet. Uppercase: %c\n", ch, toupper(ch));
}
return 0;
}
3. Using Standard Utility Functions (<stdlib.h>):
#include <stdio.h>
#include <stdlib.h> // Required for abs() and exit()
int main() {
int negative_val = -42;
// Calculating absolute positive value
printf("Absolute value of %d = %d\n", negative_val, abs(negative_val));
return 0;
}
Step 1: Include Standard Header File
The compiler preprocessor reads the #include <filename.h> directive to load function prototypes.
Step 2: Function Call with Arguments
The user invokes the function in main program code, passing valid parameter values (e.g., sqrt(16)).
Step 3: Execution & Value Return
Pre-compiled binary code executes the task and returns the computed result back to the calling line.
pow() or sqrt() without including #include <math.h>, the compiler will throw an implicit declaration error or produce incorrect output values.
- Definition: Built-in, pre-compiled functions supplied with the C compiler environment.
- Header Inclusion: Mandatory via
#include <header.h>preprocessor directives. - Common Header Files:
stdio.h(I/O),math.h(Math),string.h(Strings),ctype.h(Chars),stdlib.h(Utils). - Key Benefits: Code reusability, optimal speed, error-free execution, and standardized design.
- Usage Syntax: Called by name with valid arguments inside parentheses.