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

# ft\_print\_program\_name

**Objective:**

Create a program that displays its own name followed by a newline.

**Turn-in Directory:**

`ex00/`

**Files to Turn In:**

`ft_print_program_name.c`

**Allowed Functions:**

`write`

**Prototype:**

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

* **`argc` (Argument Count)**:
  * `argc` is an integer that tells you *how many* arguments were passed to your program when it was executed.
  * It *always* includes the name of the program itself. So, if you run your program without any additional arguments, `argc` will be 1.
* **`argv` (Argument Vector)**:
  * `argv` is an *array of strings*. Each string in the array represents one argument passed to the program.
  * `argv` is *always* the name of the program as it was executed. For example, if you run `./myprogram`, then `argv` will contain the string `"./myprogram"`.
  * `argv[1]` will be the first argument after the program name, `argv` will be the second, and so on.
  * `argv[argc]` is *always* a `NULL` pointer. This is a convention that allows you to iterate through the arguments until you reach the end of the array.

`argc` and `argv` are the standard way to access command-line arguments in a C program. Here's a more detailed explanation:\
\
**Detailed Explanation:**

This program is designed to print the name with which it was executed. The program name is always stored as the first string in the `argv` array. The goal is to access this name and print it to the standard output, followed by a newline character.

**Code Implementation:**

```c
#include <unistd.h>

int	main(int argc, char **argv)
{
	char	*name;

	if (argc)
	{
		name = argv[0];
		while (*name != '\0')
		{
			write(1, name, 1);
			name++;
		}
	}
	write(1, "\n", 1);
	return (0);
}
```

**Example Usage:**

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

```bash
$> ./a.out | cat -e
./a.out$
$>
```

**Edge Cases to Consider:**

* **No arguments**: Even when no arguments are passed, the program name will always be the first element in `argv`.
* **Renamed executable**: If the executable is renamed, the output should reflect the new name.

**How It Works:**

1. **Include Header:** The `unistd.h` header is included because it contains the declaration for the `write` function.
2. **Main Function:**
   * The `main` function takes two arguments: `argc` (argument count) and `argv` (argument vector).
   * **`argc`**: An integer that represents the number of arguments passed to the program from the command line.
   * **`argv`**: An array of strings, where each string is an argument passed to the program. `argv` is always the name of the program itself. `argv[1]` is the first argument, `argv` the second, and so on.
3. **Access Program Name:**
   * `name = argv;`: assigns the pointer to the program name to the `name` variable.
4. **Print Program Name:**
   * A `while` loop iterates through each character of the program name string until it reaches the null terminator (`\0`).
   * `write(1, name, 1);`: Inside the loop, the `write` function is used to print each character to standard output (file descriptor 1).
   * `name++;`: Increments the pointer to the next character in the string.
5. **Print Newline:**
   * `write(1, "\n", 1);`: After printing the program name, a newline character is printed to move the cursor to the next line.
   * `return (0);`: Return code 0 indicates successful program execution.

**Key Points to Remember:**

* **`argc` and `argv`**: These are essential for accessing command-line arguments in C programs.
* **`argv`**: Always contains the program's name.
* **`write` function**: Used for printing to the standard output. It takes the file descriptor (1 for standard output), a pointer to the data to be written, and the number of bytes to write.

**Example:**

If you run the following command:

```bash
./myprogram hello world 123
```

Then, inside your C program:

* `argc` will be 4 (program name + 3 arguments)
* `argv` will point to the string `"./myprogram"`
* `argv[1]` will point to the string `"hello"`
* `argv` will point to the string `"world"`
* `argv` will point to the string `"123"`
* `argv` will be `NULL`

This explanation should give you a solid understanding of how `argc` and `argv` work!


---

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