A custom C library implementing standard library functions
- About
- Project Objectives
- Functions Implemented
- Compilation
- Usage
- Testing
- Project Structure
- 42 School Standards
Libft is the first project at 42 School where students recreate essential C standard library functions from scratch. This project serves as the foundation for all future C projects at 42, teaching low-level programming concepts, memory management, and the implementation details of commonly used functions.
The library provides a comprehensive set of utility functions for string manipulation, memory management, character validation, linked list operations, and more.
- Understand standard library functions by implementing them from scratch
- Learn memory management and proper allocation/deallocation practices
- Master pointer manipulation and understanding of C data structures
- Develop debugging skills and attention to detail
- Follow strict coding standards (42 Norm)
- Create reusable code that can be used in future 42 projects
Recreation of existing C standard library functions
| Function | Description |
|---|---|
ft_isalpha |
Check if character is alphabetic |
ft_isdigit |
Check if character is a digit |
ft_isalnum |
Check if character is alphanumeric |
ft_isascii |
Check if character is ASCII |
ft_isprint |
Check if character is printable |
ft_toupper |
Convert to uppercase |
ft_tolower |
Convert to lowercase |
| Function | Description |
|---|---|
ft_strlen |
Calculate string length |
ft_strchr |
Locate character in string |
ft_strrchr |
Locate character in string (reverse) |
ft_strncmp |
Compare strings up to n characters |
ft_strlcpy |
Size-bounded string copying |
ft_strlcat |
Size-bounded string concatenation |
ft_strnstr |
Locate substring in string |
ft_atoi |
Convert string to integer |
| Function | Description |
|---|---|
ft_memset |
Fill memory with constant byte |
ft_bzero |
Zero a byte string |
ft_memcpy |
Copy memory area |
ft_memmove |
Copy memory area (handles overlap) |
ft_memchr |
Scan memory for character |
ft_memcmp |
Compare memory areas |
ft_calloc |
Allocate and zero memory |
ft_strdup |
Duplicate string |
New functions not in the standard library
| Function | Description |
|---|---|
ft_substr |
Extract substring |
ft_strjoin |
Concatenate two strings |
ft_strtrim |
Trim characters from string |
ft_split |
Split string by delimiter |
ft_itoa |
Convert integer to string |
ft_strmapi |
Apply function to each character |
ft_striteri |
Apply function to each character (with index) |
ft_putchar_fd |
Output character to file descriptor |
ft_putstr_fd |
Output string to file descriptor |
ft_putendl_fd |
Output string + newline to file descriptor |
ft_putnbr_fd |
Output number to file descriptor |
Functions for manipulating linked lists
| Function | Description |
|---|---|
ft_lstnew |
Create new list element |
ft_lstadd_front |
Add element at beginning of list |
ft_lstsize |
Count elements in list |
ft_lstlast |
Return last element of list |
ft_lstadd_back |
Add element at end of list |
ft_lstdelone |
Delete single element |
ft_lstclear |
Delete and free list |
ft_lstiter |
Iterate through list |
ft_lstmap |
Apply function to each element |
- GCC compiler
- Make
# Compile the main library
make
# Compile with bonus functions
make bonus
# Clean object files
make clean
# Clean everything (objects + library)
make fclean
# Recompile everything
make reThe library is compiled with strict flags to ensure code quality:
-Wall -Werror -Wextra- Include the header in your C file:
#include "libft.h"- Compile your program with the library:
gcc -Wall -Wextra -Werror your_program.c -L. -lft -o your_program#include "libft.h"
#include <stdio.h>
int main(void)
{
char *str = "Hello, 42!";
char *substr;
char **split_result;
int i;
// String length
printf("Length of '%s': %zu\n", str, ft_strlen(str));
// Substring extraction
substr = ft_substr(str, 7, 2);
printf("Substring: %s\n", substr);
free(substr);
// String splitting
split_result = ft_split(str, ' ');
i = 0;
while (split_result[i])
{
printf("Word %d: %s\n", i, split_result[i]);
free(split_result[i]);
i++;
}
free(split_result);
return (0);
}Create test files to verify function behavior:
# Create a simple test file
cat > test_libft.c << 'EOF'
#include "libft.h"
#include <stdio.h>
#include <string.h>
int main(void)
{
// Test ft_strlen vs strlen
char *test = "Test string";
printf("ft_strlen: %zu, strlen: %zu\n", ft_strlen(test), strlen(test));
// Test ft_strchr vs strchr
printf("ft_strchr: %s, strchr: %s\n", ft_strchr(test, 's'), strchr(test, 's'));
return (0);
}
EOF
# Compile and run
gcc -Wall -Wextra -Werror test_libft.c -L. -lft -o test_libft
./test_libftPopular testing frameworks for libft:
libft/
βββ Makefile # Build configuration
βββ libft.h # Header file with function prototypes
βββ README.md # Project documentation
βββ ft_*.c # Source files for each function
βββ libft.a # Compiled static library (after make)
ft_is*.c- Character classification functionsft_to*.c- Character conversion functionsft_str*.c- String manipulation functionsft_mem*.c- Memory operation functionsft_put*.c- Output functionsft_lst*.c- Linked list functions (bonus)
This project follows the strict 42 Norm coding standard:
- β Maximum 25 lines per function
- β Maximum 80 characters per line
- β No global variables
- β No for loops (only while)
- β Proper variable declarations
- β Consistent indentation and formatting
- β Comprehensive error handling
- β No memory leaks
All functions that allocate memory:
- Return
NULLon allocation failure - Properly handle edge cases
- Are tested for memory leaks with
valgrind
Through this project, I gained experience in:
- Low-level C programming and system calls
- Memory management and debugging memory issues
- Algorithm implementation and optimization
- Code organization and modular programming
- Testing methodologies and edge case handling
- Documentation and code maintenance
This project is part of the 42 School curriculum. Code is provided for educational purposes.
Author: cadenegr
School: 42 Berlin
Project: libft
Date: June 2023