42 Piscine/C00

# ex00/ft\_putchar.c

This code defines a function named `ft\_putchar` with a parameter `c` of type `char`. The `ft\_putchar` function takes a character as an argument and writes that character to the standard output.

1. Function Declaration
* + `void ft\_putchar(char c)` This line declares a function named `ft\_putchar` that takes a single parameter `c` of type `char` and returns `void` (meaning it doesn't return any value).

2. Function Definition
* + `{}` The curly braces `{}` enclose the body of the function, defining what the function does.

3. Function Body
* + `write(1, &c, 1);` This line uses the `write` system call to write a character to the standard output.
	+ `1` File descriptor for standard output (stdout).
	+ `&c` Address of the character `c`.
	+ `1` Number of bytes to write (in this case, it's 1 byte for the character).

4. Explanation
* + The purpose of this function is to output a single character to the standard output. It utilizes the `write` system call to achieve this. The character to be written is passed to the function as an argument (`c`), and the `write` call writes it to the standard output.

```
#include <unistd.h>

void    ft_putchar(char c)
{
    write(1, &c, 1);
}
/*
int main()
{
    ft_putchar('c');
    return (0);
}
*/

```

# ex01/ft\_print\_alphabet.c

This code defines a function named `ft\_print\_alphabet` that prints the lowercase English alphabet. The `ft\_print\_alphabet` function prints the lowercase English alphabet to the standard output.

1. Header Inclusion
* + `#include <unistd.h>` This line includes the `unistd.h` header file, which is necessary for using the `write` function.

2. Function Declaration
* + `void ft\_print\_alphabet(void)` This line declares a function named `ft\_print\_alphabet` that takes no parameters (`void`) and returns `void` (meaning it doesn't return any value).

3. Function Definition
* + `{}` The curly braces `{}` enclose the body of the function, defining what the function does.

4. Function Body
* + `write(1, "abcdefghijklmnopqrstuvwxyz", 26);` This line uses the `write` system call to write the lowercase English alphabet to the standard output. Rewrite the alphabet in reverse for ex02.
	+ `1` File descriptor for standard output (stdout).
	+ `"abcdefghijklmnopqrstuvwxyz"` String containing the lowercase English alphabet.
	+ `26` Number of bytes to write (the length of the string).

5. Explanation
* + The purpose of this function is to print the lowercase English alphabet to the standard output. It uses the `write` system call to write the string `"abcdefghijklmnopqrstuvwxyz"` to the standard output, effectively printing the entire alphabet.

```
#include <unistd.h>

void    ft_print_alphabet(void)
{
    write(1, "abcdefghijklmnopqrstuvwxyz", 26);
}

int main(void)
{
    ft_print_alphabet();
    return (0);
}

```

# ex02/ft\_print\_reverse\_alphabet.c

ex02 is the same as ex01, you can just reverse the alphabet.

# ex03/ft\_print\_numbers.c

ex03 is the same as ex01 and ex02. However, you are required to print **digits**. The digits being 0-9.

# ex04/ft\_is\_negative.c

This code defines a function named `ft\_is\_negative` that determines whether an integer `n` is negative or non-negative. The `ft\_is\_negative` function prints 'N' if the input is negative and 'P' if the input is non-negative.

1. Header Inclusion
* + `#include <unistd.h>` This line includes the `unistd.h` header file, which is necessary for using the `write` function.

2. Function Declaration
* + `void ft\_is\_negative(int n)` This line declares a function named `ft\_is\_negative` that takes an integer parameter `n` and returns `void` (meaning it doesn't return any value).

3. Function Definition
* + `{}` The curly braces `{}` enclose the body of the function, defining what the function does.

4. Conditional Statement
* + `if (n < 0)` This line starts a conditional statement. It checks if the value of `n` is less than 0.

5. If Block
* + `write(1, "N", 1);` If `n` is less than 0, this line uses the `write` system call to write the character 'N' to the standard output.
	+ `1` File descriptor for standard output (stdout).
	+ `"N"` String containing the character 'N'.
	+ `1` Number of bytes to write (in this case, it's 1 byte for the character 'N').

6. Else Block
* + `else` If the condition in the `if` statement is not true (i.e., `n` is not less than 0), this block is executed.
	+ `write(1, "P", 1);` In this case, it uses the `write` system call to write the character 'P' to the standard output.
	+ `1` File descriptor for standard output (stdout).
	+ `"P"` String containing the character 'P'.
	+ `1` Number of bytes to write (in this case, it's 1 byte for the character 'P').

7. Explanation
* + The `ft\_is\_negative` function takes an integer `n` as an argument and checks whether it is negative or non-negative. If `n` is negative, it prints 'N' to the standard output; otherwise, it prints 'P'. The output is determined based on the result of the conditional check `if (n < 0)`.

```
#include <unistd.h>

void    ft_is_negative(int n)
{
    if ( n < 0)
        write(1, "N", 1);
    else
        write(1, "P", 1);
}

```