#include <unistd.h>
void int_to_str(int num, char *buffer, size_t buf_size) {
    char digits[12];  // Max number of digits for a 32-bit int is 10, plus sign and null terminator
char *ptr = digits + sizeof(digits) - 1;

    if (num < 0) {
        *(ptr--) = '-';
        num = -num;
    }

    if (num == 0) {
        *(ptr--) = '0';
    } else {
        while (num > 0) {
            *(ptr--) = '0' + (num % 10);
            num /= 10;
        }
    }

    size_t str_len = sizeof(digits) - (ptr - digits);  // Calculate the length of the string representation
// Check if the buffer size is sufficient
if (str_len >= buf_size) {
        // Handle buffer overflow, e.g., truncate the string or signal an error
*buffer = '\0';
        return;
    }

    // Copy the string representation to the buffer
while (ptr >= digits) {
        *(buffer++) = *(ptr++);
    }

    // Null-terminate the string
*buffer = '\0';
}

size_t my_strlen(const char *str) {
    size_t len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}

int main() {
    int value = 12345;
    char buffer[12];
/*
    int_to_str(value, buffer, sizeof(buffer));

    // Output the string representation of the integer to the standard output
write(1, buffer, my_strlen(buffer));
    write(1, "\n", 1);
*/
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: