> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/c-c++-for-embedded-programming/principle-c-c++-programming/1.-arguments.md).

# Arguments

<div align="center"><figure><img src="/files/TsZWIVEqkhRoRHScHxq8" alt=""><figcaption><p>Ref: <a href="https://www.programiz.com/cpp-programming/default-argument">https://www.programiz.com/cpp-programming/default-argument</a></p></figcaption></figure></div>

### 1. การรับ Argument Input ผ่าน command line

{% code title="myarg.cpp" %}

```cpp
#include <iostream>
using namespace std;

int main( int argc, char** argv )
{
    cout << "You have entered " << argc << " arguments: " << endl;
    for (int i = 0 ; i < argc ; i++) {
    cout << "argv " << i << " is: " << argv[i] << endl;
    }
    return 0;
}

```

{% endcode %}

จากข้างบนตัว argc เป็นตัวแปรเก็บจำนวนของอาร์กิวเม้นต์ และ argv เป็นตัวแปรชนิดอะเรย์ที่เก็บข้อความแต่ละอาร์กิวเม้นต์&#x20;

หลังจากนั้นทำการคอมไพล์โปรแกรม และทดสอบการเรียกโปรแกรมพร้อมการส่งผ่านค่าอาร์กิวเม้นต์ดังนี้

```c
$ g++ myarg.cpp -o myarg
$ ./myarg arg1 arg2
```

ผลลัพธ์จากการรันโปรแกรม `myarg`

{% code title="Output" %}

```c
You have entered 3 arguments: 
argv 0 is: ./myarg
argv 1 is: arg1
argv 2 is: arg2
```

{% endcode %}

### 2. ตัวอย่างการรับค่า argument จาก **ข้อความ** --เป็น-->`ตัวเลข`

{% code title="temp\_converter.cpp" %}

```cpp
// Celsius to Fahrenheit conversion

#include <iostream>
using namespace std;
 
float celsius;
float fahrenheit;

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

    if (argc != 2){
        fprintf(stderr, "usage: ./temp_converter <Celsius's value>\n");
        exit(1);
    }
    celsius = atof(argv[1]); //  // assign value to addr stored in celsius
    fahrenheit = celsius * 1.8 + 32; // Celsius to Fahrenheit conversion

    cout << "It's " << fahrenheit << " fahrenheit" << endl; 
    
    return 0;
}
```

{% endcode %}

หลังจากนั้นทำการคอมไพล์โปรแกรม และทดสอบการเรียกโปรแกรมพร้อมการส่งผ่านค่าอาร์กิวเม้นต์ดังนี้

```c
$ g++ temp_converter.cpp -o temp_converter
$ ./temp_converter
usage: ./temp_converter <Celsius's value>
$./temp_converter 32
```

ผลลัพธ์จากการรันโปรแกรม `temp_converter`

```c
It's 89.60 fahrenheit
```


---

# 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, and the optional `goal` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/principle-c-c++-programming/1.-arguments.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
