fix printing of negative numbers

This commit is contained in:
cy384 2020-08-22 15:16:30 -04:00
parent 1ae0c0848f
commit c97e080173
1 changed files with 10 additions and 1 deletions

View File

@ -177,6 +177,7 @@ void print_int(int d)
char buffer[12] = {0}; char buffer[12] = {0};
int i = 10; int i = 10;
int negative = 0;
if (d == 0) if (d == 0)
{ {
@ -184,13 +185,21 @@ void print_int(int d)
i = -1; i = -1;
} }
if (d < 0)
{
negative = 1;
d *= -1;
}
for (; d > 0; i--) for (; d > 0; i--)
{ {
buffer[i] = itoc[d % 10]; buffer[i] = itoc[d % 10];
d /= 10; d /= 10;
} }
print_string(buffer+i+1); if (negative) buffer[i] = '-';
print_string(buffer+i+1-negative);
} }
void print_string(const char* c) void print_string(const char* c)