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

# ft\_print\_params

**Objective:**

Create a program that displays its given arguments, one per line, in the same order as they appear in the command line (excluding the program name itself, `argv`).

**Turn-in Directory:**

`ex01/`

**Files to Turn In:**

`ft_print_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. It iterates through the `argv` array, starting from the second element (`argv[1]`), as the first element (`argv`) is the program name. Each argument is printed to the standard output followed by a newline character.

**Code Implementation:**

```c
#include <unistd.h>

// Helper function to write a single character to standard output
void	ft_putchar(char c)
{
	write(1, &c, 1);
}

int	main(int argc, char **argv)
{
	int	i; // Loop counter for iterating through the arguments
	int	j; // Loop counter for iterating through the characters of each argument

	i = 1; // Start from the second argument (argv[1]), as argv[0] is the program name
	if (argc < 1) // Check if there are any arguments passed
		return (0); // If no arguments passed, exit the program
	while (argv[i]) // Loop through each argument until a NULL pointer is encountered
	{
		j = 0; // Reset the character counter for each argument
		while (argv[i][j]) // Loop through each character of the current argument
		{
			ft_putchar(argv[i][j]); // Print the current character
			j++; // Move to the next character
		}
		i++; // Move to the next argument
		write(1, "\n", 1); // Print a newline character after each argument
	}
	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
test1$
test2$
test3$
$>
```

**Edge Cases to Consider:**

* **No arguments**: If only the program name is provided (e.g., `./a.out`), nothing will be printed (other than a potential newline if you choose to include one).
* **Multiple arguments**: The program should correctly print all arguments, regardless of the number.
* **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_putchar` Function:**
   * A helper function is defined to write a single character to the standard output using the `write` function.
3. **Main Function:**
   * The `main` function takes `argc` (argument count) and `argv` (argument vector) as parameters.
   * An integer `i` is initialized to 1 to start iterating from the second element of `argv` (skipping the program name).
   * An integer `j` is initialized to 0; it is used to iterate through each character of the string
   * The outer `while` loop (`while (argv[i])`) continues as long as the current element of `argv` is not `NULL` (which indicates the end of the arguments).
   * The inner `while` loop (`while (argv[i][j])`) iterates through each character of the current argument string until a null terminator (`\0`) is encountered.
   * Inside the inner loop, `ft_putchar(argv[i][j])` prints the current character to the standard output.
   * After printing all characters of the current argument, `write(1, "\n", 1)` prints a newline character to move the cursor to the next line.
   * The outer loop increments `i` to move to the next argument.
   * The program returns 0 to indicate successful execution.

**Key Points to Remember:**

* Always start iterating from `argv[1]` to skip the program name.
* Use a loop to iterate through the arguments and print each one individually.
* Use a second loop to iterate through the caracters of the string and print each one individually.
* 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_print_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.
