Convert int to string in C

C-Plus-Plus-Programs

In this post, we are going to learn how to convert int to string in C with code examples and explanations by Using sprintf(),itoa() functions.

1. C sprintf() fuction


The C library sprintf() function allows us to convert integers into a formatted string.

Syntax

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

Parameters

  • str: It is a character array and pointer to buffer where the string will be stored.
  • format: It is a C string that specific output along with place holder.
  • arg1,arg2…argn : The integer input value converted to string.

Return Value

If the conversion is successful It returns the length of converted to number to string else negative value is a return.

1.2 Program sprint() to convert int to string in C


In this C Program, we have taken a number input from the user and mystr is an empty buffer of type string. We are using the sprint function to convert the input number to a string.

Finally using the printf() function to print the output on the console.

Using sprint() function to convert int to string in C

#include <stdio.h>

int main(void)
{
	int num;
    int len;
	char mystr[10]; 

	printf("Please enter a number: ");
	scanf("%d", &num);

    //converting num to string
	sprintf(mystr, "%d", num);  
   len = sprintf(mystr, "%d", num);   

	printf("\n Num to string: %s", mystr);
    printf("\n Length of converted string: %d ", len);

	return 0;
}

Output

Please enter a number: 14

 Num to string: 14
 Length of converted string: 2 

1.2.Program Using sprint() to convert int to string with base


In this C program example, we are using the sprintf() function to print the base value of int to string number.

C program to convert int to string with base

#include <stdio.h>

int main(void)
{
	int num;
    int len;
	char mystr[30];
		char mystr2[30]; 
		char str[30]; 

	printf("Please enter a number: ");
	scanf("%d", &num);

    //converting num to string
	sprintf(mystr, "%d", num);  
    
   
  
   

   printf("\n decimal value: %s", mystr);
    
   //hexadecimal value
  sprintf(mystr,"%x",num);
     
 printf("\n hexadecimal base : %s", mystr);
 //octal value
   sprintf(mystr,"%o",num);
 printf("\n octal value : %s", mystr);

	return 0;
}

Output

Please enter a number: 54312

 decimal value: 54312
 hexadecimal base : d428
 octal value : 152050

2. Using C itoa() function


The itoa() C function allows us to convert integer numbers into null-terminated strings.

Syntax

char *12  itoa ( int val, char * str, int base );

Parmeters

  • val: The integer number need to convert into string.
  • str : It is a character array and pointer to buffer in memory where null terminated result string will store.
  • Base : The base is a numeric value range from 2 to 32 categories as below
    • binary : if base is 2
    • octal : if base is 8
    • Decimal: if base is 10
    • Hex : base is 16

2.1 Using itoa() function to convert int to string in C


In this example, we are using itoa() function to convert int to string. This function is not defined in ANSI-C. But some compilers supports it.

C Program Example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
	int num;
	char mystr[30];
	int len;

	printf("Please enter a number: ");
	scanf("%d", &num);

	//converting num to string
	itoa(num, mystr, 2);
	printf("\n Binary value: %s", mystr);



	itoa(num, mystr, 8);
	printf("\n oct value: %s", mystr);

	itoa(num, mystr, 10);
	
	printf("\n Decimal value: %s", mystr);


	
	


	itoa(num, mystr, 16);
	printf("\n Hexa value: %s", mystr);
	

	return 0;
}

Output

Please enter a number: 54312

 Binary value: 1101010000101000
 oct value: 152050
 Decimal value: 54312
 Hexa value: d428

Summary

In this post we have learned how to convert int to string in C by using the following ways:

  • sprint() function to convert int to string in C
  • Using itoa() function to convert int to string in C