online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <stdio.h> #include <string.h> #include <stdbool.h> #include <limits.h> static char* itoa(int number, size_t sz, char buffer[sz]) { // do error handling and edge cases if (buffer == NULL || sz == 0) return NULL; else if (number == 0) return strcpy(buffer, "0"); char tmp[sz]; int idx = 0; bool is_negative = (number < 0); if (!is_negative) number = -number; // so we can handle INT_MIN for (; number != 0; number/=10, idx++) { if (sz-- == 0) return NULL; tmp[idx] = '0' - (number % 10); } if (is_negative) { if (sz-- == 0) return NULL; tmp[idx++] = '-'; } // reverse for (int i=0; i<idx; i++) buffer[idx-i-1] = tmp[i]; buffer[idx] = '\0'; return buffer; } int main(void) { char buffer[100]; int test[] = {123, 234, 0, 17, -4, -7, -17, -27, INT_MAX, INT_MIN}; size_t len = sizeof(test) / sizeof(*test); // give it a small buffer and see how it works for (int i=0, num; i < len; i++) { num = test[i]; char* str = itoa(num, 2, buffer); printf("%-10d <%s>\n", num, str); } // now give it the correctly-sized buffer for (int i=0, num; i < len; i++) { num = test[i]; char* str = itoa(num, sizeof buffer / sizeof *buffer, buffer); printf("%-10d <%s>\n", num, str); } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue