Tuesday, January 25, 2011

Making an int into a char* (string) Part 1

I need to make a function that will convert an integer into a c string.  Now, if it hadn't been shown in class, I would have just used the following:

const char* my_itoa(int x) {
  static char c[21];
  sprintf(c,"%d", x);
  return c;
}

But since it has, I essentially have to code what sprintf() is doing from scratch.

No comments:

Post a Comment