> 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_is_negative.md).

# ft\_is\_negative

This exercise requires you to write a function `ft_is_negative` that checks the sign of an integer and displays either 'N' or 'P':

* 'N' if the number is negative.
* 'P' if the number is zero or positive.

#### Requirements:

* **Input:** An integer `n`.
* **Output:**
  * Print 'N' if the integer is negative.
  * Print 'P' if the integer is zero or positive.
* **Allowed Function:** Only `write` can be used for output.
* **Return Type:** The function should not return anything (`void`).

#### Steps:

1. **Check the sign of the number:**
   * If `n` is less than 0, print 'N' for negative.
   * Otherwise, print 'P' for positive or zero.
2. **Use the `write` function** to output 'N' or 'P'.

#### Code Implementation:

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

void ft_is_negative(int n)
{
    if (n < 0)
        write(1, "N", 1);  // Write 'N' if the number is negative
    else
        write(1, "P", 1);  // Write 'P' if the number is zero or positive
}
```

#### Explanation:

* **Condition Check:**\
  The `if (n < 0)` condition checks if the integer `n` is negative. If it is, the function will print 'N'. If it's zero or positive, the function will print 'P'.
* **write Function:**
  * The `write` function is used to print the result. We pass:
    * `1` as the file descriptor for standard output.
    * A pointer to the string to print (`"N"` or `"P"`).
    * `1` as the number of bytes to write (since we're printing just one character).

#### Example of How to Test:

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

```c
#include <unistd.h>

// Function prototype
void ft_is_negative(int n);

int main(void)
{
    ft_is_negative(-5);  // Output: N
    ft_is_negative(0);   // Output: P
    ft_is_negative(42);  // Output: P
    return 0;
}
```

#### Expected Output:

```css
N
P
P
```

* For `ft_is_negative(-5)`, it should print `'N'` since -5 is negative.
* For `ft_is_negative(0)` and `ft_is_negative(42)`, it should print `'P'` because both 0 and 42 are non-negative.

#### Key Points:

* **Handling Signs:** The function uses a simple `if` statement to check if the integer is negative or not.
* **System Call (`write`):** Just like previous exercises, we use the `write` system call to output the result directly to the terminal.

#### Conclusion:

This exercise tests your ability to work with conditionals and output simple characters based on the sign of an integer. It's very similar to the previous exercises, with the difference being that you're printing different characters based on the input integer's sign.

<mark style="color:red;">**Before submitting your project, comment out or remove your main function.**</mark>


---

# 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_is_negative.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.
