![]() |
VOOZH | about |
In C, integers can be represented as strings with each digit represented by corresponding numeric character. In this article, we will learn how to convert integers into the string
Examples
Input: 1234
Output: "1234"
Explanation: The integer 1234 is converted to the string "1234".Input: -567
Output: "-567"
Explanation: The integer -567 is converted to the string "-567".
There are various methods that can be used to convert integer to a string in C:
We can convert an integer to a string manually by extracting each digit one by one and converting it to its corresponding character by using their ASCII code and storing it in the character array to form a string.
String: 1234
We can also use the sprintf function in C to convert an integer to a string. The working of sprintf() function is similar is similar to printf() but instead of printing the output it stores the formatted output into a character array(string buffer).
The integer 86 converted to string is: 86
Note: We can also use the snprintf function which is similar to sprintf but with a buffer size limit, which helps prevent buffer overflows.
itoa() is a non-standard function available in some C compilers like MSVC, etc. It stands for Integer TO ASCII. It converts the given integer value to a null-terminated string and stores the result in the character array( buffer) defined by string parameter.
Output
String: 1234