Converting a long integer to a string involves converting its numeric value into a sequence of characters stored in a character array. This can be done using standard library functions or manual digit extraction.
- Functions such as sprintf() and snprintf() convert a long integer into its string representation.
- Manual conversion extracts individual digits and stores them as characters in an array.
Examples:
Input: num = 123456789L
Output: "123456789"
Explanation: The long integer is converted into a string containing its digits.Input: num = -98765L
Output: "-98765"
Explanation: The negative long integer is converted into a string while preserving the minus sign.
Ways to Convert Long to String
There are different ways to convert a long integer into a string in C:
1. Using sprintf()
The sprintf() function converts a long integer into a string using the %ld format specifier. It stores the formatted result in a character array.
#include <stdio.h>
int main(){
long num = 123456789L;
char str[32];
// Convert long to string
sprintf(str, "%ld", num);
printf("String: %s\n", str);
return 0;
}
Output
String: 123456789
Explanation:
- The variable num stores the long integer 123456789.
- sprintf() converts the value into a string using %ld.
- The converted string is stored in str and displayed using %s.
Note: sprintf() does not check the destination buffer size. Ensure that the character array has enough space to store the complete result, including the null terminator.
2. Using snprintf()
The snprintf() function converts a long integer into a string while limiting the number of characters written to the destination array. It helps prevent buffer overflow.
#include <stdio.h>
int main(){
long num = -987654321L;
char str[32];
// Convert long to string safely
int result = snprintf(str, sizeof(str), "%ld", num);
if (result < 0 || (size_t)result >= sizeof(str)){
printf("Conversion failed\n");
return 1;
}
printf("String: %s\n", str);
return 0;
}
Output
String: -987654321
Explanation:
- The variable num stores the negative long integer -987654321.
- snprintf() converts the number into a string using %ld.
- sizeof(str) specifies the maximum buffer capacity.
- The return value is checked to detect formatting errors or insufficient space.
3. Using a Custom Algorithm
A long integer can also be converted into a string manually by extracting its digits using the modulo (%) and division (/) operators. The extracted digits are stored in reverse order and then rearranged to form the correct string.
Algorithm
- Initialize a character array and an index to store digits.
- Check whether the number is negative and record its sign.
- Extract the last digit using num % 10.
- Convert the digit into a character and store it in the array.
- Divide the number by 10 and repeat until it becomes zero.
- Add the minus sign if required, reverse the characters, and append the null terminator.
#include <stdio.h>
int main(){
long num = -12345;
char str[32];
int i = 0;
int negative = (num < 0);
// Extract digits in reverse order
do{
int digit = (int)(num % 10);
if (digit < 0)
digit = -digit;
str[i++] = (char)(digit + '0');
num /= 10;
} while (num != 0);
// Add negative sign
if (negative)
str[i++] = '-';
str[i] = '\0';
// Reverse the string
for (int j = 0; j < i / 2; j++){
char temp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = temp;
}
printf("String: %s\n", str);
return 0;
}
Output
String: -12345
Explanation:
- The variable num stores the long integer -12345.
- The modulo operator extracts each digit, and adding '0' converts it into a character.
- The extracted digits are stored in reverse order in str.
- The negative sign is added before reversing the array to obtain "-12345".
Important Points
- The %ld format specifier is used to convert a signed long integer into a decimal string.
- A character array must have enough space for all digits, an optional minus sign, and the null terminator ('\0').
- snprintf() is generally preferred over sprintf() because it limits the number of characters written.
- A custom algorithm is useful for understanding how numeric digits are converted into characters.
- The resulting string can be processed using standard string functions such as strlen() and strcmp().