> 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/c00/ft_putnbr.md).

# ft\_putnbr

This exercise requires you to write a function `ft_putnbr` that displays the number passed as a parameter. The function must handle all possible values within the range of an `int` type variable, including negative numbers.

***

#### **Requirements:**

1. **Input:** An integer `nb`.
2. **Output:**
   * Print the integer `nb` to the standard output.
   * If `nb` is negative, print a minus sign (`-`) followed by the absolute value of the number.
3. **Allowed Function:** Only `write` can be used for output.
4. **Return Type:** The function should not return anything (`void`).

***

#### **Steps to Solve:**

1. **Handle Negative Numbers:**
   * If the number is negative, print a minus sign (`-`) and convert the number to its absolute value.
2. **Recursive Approach:**
   * Use recursion to break down the number into its individual digits.
   * Print each digit from left to right.
3. **Print Digits:**
   * Convert each digit to its ASCII representation and print it using the `write` function.

***

#### **Code Implementation:**

```c
#include <unistd.h>  // For the write function

void	ft_putnbr(int nb)
{
	char	n;
	long	t;

	t = nb;  // Use a long variable to handle the edge case of INT_MIN
	if (t < 0)
	{
		t = -t;  // Convert negative number to positive
		write(1, "-", 1);  // Print the minus sign
	}
	if (t >= 10)
	{
		ft_putnbr(t / 10);  // Recursively print the digits except the last one
	}
	n = (t % 10) + 48;  // Convert the last digit to ASCII
	write(1, &n, 1);  // Print the last digit
}
```

***

#### **Explanation:**

1. **Handling Negative Numbers:**
   * The variable `t` is used to store the value of `nb` as a `long` to handle the edge case of `INT_MIN` (the smallest possible `int` value, which cannot be directly negated).
   * If `t` is negative, it is converted to a positive number, and a minus sign (`-`) is printed.
2. **Recursive Approach:**
   * If the number has more than one digit (`t >= 10`), the function calls itself recursively with `t / 10` to print all digits except the last one.
   * This ensures that the digits are printed from left to right.
3. **Printing Digits:**
   * The last digit of the number is obtained using `t % 10`.
   * It is converted to its ASCII representation by adding `48` (the ASCII value of `'0'`).
   * The `write` function is used to print the digit.

***

#### **Example Output:**

If you call `ft_putnbr` with different inputs, the output will look like this:

```c
ft_putnbr(42);    // Output: 42
ft_putnbr(-42);   // Output: -42
ft_putnbr(0);     // Output: 0
ft_putnbr(2147483647);  // Output: 2147483647
ft_putnbr(-2147483648); // Output: -2147483648
```

***

#### **Key Points:**

* **Handling Negative Numbers:** The function correctly handles negative numbers by printing a minus sign and converting the number to its absolute value.
* **Recursion:** The recursive approach simplifies the process of breaking down the number into its individual digits.
* **Edge Case Handling:** The use of a `long` variable ensures that the function works correctly for `INT_MIN`.

***

#### **Testing the Function:**

Here’s a simple `main.c` to test the `ft_putnbr` function:

```cpp
#include <unistd.h>

// Function prototype
void ft_putnbr(int nb);

int main(void)
{
    ft_putnbr(42);    // Output: 42
    write(1, "\n", 1);
    ft_putnbr(-42);   // Output: -42
    write(1, "\n", 1);
    ft_putnbr(0);     // Output: 0
    write(1, "\n", 1);
    ft_putnbr(2147483647);  // Output: 2147483647
    write(1, "\n", 1);
    ft_putnbr(-2147483648); // Output: -2147483648
    write(1, "\n", 1);
    return 0;
}
```

**Expected Output:**

```
42
-42
0
2147483647
-2147483648
```

***

#### **Conclusion:**

This exercise tests your ability to handle integers, including negative numbers and edge cases like `INT_MIN`. The recursive approach simplifies the process of printing each digit, and the use of the `write` function ensures that the output is efficient and adheres to the constraints of the exercise.


---

# 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/c00/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.
