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. Sometimes it needs to convert string to int in C
- itoa() Function to Convert an Integer to a String in C.
- sprintf() Function to Convert an Integer to a String in C
1. Convert int to string in C using sprintf() function
The C library sprintf() function allows us to convert integers into a formatted string. In this example, we are going to make use of the sprintf() function from the C standard library. This function is just like a format specifier that takes the format in which we want to print the input.
Syntax
int sprintf(char *str, const char *formatspec, [arg1, arg2, ... ])
Parameters
- str: is a pointer to char str in which we will get output.
- formatspec : is used to specify the format which we want for output.
- arg1, arg2 are integers to convert into a 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, use 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 example, we are going to make use of the sprintf() function from the C standard library. This function is just like a format specifier that takes the format in which we want to print the input.
Syntax
int sprintf(char *str, const char *formatspec, [arg1, arg2, ... ])
Parameters
- str = is a pointer to a char str in which we will get output.
- formatspec = is is used to specify the format which we want for output.
- arg1, arg2 are integers to convert into a string.
C Program to Convert an Integer to a String using sprintf()
#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. Convert int to string in C Using itoa() function
The itoa() C function allows us to convert integer numbers into null-terminated strings. In this example, we are going to make use of the itoa() function. This function can be used by just including the header file in your program. This function accepts an integer and returns a null-terminated string.
Note:- This function is not a standard function and on the Linux Posix system it may not work. It will work on windows systems for sure.
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 support it.
C Program to Convert an Integer to a String in C by using itoa() Function
#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