Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Standard Library Functions in C

Introduction

The C Standard Library provides a collection of functions, macros, and types that allow developers to perform common tasks such as input/output operations, memory management, string manipulation, and more. These functions are organized into various header files, each serving a specific purpose.

Input/Output Functions

The stdio.h header file contains functions for standard input and output operations. Some of the commonly used functions are:

  • printf: Prints formatted output to the standard output (usually the screen).
  • scanf: Reads formatted input from the standard input (usually the keyboard).
  • fopen: Opens a file and returns a file pointer.
  • fclose: Closes a file.
  • fread: Reads data from a file.
  • fwrite: Writes data to a file.

Example: Using printf and scanf

#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("You are %d years old.\n", age);
    return 0;
}
                    
Enter your age: 25
You are 25 years old.
                    

String Manipulation Functions

The string.h header file contains functions for manipulating strings. Some of the commonly used functions are:

  • strlen: Returns the length of a string.
  • strcpy: Copies one string to another.
  • strcat: Concatenates two strings.
  • strcmp: Compares two strings.
  • strchr: Finds the first occurrence of a character in a string.
  • strstr: Finds the first occurrence of a substring in a string.

Example: Using strlen and strcpy

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

int main() {
    char source[] = "Hello, World!";
    char destination[20];
    strcpy(destination, source);
    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);
    printf("Length of source: %zu\n", strlen(source));
    return 0;
}
                    
Source: Hello, World!
Destination: Hello, World!
Length of source: 13
                    

Memory Management Functions

The stdlib.h header file contains functions for dynamic memory allocation and other utility functions. Some of the commonly used functions are:

  • malloc: Allocates memory dynamically.
  • calloc: Allocates memory for an array and initializes it to zero.
  • realloc: Resizes previously allocated memory.
  • free: Frees dynamically allocated memory.

Example: Using malloc and free

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr;
    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    ptr = (int*) malloc(n * sizeof(int));
    if (ptr == NULL) {
        printf("Memory not allocated.\n");
        return 1;
    }

    printf("Memory successfully allocated using malloc.\n");

    for (i = 0; i < n; ++i) {
        ptr[i] = i + 1;
    }

    printf("The elements of the array are: ");
    for (i = 0; i < n; ++i) {
        printf("%d ", ptr[i]);
    }

    free(ptr);
    printf("\nMemory successfully freed.\n");

    return 0;
}
                    
Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1 2 3 4 5 
Memory successfully freed.
                    

Mathematical Functions

The math.h header file contains functions for performing mathematical operations. Some of the commonly used functions are:

  • sqrt: Returns the square root of a number.
  • pow: Returns the power of a number.
  • sin: Returns the sine of an angle.
  • cos: Returns the cosine of an angle.
  • tan: Returns the tangent of an angle.
  • log: Returns the natural logarithm of a number.

Example: Using sqrt and pow

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

int main() {
    double num = 16.0;

    printf("Square root of %.2f is %.2f\n", num, sqrt(num));
    printf("%.2f to the power of 2 is %.2f\n", num, pow(num, 2));

    return 0;
}
                    
Square root of 16.00 is 4.00
16.00 to the power of 2 is 256.00
                    

Conclusion

The C Standard Library is a powerful set of tools that can help you perform a wide range of tasks. This tutorial has covered some of the most commonly used functions, but there are many more available. Always refer to the official documentation for a complete list of functions and their usage.