Reputation: 1
I'm fairly new to c and have been trying to figure out how convert an integer into a string of numbers, and this i what i have so far:
long long int inputNum = 0;
char str [20];
int len = 0;
printf("Please enter the integer to be converted,\nit should be no more than 10 digits long: \n");
scanf("%d", &inputNum);
sprintf(str,"%d", inputNum);
len = strlen(str);
printf("%d", len);
return 0;
However im encountering an issue were if the int entered is more than 10 characters long the program will still say that the lenght is 10. Im not sure if this is an issue with data types (long long int for example) or if im just implementing the string impoperly.
Upvotes: 0
Views: 171
Reputation: 13854
Your format specifier is wrong. %d
is used for regular int
variables. Yours is a long long int
. Use %lld
instead and it should work just fine.
Also, you should be aware that exactly how many digits an int
can store is platform-dependant. In your case, it's probably 32 bits, which works out to a range of approx -2^31
to 2^31
. Likewise, a long long
is 64 bits long, with analogous constraints. What I'm saying is that you're going to need a different solution for an arbitrary number of digits.
Upvotes: 3