> 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/c06/ft_rev_params.md).

# ft\_rev\_params

**Objective:**

Create a program that displays its given arguments in reverse order, one per line (excluding the program name itself, `argv`).

**Turn-in Directory:**

`ex02/`

**Files to Turn In:**

`ft_rev_params.c`

**Allowed Functions:**

`write`

**Prototype:**

```c
int main(int argc, char **argv);
```

**Detailed Explanation:**

This program takes command-line arguments and prints each one on a separate line, but in reverse order compared to how they were entered in the command line. It iterates through the `argv` array from the end (excluding `argv`, the program name) to the beginning, printing each argument followed by a newline.

**Code Implementation:**

```c
#include <unistd.h>

// Function to print a string followed by a newline
void	ft_putstr(char *str)
{
    // Iterate through the string until the null terminator is reached
	while (*str)
        // Write one character at a time to standard output
		write(1, str++, 1);
    // Write a newline character to standard output after the string
	write(1, "\n", 1);
}

int	main(int argc, char **argv)
{
    // Loop through the arguments in reverse order
	while (--argc)
        // Print the current argument
        // *(argv + argc) is equivalent to argv[argc], but uses pointer arithmetic
		ft_putstr(*(argv + argc));
    return (0);
}
```

**Example Usage:**

If the compiled executable is named `a.out`, running it with arguments would produce the following output:

```c
$> ./a.out test1 test2 test3 | cat -e
test3$
test2$
test1$
$>
```

**Edge Cases to Consider:**

* **No arguments**: If only the program name is provided (e.g., `./a.out`), nothing will be printed.
* **Multiple arguments**: The program should correctly print all arguments, regardless of the number, in reverse order.
* **Empty arguments:** An empty argument (e.g., `./a.out ""`) should print an empty line.

**How It Works:**

1. **Include Header:** The `unistd.h` header is included for the `write` function.
2. **`ft_putstr` Function:**
   * This helper function takes a string (`char *str`) as input.
   * It iterates through the string character by character using a `while` loop until it reaches the null terminator (`\0`).
   * Inside the loop, it calls the `write` function to print one character at a time to the standard output.
   * After printing the entire string, it prints a newline character () to move the cursor to the next line.
3. **Main Function:**
   * The `main` function takes `argc` (argument count) and `argv` (argument vector) as parameters.
   * The `while (--argc)` loop is used to iterate through the arguments in reverse order.
     * The `--argc` part decrements `argc` *before* the loop condition is checked. This means that in the first iteration, `argc` will be `argc - 1`.
     * The loop continues as long as `argc` is greater than 0. This effectively skips `argv` (the program name) and processes the remaining arguments in reverse order.
   * Inside the loop, `ft_putstr(*(argv + argc))` is used to print the current argument.
     * `*(argv + argc)` is equivalent to `argv[argc]`, but it uses pointer arithmetic to access the argument at the current index.
     * `ft_putstr` is called to print the argument string followed by a newline character.
   * The program returns 0 to indicate successful execution.

**Key Points to Remember:**

* Decrement `argc` *before* using it to access `argv` to iterate in reverse order and skip the program name.
* The `--argc` is crucial for starting at the last argument and moving towards the first.
* Use pointer arithmetic or array indexing to access the arguments in `argv`.
* Ensure that a newline character is printed after each argument to fulfill the requirement of printing one argument per line.


---

# 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/c06/ft_rev_params.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.
