For that purpose, we will discuss quick and very beginner-friendly methods of converting an integer into a string in the C programming language.
Method 1 – Convert Int to String Using the Sprintf Function
The Sprintf function is one of the functions you can use to convert an integer value into a string. As the name suggests, the function will take any value and print it into a string.
It is very similar to the printf function. But instead of printing the value into the standard out, the function returns a formatted string which we can store in a variable and use later.
The function syntax is as shown below:
The function accepts three main parameters:
- str – this specifies a pointer to a char data type.
- format – the format parameter allows you to specify the type of output with a placeholder.
- args – this parameter specifies the integer values in which to convert into a string.
You will notice that the function returns an int type. This is because it returns a formatted string which is discarded, and a value of -1 if an error occurs.
Let us see this function in action.
In the code above, we start by importing the necessary header files. For the sprint() function, we require the standard input and output header file only.
We then open the main function and declare two variables. The first is the integer value that we wish to convert to a string.
The next variable is the character array that we will use to store the string value once we convert the int into a string.
We then use the sprint function and pass the char type, the format, and the int as the parameters.
Finally, we can print the resulting string using the printf function. The resulting output is a shown:
$ ./a.out
Var: 100
Method 2 – Convert Int to String With itoa() Function (non-standard)
There is another non-standard function in C that you can use to convert an int to a string. It is a simple type-casting function. It first appeared in the C Programming Language book.
However, as the book states, this function is non-standard and does not handle negative integers very well.
Since it is non-standard, attempting to compile the function will heavily depend on your OS and if the function is available.
However, we are developers, and experimenting is in our nature.
The following syntax shows how the itoa() function works.
The function takes three main parameters:
- num – this parameter refers to the int value that you wish to convert to a string.
- buffer – the buffer parameter is a pointer to a char data type to hold the converted string.
- base – refers to the conversion base.
The function then returns a string of the specified integer.
The code below illustrates how to use the itoa() function to convert an int to a string.
Here, we specify the conversion of the int to base 10.
Closing
In this article, we covered two main methods of converting an Int to a string in C. It is good to stick to the Sprintf function as it’s a standard function and can be exported across systems.
Thanks for reading, and Happy coding!!