> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c04/ft_putnbr.md).

# ft\_putnbr

**Objective:**

Create a function that displays the number entered as a parameter. The function must be able to display all possible values within an `int` type variable, including negative numbers.

***

**Turn-in Directory:**

`ex02/`

***

**Files to Turn In:**

`ft_putnbr.c`

***

**Allowed Functions:**

`write`

***

**Prototype:**

```c
void ft_putnbr(int nb);
```

***

#### **Detailed Explanation**

The `ft_putnbr` function takes an integer `nb` as input and displays it on the standard output. It handles both positive and negative numbers and uses recursion to break down the number into individual digits for printing.

***

#### **Code Implementation**

```c
void ft_putchar(char c)
{
    write(1, &c, 1);  // Write the character 'c' to the standard output
}

void ft_putnbr(int nb)
{
    long n;

    n = nb;  // Use a long variable to handle the edge case of INT_MIN
    if (n < 0)  // Handle negative numbers
    {
        ft_putchar('-');  // Print the minus sign
        n = -n;  // Convert the number to positive
    }
    if (n >= 10)  // Handle numbers with more than one digit
    {
        ft_putnbr(n / 10);  // Recursively print the digits except the last one
        n = n % 10;  // Get the last digit
    }
    if (n < 10)  // Handle single-digit numbers
    {
        ft_putchar(n + 48);  // Print the last digit (convert to ASCII)
    }
}
```

***

#### **Example Usage**

```c
#include <unistd.h>

void ft_putnbr(int nb);

int main(void)
{
    ft_putnbr(42);  // Output: 42
    ft_putnbr(-123);  // Output: -123
    ft_putnbr(0);  // Output: 0
    ft_putnbr(2147483647);  // Output: 2147483647 (INT_MAX)
    ft_putnbr(-2147483648);  // Output: -2147483648 (INT_MIN)
    return 0;
}
```

***

#### **Edge Cases to Consider**

1. **Positive Numbers**:
   * `42` → Prints `42`.
   * `2147483647` (INT\_MAX) → Prints `2147483647`.
2. **Negative Numbers**:
   * `-123` → Prints `-123`.
   * `-2147483648` (INT\_MIN) → Prints `-2147483648`.
3. **Zero**:
   * `0` → Prints `0`.
4. **Single-Digit Numbers**:
   * `5` → Prints `5`.
   * `-9` → Prints `-9`.

***

#### **Limitations of ft\_putnbr**

1. **No Bounds Checking**:
   * The function assumes the input is a valid integer. If given a non-integer input, the behavior is undefined.
2. **Recursion Depth**:
   * For very large numbers, the recursion depth may increase, but this is generally not an issue for `int` values.

***

#### **How It Works**

1. **Handle Negative Numbers**:
   * If the number is negative, print a minus sign (`-`) and convert the number to positive.
2. **Recursive Breakdown**:
   * If the number has more than one digit, recursively call `ft_putnbr` with `n / 10` to print all digits except the last one.
   * After the recursive call, print the last digit using `n % 10`.
3. **Print Single-Digit Numbers**:
   * If the number is a single digit, convert it to its ASCII representation by adding `48` (the ASCII value of `'0'`) and print it.

***

#### **Key Points to Remember**

1. **Handling INT\_MIN**:
   * The function uses a `long` variable to handle the edge case of `INT_MIN` (-2147483648), which cannot be represented as a positive `int`.
2. **Recursion**:
   * Recursion is used to break down the number into individual digits. This simplifies the logic for printing multi-digit numbers.
3. **ASCII Conversion**:
   * To print a digit, add `48` to convert it to its ASCII representation (e.g., `0` → `'0'`, `1` → `'1'`, etc.).

***

#### **Best Practices**

1. **Adhere to the Norm**:
   * Ensure your code follows the **norminette** guidelines (e.g., no more than 25 lines per function, proper indentation, etc.).
   * Run `norminette` on your file to check for compliance.
2. **Test Edge Cases**:
   * Test your function with `INT_MAX`, `INT_MIN`, `0`, and single-digit numbers.
3. **Avoid Using Library Functions**:
   * Do not use built-in functions like `printf` or `itoa` from the C standard library. The goal is to implement the functionality yourself.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c04/ft_putnbr.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
