This function is almost identical to itoa. Consider refactoring to avoid code duplication
| 50 | // This function is almost identical to itoa. |
| 51 | // Consider refactoring to avoid code duplication |
| 52 | void llitoa(long long int num, char *str) { |
| 53 | int i = 0; |
| 54 | int isNegative = 0; |
| 55 | |
| 56 | if (num == 0) { |
| 57 | str[i++] = '0'; |
| 58 | str[i] = '\0'; |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (num < 0) { |
| 63 | isNegative = 1; |
| 64 | num = -num; |
| 65 | } |
| 66 | |
| 67 | while (num != 0) { |
| 68 | int rem = num % 10; |
| 69 | str[i++] = TO_DIGIT(rem); // Use macro |
| 70 | num = num / 10; |
| 71 | } |
| 72 | |
| 73 | if (isNegative) { |
| 74 | str[i++] = '-'; |
| 75 | } |
| 76 | |
| 77 | // Reverse the string (could be optimized) |
| 78 | for (int start = 0, end = i - 1; start < end; start++, end--) { |
| 79 | char temp = str[start]; |
| 80 | str[start] = str[end]; |
| 81 | str[end] = temp; |
| 82 | } |
| 83 | |
| 84 | str[i] = '\0'; |
| 85 | } |
| 86 | |
| 87 | void puts(const char *str) { |
| 88 | while (*str) { |