You read the question I need to be able to scan a int value and print it back out as a string variable
Asked
Active
Viewed 190 times
3 Answers
0
If you just want to print it back, you can use the "%d" specifier when using printf. If you want to convert the integer value to a character string for other purposes, you can use itoa.
shebaw
- 1,575
- 12
- 14
-
1`itoa` is not a standard function. `sprintf` or `snprintf` would be a better idea. – M.M Apr 10 '14 at 12:11
0
You can use the sprintf function to do it.
int a;
// [log(2^63 - 1)] + 1 = 19 where
// [x] is the greatest integer <= x
// +1 for the terminating null byte
char s[19+1];
// read an integer
scanf("%d", &a);
// store the integer value as a string in s
sprintf(s, "%d", a);
ajay
- 9,041
- 7
- 37
- 68
-
May overflow `s`, I'd highly recommend using `snprintf`. Who's to say you won't upgrade compiler at some point in future and get a 64-bit int? – M.M Apr 10 '14 at 12:12
-