98. Convert Integer to String

#include <stdio.h>

// Convert integer to string and print each character with space
void print_integer_as_string(int num) {
    if (num == 0) {
        printf("0");
        return;
    }

    char buffer[12];  // Enough for -32768 to 32767
    int i = 0;
    int is_negative = 0;

    if (num < 0) {
        is_negative = 1;
        num = -num;
    }

    // Convert digits in reverse
    while (num > 0) {
        buffer[i++] = (num % 10) + '0';
        num /= 10;
    }

    if (is_negative) {
        buffer[i++] = '-';
    }

    // Print characters in reverse order with space
    for (int j = i - 1; j >= 0; j--) {
        printf("%c", buffer[j]);
        if (j > 0) printf(" ");
    }
}

int main() {
    int num;
    scanf("%d", &num);
    print_integer_as_string(num);
    return 0;
}

Why It’s Important in Firmware?

  • Required for displaying numbers on UART, LCD, or serial logs 
  • Avoids dependency on heavy libraries in bare-metal systems 
  • Builds deep understanding of integer encoding and display formatting 

Solution Logic

  • Handle Zero Case Directly
    If the input number is 0, we immediately print "0" and return.
    This avoids extra logic for zero inside the main loop.
     
  • Check for Negative Numbers
    If the input number is negative:
    • Set a flag is_negative = 1 
    • Convert it to positive by negating (num = -num) 
    • This allows us to treat all digits uniformly in the next steps.
       
  • Extract Digits from Least Significant to Most
    • Use a loop: num % 10 gives the last digit 
    • Convert digit to character: add '0' → (num % 10) + '0' 
    • Store each digit in a buffer in reverse order (from least to most significant)
       
  • Append Negative Sign if Needed
    • If the original number was negative, add '-' to the buffer after digits
       
  • Print Characters in Reverse
    • Since digits were stored in reverse, we loop backward through the buffer 
    • Print each character, with a space between them for clarity

       
Loading...

Input

123

Expected Output

1 2 3