-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes_functions_int.c
More file actions
59 lines (50 loc) · 1.15 KB
/
types_functions_int.c
File metadata and controls
59 lines (50 loc) · 1.15 KB
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
56
57
58
59
#include "main.h"
/**
* print_int - Prints an integer from the argument list
* @args: va_list containing the integer to be printed
*
* Description:
* This function receives an integer from the variable argument list
* and prints its transcription in string to the standard output.
* It handles positive and negative values.
*
* Return: Number of characters printed
*/
int print_int(va_list args)
{
int n = va_arg(args, int);
int digit = 0;
int i = 0;
int start = 0, end = 0; /* indexes to move trough the buffer */
char temp;
char buffer[12] = {0};
if (n == 0)
{
buffer[i] = '0';
i++;
}
if (n < 0)
{
buffer[i] = '-';
n = -n;
i++;
}
start = i; /* var to stock val of i if '-' to add before number starts */
while (n > 0)
{
digit = n % 10; /* allows us to acces the last digit */
buffer[i] = digit + '0'; /* stock the last digit and convert it to a char using ascii table */
n = n / 10; /* allows us to move to the next digit*/
i++;
}
end = i - 1;
while (start < end)
{
temp = buffer[start];
buffer[start] = buffer[end];
buffer[end] = temp;
end--;
start++;
}
return (write(1, buffer, i));
}