This project is an implementation of the printf
function in the C standard libray.
The main.h
header must be included before you can use the function. The _printf
function returns the number of characters that were printed to stdout.
The format specifier uses the following pattern:
%[flag][width][.precision][length]specifier
The fields in brackets are optional, a specifier must be provided, and the fields must appear in the same order as the pattern provided above. If a specifier isn't provided (or the format specifier is invalid) and the format specifier is at the end of the format string an error occurs. On the contrary, if the specifier isn't provided (or the format specifier is invalid) and the format specifier is not at the end of the string, the raw invalid format specifier is printed as is.
Flag | Description |
---|---|
# | The value should be converted to an alternate form. |
0 | The value should be padded with zeros rather than spaces. |
- | The output should be left_align-aligned on the field boundary since the default is right-alignment on the field boundary. |
' ' | (a space) A blank space should be left_align before a positive number that is produced by a sign conversion. |
+ | A sign (+ or -) should always be placed before a number produced by a signed conversion. By default only the signs of negative numbers are shown. |
The width and precision fields can be provided as integers beginning with a non-zero digit. They can also be specified with the *
sign. If they're specified with the *
sign, the value is read from the argument list provided to _printf
. The maximum value of the width and precision fields is 2147483647
.
Length | Description |
---|---|
h | Modifies the integer to be a short signed or unsigned integer |
l | Modifies the integer to be a long signed or unsigned integer |
Specifier | Description |
---|---|
c | Prints the ASCII character representation of an integer. |
s | Prints the characters of a char * that is terminated by a null character (\0 ). |
d, i | Prints the signed decimal notation of an integer argument. |
f | Prints the signed decimal notation of an IEEE 754 floating-point argument. |
o, u, x, X | Prints the unsigned int argumen in unsigned octal notation (o), decimal notation (u) or hexadecimal notation (x and X). x uses lower-case hexadecimal characters and X uses the upper-case hexadecimal characters. |
% | Prints the "%" sign only. |
p | Prints a void * argument in hexadecimal form. |
b | Prints the binary representation of an unsigned integer. |
R | Prints the rot13 translation of a string. |
r | Prints the reverse of a string. |
S | Prints a string and the hexadecimal representation of unprintable characters (prefixed with \x ) in a string that is null terminated. |
_printf("%.2f\n", -3.46);
produces-3.46
._printf("%#x\n", 478);
produces0x1de
._printf("%R\n", "foobar");
producessbbone
._printf("%r\n", "foo")
producesoof
.
0. I'm not going anywhere. You can print that wherever you want to. I'm here and I'm a Spur for life
Write a function that produces output according to a format.
Prototype: int _printf(const char *format, ...); Returns: the number of characters printed (excluding the null byte used to end output to strings) write output to stdout, the standard output stream format is a character string. The format string is composed of zero or more directives. See man 3 printf for more detail. You need to handle the following conversion specifiers: c s % You don’t have to reproduce the buffer handling of the C library printf function You don’t have to handle the flag characters You don’t have to handle field width You don’t have to handle precision You don’t have to handle the length modifiers
Handle the following conversion specifiers:
d i You don’t have to handle the flag characters You don’t have to handle field width You don’t have to handle precision You don’t have to handle the length modifiers
Handle the following custom conversion specifiers:
b: the unsigned int argument is converted to binary
alex@ubuntu:~/c/printf$ cat main.c
#include "main.h"
/**
* main - Entry point
*
* Return: Always 0
*/
int main(void)
{
_printf("%b\n", 98);
return (0);
}
alex@ubuntu:~/c/printf$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 main.c
alex@ubuntu:~/c/printf$ ./a.out
1100010
alex@ubuntu:~/c/printf$
Handle the following conversion specifiers:
u o x X You don’t have to handle the flag characters You don’t have to handle field width You don’t have to handle precision You don’t have to handle the length modifiers
Use a local buffer of 1024 chars in order to call write as little as possible.
Handle the following custom conversion specifier:
S : prints the string. Non printable characters (0 < ASCII value < 32 or >= 127) are printed this way: \x, followed by the ASCII code value in hexadecimal (upper case - always 2 characters)
alex@ubuntu:~/c/printf$ cat main.c
#include "main.h"
/**
* main - Entry point
*
* Return: Always 0
*/
int main(void)
{
_printf("%S\n", "Best\nSchool");
return (0);
}
alex@ubuntu:~/c/printf$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 main.c
alex@ubuntu:~/c/printf$ ./a.out
Best\x0ASchool
alex@ubuntu:~/c/printf$
6. How is the world ruled and led to war? Diplomats lie to journalists and believe these lies when they see them in print
Handle the following conversion specifier: p.
You don’t have to handle the flag characters You don’t have to handle field width You don’t have to handle precision You don’t have to handle the length modifiers
Handle the following flag characters for non-custom conversion specifiers:
+
space#
Handle the following length modifiers for non-custom conversion specifiers:
l h Conversion specifiers to handle: d, i, u, o, x, X
Handle the field width for non-custom conversion specifiers.
Repo:
GitHub repository: printf
Handle the precision for non-custom conversion specifiers.
Repo:
GitHub repository: printf
Handle the 0 flag character for non-custom conversion specifiers.
Repo:
GitHub repository: printf
12. Every time that I wanted to give up, if I saw an interesting textile, print what ever, suddenly I would see a collection
Handle the - flag character for non-custom conversion specifiers.
Repo:
GitHub repository: printf
Handle the following custom conversion specifier:
r : prints the reversed string
Handle the following custom conversion specifier:
R: prints the rot13'ed string
All the above options work well together. This project is done by Muhammad & Amira