0

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.

3
  • len=sprintf(str,... Commented Feb 16, 2014 at 22:43
  • scanf("%lld", &inputNum); printf("%lld", inputNum); Commented Feb 16, 2014 at 23:37
  • print out the converted string and you'll know why Commented Feb 17, 2014 at 0:18

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.