You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.0 KiB
46 lines
1.0 KiB
8 months ago
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <limits.h>
|
||
|
|
||
|
void itoa(int n, char s[]);
|
||
|
void reverse(char s[]);
|
||
|
|
||
|
int main(void) {
|
||
|
char buffer[20];
|
||
|
int i = 35;
|
||
|
|
||
|
printf("Number: %d\n", i);
|
||
|
itoa(i, buffer);
|
||
|
printf("Buffer : %s\n", buffer);
|
||
|
|
||
|
printf("INT_MIN: %d\n", INT_MIN);
|
||
|
itoa(INT_MIN, buffer);
|
||
|
printf("Buffer : %s\n", buffer);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/* itoa: преобразование n в строку s */
|
||
|
void itoa(int n, char s[]) {
|
||
|
int i, sign;
|
||
|
sign = n; /* сохраняем знак */
|
||
|
|
||
|
i = 0;
|
||
|
do { /* генерируем цифры в обратном порядке */
|
||
|
s[i++] = abs(n % 10) + '0'; /* следующая цифра */
|
||
|
} while ( n /= 10 ); /* исключить ее */
|
||
|
if (sign < 0)
|
||
|
s[i++] = '-';
|
||
|
|
||
|
s[i] = '\0';
|
||
|
reverse(s);
|
||
|
}
|
||
|
|
||
|
void reverse(char s[]) {
|
||
|
int c, i, j;
|
||
|
for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {
|
||
|
c = s[i];
|
||
|
s[i] = s[j];
|
||
|
s[j] = c;
|
||
|
}
|
||
|
}
|