-
Notifications
You must be signed in to change notification settings - Fork 0
Libc
Mimics
strlenfunction:
Calculates the length of a string, i.e., the number of characters before the null terminator (\0).
The null terminator is not included in the length.
| Library | <string.h> |
|---|---|
| Signature | size_t strlen(const char *s); |
| Parameters |
s: Pointer to the string whose length will be counted. |
| Return | The length of the string as a size_t value, excluding the null terminator (\0). |
| Manual | strlen(3) - Linux Manual |
size_t len = strlen("Hello, World!"); // Returns 13
size_t len_empty = strlen(""); // Returns 0
size_t len_null_char = strlen("\0"); // Returns 0No explicit check for
NULLis made in the original function.
char *str = NULL;
size_t len = strlen(str); // Undefined behaviorNo explicit check for the null terminator (
\0) at the end of the buffer is made in the original function.
char buffer[] = {'H', 'e', 'l', 'l', 'o'}; // Without '\0' at the end
size_t len = strlen(buffer); // Undefined behaviorMimics
strlcpyfunction:
Copies a string fromsrctodst, ensuring the destination buffer doesn't overflow (dstsize - 1)
and guaranteeing null-termination ifdstsizeis not 0.
| Library | <bsd/string.h> |
|---|---|
| Signature | size_t strlcpy(char *dst, const char *src, size_t dstsize); |
| Parameters |
dst: Pointer to the destination buffer. |
src: Pointer to the source string to be copied. |
|
dstsize: Size of the destination buffer. |
|
| Return | The total length of the source string (src). |
| Manual | strlcpy(3) - Linux Manual |
char dest[5];
size_t len = strlcpy(dest, "Hello", sizeof(dest)); // Copies "Hell", len returns 5Ensures null-termination if
dstsize > 0.
char dest[5];
size_t len = strlcpy(dest, "Hello", sizeof(dest)); // Copies "Hell\0"Mimics
strlcatfunction:
Appends a string fromsrctodst, ensuring the destination buffer doesn't overflow (dstsize - 1)
and guaranteeing null-termination ifdstsizeis not 0.
| Library | <bsd/string.h> |
|---|---|
| Signature | size_t strlcat(char *dst, const char *src, size_t dstsize); |
| Parameters |
dst: Pointer to the destination string buffer. |
src: Pointer to the source string. |
|
dstsize: Size of the destination buffer. |
|
| Return | The total length of the string it tried to create. |
| Manual | strlcat(3) - Linux Manual |
char dest[10] = "Hi";
size_t len = strlcat(dest, " there", sizeof(dest)); // Returns total length attempted (8)Ensures null-termination if
dstsize > 0and destination space is available.
char dest[5];
size_t len = strlcat(dest, "Hello", sizeof(dest)); // Copies "Hel\0"Mimics
strchrfunction:
Locates the first occurrence of a charactercin the strings.
| Library | <string.h> |
|---|---|
| Signature | char *strchr(const char *s, int c); |
| Parameters |
s: Pointer to the string to search. |
c: Character to locate. |
|
| Return | A pointer to the first occurrence of c, or NULL if not found. |
| Manual | strchr(3) - Linux Manual |
char *ptr = strchr("Hello", 'e'); // Returns pointer to 'e'If
cis'\0',strchrreturns a pointer to the null terminator.
char *ptr = strchr("Hello", '\0'); // Returns pointer to null terminatorMimics
strrchrfunction:
Locates the last occurrence of a charactercin the strings.
| Library | <string.h> |
|---|---|
| Signature | char *strrchr(const char *s, int c); |
| Parameters |
s: Pointer to the string to search. |
c: Character to locate. |
|
| Return | A pointer to the last occurrence of c, or NULL if not found. |
| Manual | strrchr(3) - Linux Manual |
char *ptr = strrchr("Hello", 'l'); // Returns pointer to the second 'l'If
cis'\0',strrchrreturns a pointer to the null terminator.
char *ptr = strrchr("Hello", '\0'); // Returns pointer to null terminatorMimics
strncmpfunction:
Compares up toncharacters of two strings.
| Library | <string.h> |
|---|---|
| Signature | int strncmp(const char *s1, const char *s2, size_t n); |
| Parameters |
s1: First string. |
s2: Second string. |
|
n: Maximum number of characters to compare. |
|
| Return | Integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2. |
| Manual | strncmp(3) - Linux Manual |
int result = strncmp("Hello", "World", 3); // Compares first 3 chars, returns negativeMimics
strnstrfunction:
Locates the first occurrence of the substringlittleinbig, searching only the firstlencharacters.
| Library | <string.h> |
|---|---|
| Signature | strnstr(const char *big, const char *little, size_t len); |
| Parameters |
big: String to search in. |
little: Substring to find. |
|
len: Maximum number of characters to search. |
|
| Return | Pointer to the beginning of the located substring, or NULL if not found. |
| Manual | strnstr(3) - FreeBSD Manual |
char *result = strnstr("Hello, World!", "World", 12); // Finds "World"Mimics
strdupfunction:
Allocates memory and duplicates the given strings.
| Library | <string.h> |
|---|---|
| Signature | char *strdup(const char *s); |
| Parameters |
s: String to duplicate. |
| Return | Pointer to the newly allocated string, or NULL if memory allocation fails. |
| Manual | strdup(3) - Linux Manual |
char *copy = strdup("Hello"); // Allocates and copies "Hello" into a new string
strdupallocates memory, so the returned string must be freed after use.
char *copy = strdup("Hello");
free(copy); // Freeing the allocated memoryMimics
memsetfunction:
Fills a block of memory with a specified value.
| Library | <string.h> |
|---|---|
| Signature | void *memset(void *s, int c, size_t n); |
| Parameters |
s: Pointer to the memory block. |
c: Value to set (converted to unsigned char). |
|
n: Number of bytes to set. |
|
| Return | Pointer to the memory block s. |
| Manual | memset(3) - Linux Manual |
char buffer[10];
memset(buffer, 0, 10); // Fills buffer with zerosMimics
bzerofunction:
Zeroes out a block of memory.
| Library | <strings.h> |
|---|---|
| Signature | void bzero(void *s, size_t n); |
| Parameters |
s: Pointer to the memory block. |
n: Number of bytes to set to zero. |
|
| Return | None. |
| Manual | bzero(3) - Linux Manual |
char buffer[10];
bzero(buffer, 10); // Sets all bytes in buffer to zero
bzerois considered deprecated in favor ofmemset, which achieves the same result.
Copiar código
bzero(buffer, 10); // Equivalent to memset(buffer, 0, 10);Mimics
memcpyfunction:
Copiesnbytes from source to destination.
| Library | <string.h> |
|---|---|
| Signature | void *memcpy(void *dest, const void *src, size_t n); |
| Parameters |
dest: Destination memory block. |
src: Source memory block. |
|
n: Number of bytes to copy. |
|
| Return | Pointer to the destination memory block dest. |
| Manual | memcpy(3) - Linux Manual |
char src[10] = "Hello";
char dest[10];
memcpy(dest, src, 5); // Copies "Hello" to dest
memcpyhas undefined behavior if source and destination overlap. Usememmoveinstead for overlapping memory.
memcpy(src + 1, src, 5); // Undefined behaviorMimics
memmovefunction:
Safely copiesnbytes from source to destination, even if memory regions overlap.
| Library | <string.h> |
|---|---|
| Signature | void *memmove(void *dest, const void *src, size_t n); |
| Parameters |
dest: Destination memory block. |
src: Source memory block. |
|
n: Number of bytes to copy. |
|
| Return | Pointer to the destination memory block dest. |
| Manual | memmove(3) - Linux Manual |
char src[10] = "Hello";
memmove(src + 1, src, 5); // Safely copies "Hello" to "HHello"Mimics
memchrfunction:
Searches for the first occurrence of a byte in a memory block.
| Library | <string.h> |
|---|---|
| Signature | void *memchr(const void *s, int c, size_t n); |
| Parameters |
s: Pointer to the memory block. |
c: Byte to search for (converted to unsigned char). |
|
n: Number of bytes to search. |
|
| Return | Pointer to the first occurrence of the byte c, or NULL if not found. |
| Manual | memchr(3) - Linux Manual |
char data[] = "Hello, World!";
char *ptr = memchr(data, 'W', sizeof(data)); // Returns pointer to 'W'Mimics
memcmpfunction:
Compares the firstnbytes of two memory blocks.
| Library | <string.h> |
|---|---|
| Signature | int memcmp(const void *s1, const void *s2, size_t n); |
| Parameters |
s1: First memory block. |
s2: Second memory block. |
|
n: Number of bytes to compare. |
|
| Return | Integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2. |
| Manual | memcmp(3) - Linux Manual |
int result = memcmp("ABC", "ABD", 3); // Returns a negative valueMimics
callocfunction:
Allocates memory for an array ofnmembelements ofsizebytes each and initializes the allocated memory to zero.
| Library | <stdlib.h> |
|---|---|
| Signature | void *calloc(size_t nmemb, size_t size); |
| Parameters |
nmemb: Number of elements to allocate. |
size: Size of each element. |
|
| Return | Pointer to the allocated memory, or NULL if the allocation fails. |
| Manual | calloc(3) - Linux Manual |
int *arr = calloc(5, sizeof(int)); // Allocates and zeroes memory for an array of 5 integersIf
nmemborsizeis 0,callocreturns a unique pointer that can be passed tofree().
int *arr = calloc(0, sizeof(int)); // Returns a unique pointer, safe to pass to free()If the multiplication of
nmembandsizeresults in an integer overflow,callocreturnsNULL.
size_t nmemb = SIZE_MAX;
size_t size = 2;
void *ptr = calloc(nmemb, size); // Returns NULL due to integer overflowMimics
isalphafunction:
Checks whether the passed character is an alphabetic letter.
| Library | <ctype.h> |
|---|---|
| Signature | int isalpha(int c); |
| Parameters |
c: Character to be checked. |
| Return | Non-zero if the character is an alphabetic letter, zero otherwise. |
| Manual | isalpha(3) - Linux Manual |
int result = isalpha('A'); // Returns non-zero
int result = isalpha('1'); // Returns 0Mimics
isdigitfunction:
Checks whether the passed character is a decimal digit (0-9).
| Library | <ctype.h> |
|---|---|
| Signature | int isdigit(int c); |
| Parameters |
c: Character to be checked. |
| Return | Non-zero if the character is a decimal digit (0-9), zero otherwise. |
| Manual | isdigit(3) - Linux Manual |
int result = isdigit('5'); // Returns non-zero
int result = isdigit('a'); // Returns 0Mimics
isalnumfunction:
Checks whether the passed character is alphanumeric (either a digit or a letter).
| Library | <ctype.h> |
|---|---|
| Signature | int isalnum(int c); |
| Parameters |
c: Character to be checked. |
| Return | Non-zero if the character is alphanumeric, zero otherwise. |
| Manual | isalnum(3) - Linux Manual |
int result = isalnum('A'); // Returns non-zero
int result = isalnum('1'); // Returns non-zero
int result = isalnum('$'); // Returns 0Mimics
isasciifunction:
Checks whether the passed character is an ASCII character (0-127).
| Library | <ctype.h> |
|---|---|
| Signature | int isascii(int c); |
| Parameters |
c: Character to be checked. |
| Return | Non-zero if the character is an ASCII character, zero otherwise. |
| Manual | isascii(3) - Linux Manual |
int result = isascii(65); // Returns non-zero for 'A'
int result = isascii(200); // Returns 0Mimics
isprintfunction:
Checks whether the passed character is printable, including space.
| Library | <ctype.h> |
|---|---|
| Signature | int isprint(int c); |
| Parameters |
c: Character to be checked. |
| Return | Non-zero if the character is printable, zero otherwise. |
| Manual | isprint(3) - Linux Manual |
int result = isprint(65); // Returns non-zero for 'A'
int result = isprint(127); // Returns 0 (non-printable)Mimics
toupperfunction:
Converts a lowercase alphabetic character to uppercase.
| Library | <ctype.h> |
|---|---|
| Signature | int toupper(int c); |
| Parameters |
c: Character to be converted. |
| Return | Uppercase equivalent if c is lowercase; otherwise returns c. |
| Manual | toupper(3) - Linux Manual |
int upper = toupper('a'); // Returns 'A'
int unchanged = toupper('A'); // Returns 'A'Mimics
tolowerfunction:
Converts an uppercase alphabetic character to lowercase.
| Library | <ctype.h> |
|---|---|
| Signature | int tolower(int c); |
| Parameters |
c: Character to be converted. |
| Return | Lowercase equivalent if c is uppercase; otherwise returns c. |
| Manual | tolower(3) - Linux Manual |
int lower = tolower('A'); // Returns 'a'
int unchanged = tolower('a'); // Returns 'a'Mimics
atoifunction:
Converts a string to an integer, ignoring leading whitespace and stopping conversion at the first non-numeric character.
| Library | <stdlib.h> |
|---|---|
| Signature | int atoi(const char *str); |
| Parameters |
str: The string to convert. |
| Return | The integer value of the string, or 0 if no valid conversion is performed. |
| Manual | atoi(3) - Linux Manual |
int number = atoi("42"); // Returns 42
int number = atoi(" -42"); // Returns -42
int number = atoi("42abc"); // Returns 42
atoidoes not handle overflow or underflow. For large inputs, it may result in undefined behavior.
int number = atoi("999999999999999999999999"); // Undefined behavior, may overflowPrevious ⬅️ • Top ⬆️ • Next ➡️