{"id":26715580,"url":"https://github.com/bmmunga/printf","last_synced_at":"2025-09-02T12:42:20.237Z","repository":{"id":281936847,"uuid":"618759065","full_name":"bmmunga/printf","owner":"bmmunga","description":"This repository is a group project for the recreation of the C standard library printf function. It was done as part of the ALX Africa curriculum facilitated by Holberton School.","archived":false,"fork":false,"pushed_at":"2023-03-29T11:10:39.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-11T23:33:07.363Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/bmmunga.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-03-25T09:09:16.000Z","updated_at":"2023-03-28T21:56:03.000Z","dependencies_parsed_at":"2025-03-11T23:43:12.861Z","dependency_job_id":null,"html_url":"https://github.com/bmmunga/printf","commit_stats":null,"previous_names":["bmmunga/printf"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmmunga%2Fprintf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmmunga%2Fprintf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmmunga%2Fprintf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bmmunga%2Fprintf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bmmunga","download_url":"https://codeload.github.com/bmmunga/printf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245863384,"owners_count":20684843,"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":[],"created_at":"2025-03-27T14:33:25.646Z","updated_at":"2025-03-27T14:33:27.820Z","avatar_url":"https://github.com/bmmunga.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# _printf\n\nA formatted output conversion C program completed as part of the low-level\nprogramming and algorithm track at ALX Africa facilitated by Holberton School.\nThe program is a pseudo-recreation of the C standard library function, `printf`.\n\n## Dependencies\n\nThe `_printf` function was coded on an Ubuntu 20.04 LTS machine with `gcc` version 9.4.0.\n\n## Usage\n\nTo use the `_printf` function, assuming the above dependencies have been installed,\ncompile all `.c` files in the repository and include the header `main.h` with\nany main function.\n\nExample `main.c`:\n```\n#include \"main.h\"\n\nint main(void)\n{\n    _printf(\"Hello, World!\");\n\n    return (0);\n}\n```\n\nCompilation:\n```\n$ gcc *.c -o tester\n```\n\nOutput:\n```\n$ ./tester\nHello, World!\n$\n```\n\n## Description\n\nThe function `_printf` writes output to standard output. The function writes\nunder the control of a `format` string that specifies how subsequent arguments\n(accessed via the variable-length argument facilities of `stdarg`) are\nconverted for output.\n\nPrototype: `int _printf(const char *format, ...);`\n\n### Return Value\n\nUpon successful return, `_printf` returns the number of characters printed\n(excluding the terminating null byte used to end output to strings). If an\noutput error is encountered, the function returns `-1`.\n\n### Format of the Argument String\n\nThe `format` string argument is a constant character string composed of zero\nor more directives: ordinary characters (not `%`) which are copied unchanged\nto the output stream; and conversion specifications, each of which results in\nfetching zero or more subsequent arguments. Conversion specification is\nintroduced by the character `%` and ends with a conversion specifier. In\nbetween the `%` character and conversion specifier, there may be (in order)\nzero or more _flags_, an optional minimum _field width_, an optional\n_precision_ and an optional _length_ modifier. The arguments must correspond\nwith the conversion specifier, and are used in the order given.\n\n#### Flag Characters\n\nThe character `%` may be followed by zero or more of the following flags:\n\n#### #\n  * For `o` conversions, the first character of the output string is prefixed\n  with `0` if it was not zero already.\n  * For `x` converions, `0x` is prepended for non-zero numbers.\n  * For `X` conversions, `0X` is prepeneded for non-zero numbers.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%#x\\n\", 7);\n}\n```\nOutput:\n```\n0x7\n```\n\n#### (space)\n  * A blank is left before a positive number or empty string produced by a\n  signed conversion.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"% d\\n\", 7);\n}\n```\nOutput:\n```\n 7\n```\n\n#### +\n  * A sign (`+` or `-`) is always placed before a number produced by signed\n  conversion.\n  * Overrides a space flag.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%+d\\n\", 7);\n}\n```\nOutput:\n```\n+7\n```\n\n#### 0\n  * For `d`, `i`, `o`, `u`, `x`, and `X` conversions, the converted value is\n  padded on the left with zeroes rather than blanks.\n  * If the `0` flag is provided to a numeric conversion with a specified\n  precision, it is ignored.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%05d\\n\", 7);\n}\n```\nOutput:\n```\n00007\n```\n\n#### -\n  * The converted value is left-justified (padded on the right with blanks\n  instead of on the left with blanks or zeroes).\n  * Overrides a `0` flag.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%-5d7\\n\", 7);\n}\n```\nOutput:\n```\n7    7\n```\n\n#### Field Width\n\nAfter flags, a minimum field width may be specified by a decimal digit string\nThe first digit must be non-zero. If the converted value has fewer characters\nthan the provided width, the output is padded on the left or right with spaces\n(depending on whether the `-` flag was provided).\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%7d\\n\", 7);\n}\n```\nOuptut:\n```\n      7\n```\n\nAlternatively, width may be provied as an argument using the `*` character\nFor example, in the following:\n`_printf(\"%*d\\n\", 9, 1);`\nthe argument `9` is considered the width for the conversion of the decimal `1`.\n\n#### Precision\n\nAfter any flags or provided width, a precision may be specified by a `.`\nfollowed by a decimal digit string. For `d`, `i`, `o`, `u`, `x`, and `X`\nconversions, the precision specifies the minimum number of digits to appear.\nFor `s` and `S` conversions, the precision specifies the maximum characters\nto be printed.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%.7d\\n\", 7);\n}\n```\nOutput:\n```\n0000007\n```\n\nAlternatively, precision may be provided as an argument using the `*` character\nafter the `.`. For example, in the following:\n`_printf(\"%.*d\\n\", 9, 1);`\nthe argument `9` is considered the precision for the conversion of the decimal\n`1`.\n\n#### Length Modifiers\n\nAfter flags, width, and precision and before a conversion specifier, one of the\nfollowing length modifiers may be provided:\n\n#### h\nSpecifies that an integer conversion corresponds to a `short int` or\n`unsigned short int` argument.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%hd\\n\", SHRT_MAX);\n}\n```\nOutput:\n```\n32767\n```\n\n#### l\nSpecifies that an integer conversion corresponds to a `long int` or\n`unsigned long int` argument.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%ld\\n\", LONG_MAX);\n}\n```\nOutput:\n```\n91122337235554775807\n```\n\n#### Conversion Specifiers\n\nThe conversion specifier (introduced by the character `%`) is a character that\nspecifies the type of conversion to be applied. The `_printf` function\nsupports the following conversion specifiers:\n\n#### d, i\nThe `int` argument is converted to signed decimal notation.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%d\\n\", 9);\n}\n```\nOutput:\n```\n9\n```\n\n#### b\nThe `unsigned int` argument is converted to signed decimal notation.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%b\\n\", 7);\n}\n```\nOutput:\n```\n111\n```\n\n#### o, u, x, X\nThe `unsigned int` argument is converted to unsigned octal (`o`), unsigned\ndecimal (`u`), or unsigned hexadecimal (`x` and `X`). The letters `abcdef` are\nused for `x` conversions and the letters `ABCDEF` are used for `X` conversions.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%o\\n\", 77);\n}\n```\nOutput:\n```\n115\n```\n\n#### c\nThe `int` argument is converted to an `unsigned char`.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%c\\n\", 48);\n}\n```\nOutput:\n```\n0\n```\n\n#### s\nThe `const char *` argument is expected to be a pointer to a character array\n(aka. pointer to a string). Characters from the array are written starting\nfrom the first element of the array and ending at, but not including, the\nterminating null byte (`\\0`).\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%s\\n\", \"Hello, World!\");\n}\n```\nOutput:\n```\nHello, World!\n```\n\n#### S\nIdentical to the `s` conversion specifier, except any non-printable characters\nin the array (ie. characters with an ASCII value \u003c 32 or \u003e= 127) are written\nas `\\x` followed by the ASCII code value in hexadecimal (upper case, two\ncharacters).\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%S\\n\", \"Hello, World! Π\");\n}\n```\nOutput:\n```\nHello, World! \\x0FFFFFFFFFFFFFFCE\\x0FFFFFFFFFFFFFFA0\n```\n\nr\nIdentical to the `s` conversion specifier, except characters from the array\nare written in reverse, starting from, but not including, the terminating null\nbyte (`\\0`) and ending at the first element of the array.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"r\\n\", \"Hello, World\");\n}\n```\nOutput:\n```\ndlroW ,olleH\n```\n\n#### R\n\nIdentical to the `s` conversion specifier, except each character of the array\nis converted to its corresponding character in ROT13 before being written.\n\nExample `main.c`:\n```\nint main(void)\n{\n    _printf(\"%R\\n\", \"Hello, World\");\n}\n```\nOutput:\n```\nUryyb, Jbeyq\n```\n\n#### p\nThe address of the argument is written. The address is written in hexadecimal\nwith a leading `0x`.\n\nExample `main.c`:\n```\nint main(void)\n{\n    char *str = \"Hello, World\";\n\n    _printf(\"%p\\n\", (void *)str);\n}\n```\nOutput:\n```\n0x561a6d7bab5d\n```\n\n#### %\nA `%` is written. No argument is converted. The complete conversion\nspecification is `%%`.\n\nExample:\n```\nint main(void)\n{\n    _printf(\"%%\\n\");\n}\n```\nOutput:\n```\n%\n```\n## Authors\n\n* Boniface Munga \u003c[MungaSoftwiz](https://github.com/MungaSoftwiz)\u003e\n* Yusuff Abdulhakeem \u003c[\nhakeemyusuff](https://github.com/hakeemyusuff)\u003e\n\n## Acknowledgements\n\nThe `_printf` function emulates functionality of the C standard library\nfunction `printf`. This README borrows from the Linux man page\n[printf(3)](https://linux.die.net/man/3/printf).\n\nAll work contained in this repository was completed as part of the software\nengineering curriculum at ALX Africa. ALX Africa focuses on preparing students for\ntech roles using projects based peer learning. For more information, visit\n[this link](https://www.alxafrica.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbmmunga%2Fprintf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbmmunga%2Fprintf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbmmunga%2Fprintf/lists"}