{"id":21458164,"url":"https://github.com/samuelselasi/printf","last_synced_at":"2025-03-17T04:16:09.300Z","repository":{"id":65389336,"uuid":"591500015","full_name":"samuelselasi/printf","owner":"samuelselasi","description":"C Custom Printf Function","archived":false,"fork":false,"pushed_at":"2023-02-27T16:53:47.000Z","size":114,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-23T13:43:55.794Z","etag":null,"topics":["alx-low-level-programming","alx-system-engineering","c","pointers-and-arrays","printf","struct","typedef","variadic-functions"],"latest_commit_sha":null,"homepage":"https://github.com/samuelselasi/printf","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samuelselasi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-20T22:57:16.000Z","updated_at":"2023-02-27T19:36:01.000Z","dependencies_parsed_at":"2025-01-23T13:41:32.700Z","dependency_job_id":"0c2f136a-de02-43ff-8fed-51abd429a339","html_url":"https://github.com/samuelselasi/printf","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelselasi%2Fprintf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelselasi%2Fprintf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelselasi%2Fprintf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelselasi%2Fprintf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samuelselasi","download_url":"https://codeload.github.com/samuelselasi/printf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243971212,"owners_count":20376784,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alx-low-level-programming","alx-system-engineering","c","pointers-and-arrays","printf","struct","typedef","variadic-functions"],"created_at":"2024-11-23T06:18:03.869Z","updated_at":"2025-03-17T04:16:09.261Z","avatar_url":"https://github.com/samuelselasi.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C - Custom printf function\n\n## Background Context\n```\nCustom printf function\n```\n![printf](https://user-images.githubusercontent.com/85158665/213859591-8d8dbbb1-2be7-4f75-ae00-cc1b66dda46c.png)\n## Requirements\n### General\n* Used editors: `vi`, `vim`\n* All files will compiled on Ubuntu 20.04 LTS using gcc, using the options `-Wall -Werror -Wextra -pedantic -std=gnu89`\n* All files end with a new line\n* Code for custom printf uses the `Betty` style. Checked using [betty-style.pl](https://github.com/holbertonschool/Betty/blob/master/betty-style.pl) and [betty-doc.pl](https://github.com/holbertonschool/Betty/blob/master/betty-doc.pl)\n* No global variables used\n* No more than 5 functions per file\n* The prototypes of all your functions are included in header file called [main.h](./main.h)\n* All header files are include guarded\n## More Info\n### Global functions and macros used\n* `write` (man 2 write)\n* `malloc` (man 3 malloc)\n* `free` (man 3 free)\n* `va_start` (man 3 va_start)\n* `va_end` (man 3 va_end)\n* `va_copy` (man 3 va_copy)\n* `va_arg` (man 3 va_arg)\n## Compilation\n```\n$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c\n```\n## Tasks\n[task0](./printf.c)\n```\nWrite a function that produces output according to a format.\n```\n* Prototype: int _printf(const char *format, ...);\n* Returns: the number of characters printed (excluding the null byte used to end output to strings)\n* write output to stdout, the standard output stream\n* 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:\n\t* [c](./print_char.c)\n\t* [s](./print_string.c)\n\t* [%](./_printf.c)\n* You don’t have to reproduce the buffer handling of the C library printf function\n* You don’t have to handle the flag characters\n* You don’t have to handle field width\n* You don’t have to handle precision\n* You don’t have to handle the length modifiers\n\n[task1](./_printf.c)\n```\nHandle the following conversion specifiers:\n```\n* [d](./print_int.c)\n* [i](./print_int.c)\n\n* You don’t have to handle the flag characters\n* You don’t have to handle field width\n* You don’t have to handle precision\n* You don’t have to handle the length modifiers\n\n[task2](./_printf.c)\n```\nHandle the following custom conversion specifiers:\n```\n* b: the unsigned int argument is converted to binary\n\t* [Print_Binary](./print_binary.c) \n\n[task3](./printf.c)\n```\nHandle the following conversion specifiers:\n```\n* u:\n\t* [Print_Unsigned](./print_unsigned.c)\n* o:\n\t* [Print_Octal](./print_octal.c)\n* x:\n\t* [Print_Hexadecimal](./print_hexadecimal.c)\n\t* [Print_Lower_Hex_Chars](./print_hex_lower.c)\n* X:\n\t* [Print_Hexadecimal](./print_hexadecimal.c)\n\t* [Print_Upper_Hex_Chars](./print_hex_upper.c)\n\n* You don’t have to handle the flag characters\n* You don’t have to handle field width\n* You don’t have to handle precision\n* You don’t have to handle the length modifiers\n\n[task4](./_printf.c)\n```\nUse a local buffer of 1024 chars in order to call write as little as possible.\n```\n* [buffer](./print_buffer.c)\n\n[task5](./_printf.c)\n```\nHandle the following custom conversion specifier:\n```\n* S : prints the string. [Print_String](./print_string.c)\n* Non printable characters (0 \u003c ASCII value \u003c 32 or \u003e= 127) are printed this way: \\x, followed by the ASCII code value in hexadecimal (upper case - always 2 characters)\n\t* [Print_Non-Printable_Chars](./print_non_printable.c)\n\n\n[task6](./_printf.c)\n```\nHandle the following conversion specifier: p.\n```\n* You don’t have to handle the flag characters\n* You don’t have to handle field width\n* You don’t have to handle precision\n* You don’t have to handle the length modifiers\n\t* [Print_Pointer](./print_pointer.c)\n\n[task7](./_printf.c)\n```\nHandle the following flag characters for non-custom conversion specifiers:\n```\n* [+](./get_flags.c)\n* [space](./get_flags.c)\n* [#](./get_flags.c)\n\n\n[task8](./_printf.c)\n```\nHandle the following length modifiers for non-custom conversion specifiers:\n```\n* [l](./convert_size_number.c)\n\n* [h](./convert_size_unsigned.c)\n\n* Conversion specifiers to handle: d, i, u, o, x, X\n\n\n[task9](./_printf.c)\n```\nHandle the field width for non-custom conversion specifiers.\n```\n* [Handle_Field_Width](./get_width.c)\n\n\n[task10](./_printf.c)\n```\nHandle the precision for non-custom conversion specifiers.\n```\n* [Handle_Precision](./get_precision.c)\n\n\n[task11](./_printf.c)\n```\nHandle the 0 flag character for non-custom conversion specifiers.\n```\n* [Handle_0_Flag](./nprint_int.c)\n\n\n[task12](./_printf.c)\n```\nHandle the - flag character for non-custom conversion specifiers.\n```\n* [Handle_-_Flag](./get_flags.c)\n\n\n[task13](./_printf.c)\n```\nHandle the following custom conversion specifier:\n```\n* r : prints the reversed string.\n\t* [Handle_String_Reverse](./print_reverse.c)\n\n\n[task14](./_printf.c)\n```\nHandle the following custom conversion specifier:\n```\n* R: prints the rot13'ed string\n\t* [Rot13_Encryption](./print_rot13.c)\n\n\n[task15](./_printf.c)\n```\nAll the above options work well together.\n```\n* [header_file](./main.h)\n\n## Pseudocode\n[draw.io](https://drive.google.com/file/d/1PCFpX56mtAerF7RS7wzx9HCEFXc-Drib/view?usp=sharing)\n[Pseudocode_For_Printf](./https://docs.google.com/document/d/1BOKUnRWP_8GRWBPqD_BZW-OB2V7Nh4YgCH765JsM1gg/edit?usp=sharing)\n\n## Flow-Chart Diagram\n![_PRINTF](https://user-images.githubusercontent.com/117751396/214351434-afbca8cb-b6d0-4494-91c8-08236143bcb8.png)\n## Implementation\n1. The function takes a format string and a variable number of arguments.\n\n2. It initialises variables including an integer i, printed, printed_chars, flags, width, precision, size, buff_ind, a char buffer with a size of BUFF_SIZE and a va_list named list\n\n3. The function checks if the format string is NULL and returns -1 if it is.\n\n4. It initialises the variable argument list and sets the buffer index to 0 and the printed characters to 0.\n\n5. The function iterates through the format string, for each iteration it checks if the current character is not '%'\n\n6. If the current character is not '%', it adds the character to the buffer and increments the buffer index by 1.\n\n7. If the buffer index reaches the size of the buffer, it calls the print_buffer function to print the buffer.\n\n8. If the current character is \"%\", it calls the helper functions get_flags, get_width, get_precision, get_size and handle_print to process the formatting\n9. It prints the output to the buffer and increments the printed_chars by the number of characters printed.\n\n10. At the end of the iteration; it calls the print_buffer function to print whatever is left in the buffer.\n\n11. The function ends the variable argument list and returns the total number of characters printed.\n\n\n# Collaborators\n- **Yasmine Ben Ali**\n- **Samuel Selasi**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelselasi%2Fprintf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamuelselasi%2Fprintf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelselasi%2Fprintf/lists"}