Sai A Sai A
Updated date Feb 28, 2024
In this blog, we will learn simple methods to convert long integers to strings in C programming language. Explore sprintf(), snprintf(), and itoa() functions with code examples and explanations.

Introduction:

Converting a long integer to a string in C might seem like a daunting task, especially for beginners. However, with the right approach, it can be achieved efficiently. In this blog, we'll explore several methods to convert a long to a string in C.

Method 1: Using sprintf()

To convert a long to a string in C is by using the sprintf() function. This function takes a format string and any number of additional arguments, formatting them into a string. Here's how you can use sprintf() to convert a long to a string:

#include <stdio.h>

int main() {
    long num = 1234567890;
    char str[20]; // Make sure the buffer is large enough

    sprintf(str, "%ld", num);

    printf("String representation using sprintf(): %s\n", str);

    return 0;
}

Output

String representation using sprintf(): 1234567890
  • We declare a long integer num and initialize it with a value.
  • Next, we declare a character array str to store the string representation of num.
  • We use sprintf() to convert the long integer num to a string and store it in the str buffer.
  • Finally, we print the string representation using printf().

Method 2: Using snprintf()

Similar to sprintf(), snprintf() allows us to format and store a string, but with the added benefit of specifying the maximum number of characters to write. This helps prevent buffer overflow. Here's how you can use snprintf() for long to string conversion:

#include <stdio.h>

int main() {
    long num = 9876543210;
    char str[20]; // Make sure the buffer is large enough

    snprintf(str, sizeof(str), "%ld", num);

    printf("String representation using snprintf(): %s\n", str);

    return 0;
}

Output:

String representation using snprintf(): 9876543210
  • We declare a long integer num and initialize it with a value.
  • Similarly, we declare a character array str to store the string representation.
  • We use snprintf() to convert the long integer num to a string with a specified buffer size to prevent overflow.
  • Finally, we print the string representation using printf().

Method 3: Using itoa() (Non-Standard)

Another method is using the itoa() function, which is not standard in C but is commonly available in many C libraries. It directly converts an integer to a string. Here's how you can use itoa():

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

int main() {
    long num = 1357924680;
    char str[20]; // Make sure the buffer is large enough

    itoa(num, str, 10);

    printf("String representation using itoa(): %s\n", str);

    return 0;
}

Output:

String representation using itoa(): 1357924680
  • We include the <stdlib.h> header for the itoa() function.
  • We declare a long integer num and initialize it with a value.
  • Similarly, we declare a character array str to store the string representation.
  • We use itoa() to directly convert the long integer num to a string with a specified base (10 in this case).
  • Finally, we print the string representation using printf().

Conclusion:

In this blog, we have explored several methods to convert a long integer to a string in C. We discussed using sprintf(), snprintf(), and itoa() functions, providing code examples and explanations for each method. 

Comments (0)

There are no comments. Be the first to comment!!!