0

You read the question I need to be able to scan a int value and print it back out as a string variable

3 Answers3

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,775
  • 1
  • 13
  • 15
  • 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,402
  • 8
  • 44
  • 71
  • 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
  • @MattMcNabb updated the answer. Yes, `snprintf` is a safer option. – ajay Apr 10 '14 at 12:34
0

YOu should use sprintf instead of itoa as it iota is not a starndard

int aInt;
scanf("%d", &aInt)
char str[50];
sprintf(str, "%d", aInt);

detail use of sprintf

Community
  • 1
  • 1
neel shah
  • 2,231
  • 15
  • 19