c long to string

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

// Function to convert a long integer to a string
char* longToString(long num);

int main() {
    // Example usage
    long number = 1234567890;
    char* str = longToString(number);

    // Display the result
    printf("The string representation of %ld is: %s\n", number, str);

    // Free the allocated memory
    free(str);

    return 0;
}

// Function to convert a long integer to a string
char* longToString(long num) {
    // Determine the number of digits in the long integer
    int numDigits = snprintf(NULL, 0, "%ld", num);

    // Allocate memory for the string representation, including space for the null terminator
    char str = (char)malloc((numDigits + 1) * sizeof(char));

    // Convert the long integer to a string and store it in the allocated memory
    snprintf(str, numDigits + 1, "%ld", num);

    return str;
}