ft_printf() project - kevshouse/The-Under_C_World GitHub Wiki

Hi DS, I am about to start developing my ft_printf() at School 42. Please give me a functional overview of requirements I will need to satisfy.

Okay, the user is starting to work on their ft_printf project for School 42. They need a functional overview of the requirements. Let me recall what the standard printf does and what 42's version typically requires.

First, the basics: they need to replicate some of the conversions from the standard library's printf. The mandatory part usually includes handling c, s, p, d, i, u, x, X, and %. The bonus might involve flags like '-', '0', '.', '*', and ' '.

They need to manage different data types. For example, %c for characters, %s for strings. Pointers with %p, which requires hexadecimal output. Integers with d and i, unsigned with u, and hex for x and X. The % symbol itself needs to be printed.

Then there are the flags. The '-' for left justification, '0' for zero padding, '.' for precision, '*' for variable width/precision, and space for positive numbers. The width and precision specifiers are important for formatting.

Precision handling varies per specifier. For strings, it limits the length. For numbers, it sets the minimum digits. For pointers, maybe it's not applicable. They need to make sure that the precision doesn't cause issues, like truncating strings or leading zeros in numbers.

The length modifiers like 'l' or 'll' might be part of the bonus, but maybe not mandatory. The user should check the project PDF to confirm, but since they asked for requirements, I should mention common ones.

Error handling is crucial. They need to return -1 on error and manage the number of characters printed correctly. Buffer management might be necessary to avoid multiple write calls, which can affect performance.

Testing is a big part. They should test all conversion specifiers, edge cases like NULL pointers for %s, zero values, maximum/minimum integers, and various combinations of flags and modifiers.

Implementation steps: start by parsing the format string, identify conversion specifiers, extract flags, width, precision, and length modifiers. Then process each argument accordingly, convert to the correct string representation, apply formatting, and write to output.

Common pitfalls:

  • mishandling pointer addresses,
  • incorrect precision with numbers,
  • forgetting to handle negative numbers with certain flags,
  • and proper error checks for write function failures.

They should refer to the man pages for printf to understand expected behavior and test against the real printf to ensure compliance. Also, using the vargs macros correctly (va_start, va_arg, va_end) is essential for variable arguments.

So summarizing, the functional requirements include:

  • handling specific conversions,
  • flags, width/precision,
  • returning correct values,
  • error management,
  • and thorough testing.

They should structure their code to:

  • parse the format string efficiently,
  • manage variable arguments,
  • and format each specifier correctly with the appropriate flags.

Here's a functional overview of the requirements for ft_printf at 42 School (based on common project requirements):

1. Basic Requirements * Replicate behavior of printf from <stdio.h>, but only for specified conversions.

  * Return: Total number of characters printed (or -1 on error).

  * No memory leaks (must pass valgrind checks).

2. Mandatory Conversions

Mandatory Conversions

Must support these format specifiers:

Specifier Output
%c Single character
%s String (prints (null) if NULL)
%p Pointer address in hexadecimal (e.g., 0x1a2b3c)
%d Signed decimal integer
%i Signed decimal integer
%u Unsigned decimal integer
%x Lowercase hexadecimal (e.g., 1a2b)
%X Uppercase hexadecimal (e.g., 1A2B)
%% Literal % character

Commonly required flags (check your project PDF for exact requirements):

 *  `-` : Left-justify output within field width.

 *  `0` : Pad with zeros (ignored if - or precision is specified for numbers).

 *  `.` : Precision for numbers/strings (e.g., %.3s truncates to 3 chars).

 *  `*` : Dynamic width/precision via va_arg (e.g., %*d).

 *  `(space)`: Prefix positive numbers with a space (if no +).
  1. Precision & Width Handling Width: Minimum characters printed (pad with spaces/zeros).

    Example: %5d prints 123 (right-aligned).

Precision:

 * For %s: Max characters printed (truncate if needed).

 * For numbers: Min digits printed (pad with leading zeros).

 **Example:** %.5d prints 00123 for 123.
  1. Edge Cases & Error Handling
  • Handle NULL pointers for %s (print (null)).

  • Return -1 if write fails.

  • Handle undefined behavior cases gracefully (e.g., invalid specifiers).

  • Overflows (e.g., INT_MIN, UINT_MAX).

  1. Implementation Tips
  • Use va_list macros (va_start, va_arg, va_end) for variadic arguments.

  • Break code into modular functions:

  • Parsing (flags, width, precision, specifiers).

  • Conversion handlers (e.g., handle_int, handle_hex).

  • Use buffer management to minimize write calls.

Test extensively against printf for identical output.

  1. Testing Strategy Test all mandatory conversions with/without flags.

Test edge cases:

%p with NULL → 0x0.

%s with empty string and NULL.

Precision 0 for numbers (e.g., %.0d for 0 → empty string).

Use 42TESTERS and printfTester (GitHub repos) for validation.

⚠️ **GitHub.com Fallback** ⚠️