This project has been created as part of the 42 curriculum by srosu.
keywords: Unix logic
skills: Imperative programming - Rigor - Algorithms & AI
Libft is the first core project of the 42 curriculum and consists of reimplementing a subset of the C standard library, along with additional utility functions and basic data structures.
The objective is to build a custom, reusable static library that will serve as a foundation for future projects. By recreating these functions from scratch, the project enforces a precise understanding of:
- Memory allocation and deallocation
- Pointer manipulation and data representation
- String processing at a low level
- Robust error handling and edge-case management
This library replaces reliance on external standard functions and ensures full control over behavior, performance, and reliability. It also establishes rigorous coding habits that are essential for systems programming.
The project uses a Makefile to automate compilation and maintenance tasks.
Available rules:
-
makeCompiles all source files and produces the static librarylibft.a. -
make cleanRemoves all intermediate object files (.o), keeping the compiled library intact. -
make fcleanRemoves both object files and the final library (libft.a). -
make rePerforms a full rebuild by executingfcleanfollowed bymake.
No main.c is provided in this project, as libft is intended to be used as a reusable library.
To test or use the library, you can create your own main.c and compile it manually with the generated object files:
cc -Wall -Wextra -Werror main.c *.oAlternatively, you can link against the compiled static library:
cc -Wall -Wextra -Werror main.c -L. -lftMake sure to include the header in your source file:
#include "libft.h"This section focuses on reproducing standard C library behavior without using the original implementations.
-
ft_isalpha,ft_isdigit,ft_isalnum,ft_isascii,ft_isprintPerform character classification based on ASCII value ranges. -
ft_strlenComputes string length by iterating until the null terminator. -
ft_memset,ft_bzeroInitialize memory regions with a specific value or zero. -
ft_memcpy,ft_memmoveCopy raw memory.ft_memmovehandles overlapping memory safely by adjusting the copy direction. -
ft_strlcpy,ft_strlcatProvide size-aware string copy and concatenation, preventing buffer overflow. -
ft_toupper,ft_tolowerConvert character case based on ASCII transformations. -
ft_strchr,ft_strrchrLocate the first or last occurrence of a character in a string. -
ft_strncmp,ft_memcmpCompare strings or memory blocks lexicographically. -
ft_memchrScan a memory region for a specific byte. -
ft_strnstrLocate a substring within a bounded length. -
ft_atoiConvert a string to an integer, handling whitespace, sign, and potential overflow conditions. -
ft_callocAllocate memory and initialize it to zero, ensuring safe default values. -
ft_strdupAllocate and duplicate a string into a new memory buffer.
These functions extend the library with higher-level operations.
-
ft_substrExtract a substring from a given string, ensuring bounds safety. -
ft_strjoinConcatenate two strings into a newly allocated buffer. -
ft_strtrimRemove specified characters from the beginning and end of a string. -
ft_splitDivide a string into an array of substrings based on a delimiter. This function requires careful handling of multiple allocations and cleanup in case of failure. -
ft_itoaConvert an integer into its string representation, including negative values. -
ft_strmapiApply a transformation function to each character, producing a new string. -
ft_striteriIterate over a string and apply a function in place. -
ft_putchar_fd,ft_putstr_fd,ft_putendl_fd,ft_putnbr_fdOutput characters, strings, and numbers to a specified file descriptor, enabling flexible I/O handling.
This section introduces singly linked lists using a generic structure:
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;t_list *ft_lstnew(void *content)
{
t_list *node;
node = (t_list *)malloc(sizeof(t_list));
if (!node)
return (NULL);
node->content = content;
node->next = NULL;
return (node);
}This function allocates a new node, initializes its content, and sets the next pointer to NULL, establishing the base element of a linked list.
-
ft_lstadd_front,ft_lstadd_backInsert elements at the beginning or end of the list. -
ft_lstsizeCount the number of elements through traversal. -
ft_lstlastRetrieve the final node in the list. -
ft_lstdelone,ft_lstclearFree memory safely, ensuring no leaks remain. -
ft_lstiterApply a function to each element in the list. -
ft_lstmapCreate a new list by applying a transformation to each element, with proper cleanup in case of allocation failure.
-
ISO/IEC JTC 1/SC 22/WG14 — C Standard Draft (N3220): https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf
-
mycodeschool — explanations of algorithms and data structures
-
Stack Overflow — discussions on:
AI tools such as ChatGPT and Claude were used as supplementary resources to clarify certain concepts, validate approaches, and assist with documentation.
All implementations and design choices were carried out independently.