C Programming

Convert Int to String in C

Strings data types are everywhere in modern programming, from low-level programming languages to high-level languages that make a great effort to abstract the fundamental concepts. Therefore, you will find yourself working with strings in any programming language more times than not.

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:

int sprintf(char *str, const char *format, [arg1, arg2, ... ]);

The function accepts three main parameters:

  1. str – this specifies a pointer to a char data type.
  2. format – the format parameter allows you to specify the type of output with a placeholder.
  3. 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.

#include <stdio.h>

int main() {
  int var =100;
  char int_str[20];

  sprintf(int_str, "%d", var);
  printf("Var: %s\n", int_str);
  return 0;
}

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:

$ gcc to_string.c

$ ./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.

char* itoa(int num, char * buffer, int base)

The function takes three main parameters:

  1. num – this parameter refers to the int value that you wish to convert to a string.
  2. buffer – the buffer parameter is a pointer to a char data type to hold the converted string.
  3. 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.

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

int main() {

  int var =100;
  char int_string[20];

  itoa(var, int_string, 10);
  printf("Var: %s\n", int_string);

  return 0;
}

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!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list