-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitoa.c
55 lines (41 loc) · 1 KB
/
itoa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Exercise 3-4. In a two’s complement number representation, our version of
* itoa does not handle the largest negative number, that is, the value of n
* equal to -(2^(wordsize-1)). Explain why not. Modify it to print that value
* correctly, regardless of the machine on which it runs. */
#include "stdio.h"
#include "string.h"
#include "limits.h"
#define MAX 512
void itoa(int n, char s[]);
void strrev(char s[]);
int main(void) {
int n = INT_MAX;
char s[MAX];
itoa(n, s);
printf("%d:\n%s\n", n, s);
return 0;
}
void itoa(int n, char s[]) {
enum signs {POS, NEG};
int sign = n < 0 ? NEG : POS;
size_t i;
unsigned un = n==INT_MIN || n>0 ? n : -n;
i = 0;
do {
s[i++] = un % 2 + '0';
} while ((un >>= 1) > 0);
if (sign == NEG)
s[i++] = '-';
s[i] = '\0';
strrev(s);
}
/* String reverser */
void strrev(char s[]) {
size_t i, j;
char temp;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}