Simvik

Integer to string

Format a signed 32-bit integer as decimal text without printf. A medium embedded C problem (Arrays and strings), run and graded in the browser.

medium · Arrays and strings · formatting · itoa

Write the decimal representation of v to out, with a leading - for negative values, and terminate it. out has room for 12 bytes, which is enough for any int32_t.

Starter code

void int_to_str(int32_t v, char *out) {
  // 0 -> "0", -123 -> "-123"
  out[0] = '\0';
}

Example tests

Zero

char s[12];
int_to_str(0, s);
printf("%s\n", s);

Expected output:

0

Negative number

char s[12];
int_to_str(-123, s);
printf("%s\n", s);

Expected output:

-123

Learn it first

  • Arrays and strings · Decay, bounds, safe copying, and parsing and formatting without the heap.

More Arrays and strings problems