How to convert integer or float or double type to string type in C

Convert Integer or Float or Double to String

In C programming, an integer or a float or a double can be converted into a string using the sprintf() function

The sprintf() function is used to format and store a series of characters and values in a string buffer. It stands for "String Print Formatted".

Different format specifiers are used to convert different data types.

The sprintf() takes 3 arguments: string buffer like char array, format specifier, and data to be formatted/converted.

Convert Integer into String in C

In C programming, use "%d" format specifier to convert an integer into a string using the sprintf() function as demontrated in the following example:

Examples of Converting Integer into String

Example explained
  • int intNum = 1225;: The line declares an integer variable named 'intNum' and assigns value 1225.
  • char stringVar[10];: It declares a char array variable named 'stringVar'.
  • sprintf(stringVar, "%d", intNum);: It converts the integer value of 'intNum' variable into string type and stores converted value in the 'stringVar'. "%d" is the format specifier for integer data type.
  • printf("%s is a converted string.", stringVar);: It prints the converted string data.

Convert Float into String in C

In C programming, use "%f" format specifier to convert a float type into a string type using the sprintf() function as demontrated in the following example:

Examples of Converting Float into String

Example explained
  • float floatNum = 12.314;: The line declares a float variable named 'floatNum' and assigns value 12.314.
  • char stringfloatVar[10];: It declares a char array variable named 'stringfloatVar'.
  • sprintf(stringfloatVar, "%f", floatNum);: It converts the float value of 'floatNum' variable into string type and stores converted value in the 'stringfloatVar'. "%f" is the format specifier for float data type.
  • printf("%s is a converted string.", stringfloatVar);: It prints the converted string data.

Convert Double into String in C

In C programming, use "%lf" format specifier to convert a double type into a string type using the sprintf() function as demontrated in the following example:

Examples of Converting Double into String

Example explained
  • double doubleNum = 12.314;: The line declares a double variable named 'doubleNum' and assigns value 12.314.
  • char stringdoubleVar[10];: It declares a char array variable named 'stringdoubleVar'.
  • sprintf(stringdoubleVar, "%lf", doubleNum);: It converts the double value of 'doubleNum' variable into string type and stores converted value in the 'stringdoubleVar'. "%lf" is the format specifier for double data type.
  • printf("%s is a converted string.", stringdoubleVar);: It prints the converted string data.

Live Code Playground

In the following C code editor, you can practice by converting more data types to string. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output