Skip to main content

6. Bonus: Some C Library Functions

Throughout this module, we've focused on defining our own functions. However, C also provides a rich set of built-in functions, known as Standard Library Functions. These functions are pre-written and grouped into various libraries (e.g., <stdio.h>, <stdlib.h>, <math.h>) to perform common tasks like input/output, memory management, and mathematical operations. When you use functions like printf() or scanf(), you are actually calling functions that have been defined in the standard input/output library (stdio.h). These functions abstract away complex implementations, allowing you to use them easily by simply including their respective header files.

6.1 Mathematical Functions

Include <math.h> header for mathematical functions:

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

int main() {
    double angle = 45.0;
    double radians = angle * M_PI / 180.0;  // Convert to radians
    
    printf("Mathematical Functions:\n");
    printf("sqrt(16) = %.2f\n", sqrt(16.0));           // Square root
    printf("pow(2, 3) = %.2f\n", pow(2.0, 3.0));       // 2^3
    printf("sin(45°) = %.4f\n", sin(radians));         // Sine
    printf("cos(45°) = %.4f\n", cos(radians));         // Cosine
    printf("log(10) = %.4f\n", log(10.0));             // Natural log
    printf("log10(100) = %.2f\n", log10(100.0));       // Base-10 log
    printf("ceil(4.3) = %.0f\n", ceil(4.3));           // Ceiling
    printf("floor(4.7) = %.0f\n", floor(4.7));         // Floor
    printf("fabs(-5.5) = %.1f\n", fabs(-5.5));         // Absolute value
    
    return 0;
}

Note: When compiling programs that use <math.h>, you might need to link the math library:

gcc -o program program.c -lm

6.2 String Functions

Include <string.h> header for string manipulation:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[50] = "Hello";
    char str2[50] = "World";
    char str3[100];
    
    // String length
    printf("Length of '%s': %lu\n", str1, strlen(str1));
    
    // String copy
    strcpy(str3, str1);
    printf("After strcpy: str3 = '%s'\n", str3);
    
    // String concatenation
    strcat(str3, " ");
    strcat(str3, str2);
    printf("After strcat: str3 = '%s'\n", str3);
    
    // String comparison
    if (strcmp(str1, "Hello") == 0) {
        printf("str1 equals 'Hello'\n");
    }
    
    return 0;
}

6.3 Character Functions

Include <ctype.h> for character testing and conversion:

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'A';
    
    printf("Character testing for '%c':\n", ch);
    printf("isalpha: %d\n", isalpha(ch));      // Is alphabetic?
    printf("isdigit: %d\n", isdigit(ch));      // Is digit?
    printf("isupper: %d\n", isupper(ch));      // Is uppercase?
    printf("islower: %d\n", islower(ch));      // Is lowercase?
    printf("isspace: %d\n", isspace(ch));      // Is whitespace?
    
    printf("Lowercase: %c\n", tolower(ch));    // Convert to lowercase
    printf("Uppercase: %c\n", toupper('a'));   // Convert to uppercase
    
    return 0;
}