-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.6.c
43 lines (39 loc) · 925 Bytes
/
3.6.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
#include <stdio.h>
#include <stdlib.h>
#include "lib/libkr.h"
#define MAXLINE 1024
/*
Exercise 3-6. Write a version of itoa that accepts three arguments
instead of two. The third argument is a minimum field width; the
converted number must be padded with blanks on the left if necessary to
make it wide enough.
*/
void itoa(int, char [], unsigned int);
/* itoa: convert n to characters in s */
void
itoa(int n, char s[], unsigned int w)
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = abs(n % 10) + '0'; /* get next digit */
} while (n /= 10); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
while (i < w)
s[i++] = ' ';
reverse(s, i);
}
int
main(void)
{
int n = 1024;
unsigned int width = 10;
char out[MAXLINE] = {0};
itoa(n, out, width);
printf("%s\n", out);
return 0;
}