-
Notifications
You must be signed in to change notification settings - Fork 0
/
man_3_printf
102 lines (102 loc) · 3.54 KB
/
man_3_printf
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
.TH man 3 "06 OCTOBER 2021" "PRINTF AT ALX" "_PRINTF"
.SH NAME
.B _printf
- formatted output conversion
.SH SYNOPSIS
.B #include \(dqmain.h\(dq
.sp
.B int _printf(const char *
.I format
.B , ...);
.SH DESCRIPTION
The function _printf is a variadic function that produces a string according to a
.I format
and prints it to the standard output, much like the C standard library function printf. It can receive a variable number of arguments.
This function has been created in the context of a first-year project for Holberton School.
.sp
.B Return value
.sp
On success, _printf returns the number of characters printed. If
.I format
is NULL or if the functions encounters any errors, it returns -1.
.sp
.B Format of the format string
.sp
The format string can contain any characters, just like a regular string, in double quotes, which will be printed as is. For example, _printf(\(dqHello\(dq) will print: Hello.
However, this format string can contain
.I conversion specifiers.
These start with a %, and are followed with a character (see
.I conversion specifiers
). Special characters like a new line are escaped, for example _printf(\(dq\\n\(dq) will print a new line.
.sp
.B Conversion specifiers
.sp
These are the characters that specify the type of the variable that needs to be printed. The variables to be printed are separated from the format string with a comma. Each variable passed to _printf need to have a conversion specifier.
.sp
.B i, d
- Both these specifiers are for
.I int
arguments. They convert the variable to a decimal number, positive or negative.
.sp
.B c
- Prints a
.I single character.
.sp
.B s
- Prints an entire
.I string.
.sp
.B u
- Prints an
.I unsigned int.
.sp
.B x, X
- Respecively print a number in lowercase and uppercase
.I hexadecimal
base.
.sp
.B o, b
- Respectively print a number in
.I octal
and
.I binary
base.
.sp
.B %
- Acts like an escape for the character %. To print \(dq%\(dq, the format string should contain \(dq%%\(dq.
.sp
.B r, R
- Respectively print a string in reverse and in ROT13 encryption.
.sp
.B S
- Prints a string, but non-printable characters are printed this way: \\x, followed by the ASCII code value in uppercase hexadecimal.
.sp
.B p
- Prints an address, given by a pointer variable
.sp
.B Flag characters for conversion specifiers
.sp
.B +
- Prints a plus sign in front of positive signed integers and a minus sign in front of negative signed integers.
+ overrides a space when both flags are given.
.sp
.B ' '
- Prints a space in front of signed integers.
.sp
.B #
- Modifier for unsigned conversions. For x, prepends \(dq0x\(dq, and for X, prepends \(dq0X\(dq. For o, if the converted number doesn't already begin with a 0, prepends a 0.
.sp
.SH SEE ALSO
.I printf(3)
.I _putchar.c
.SH NOTES
_printf uses a custom character printing function called _putchar, that uses the system call write().
_putchar uses a static buffer of 1024 bytes and a static incrementing variable.
That way, everytime _putchar is called inside _printf, the character is stored inside the buffer and the function moves to the next character,
until _printf calls _putchar(-1), which \(dqflushes\(dq the buffer and resets the incrementing variable to 0.
That way, _putchar only needs to call write one time, passing to it the whole buffer and the number of bytes in it, which is given by the value of the incrementing variable.
.sp
.SH BUGS
_printf does not take care of length and precision modifiers yet. For example, _printf(\(dq%hx\(dq) will not produce the same output as the call to the standard library printf.
.SH AUTHORS
Moses Oyet and Faith Milidzani Mcube, Cohort 4.