# Variable and Operator

## การแสดงค่า variable ในภาษา c++

ในภาษา C++ เราสามารถเรียกใช้ library ที่ชื่อว่า  \<iostream> เพื่อให้สามารถใช้คำสั่ง std::cout ในการแสดงผลบนหน้าจอคอมพิวเตอร์,  std::cin ในการรับค่ามาจากคีย์บอร์ดได้ และ std::endl สำหรับจบการแสดงผลแล้วขึ้นบรรทัดใหม่<br>

**คำสั่ง เรียกใช้ library iostream**

```cpp
#include <iostream>
```

**ตัวอย่าง การแสดงผลข้อมูล**

```cpp
#include <iostream>
int main() {
int a=0;
float g = 1.0;
  std::cout << a << " Test" << g <<std::endl; 
  /*prints 'a' is int a data type, 
    "Test" is a message and 
    'g' is float a data type to the output. */
  return 0;
}
```

\
&#x20;  สำหรับคำสั่ง std ย่อมาจาก standard library ถ้าต้องการใช้คำสั่ง cout โดยไม่ระบุ std หน้า cout หรือ cin ทุกครั้ง สามารถเพิ่มคำสั่ง using namespace เข้าไปดังนี้

```cpp
using namespace std;
```

**ตัวอย่าง**

```cpp

#include <iostream>
using namespace std;
int main() {
int a=0;
float g = 1.0;
  cout << a << " Test" << g <<endl; 
  /*prints 'a' is int a data type, 
    "Test" is a message and 
    'g' is float a data type to the output. */
  return 0;
}
```

&#x20;        สำหรับตัวแปรประเภท char ที่จะรับตัวอักษรได้เพียง 1 ตัวอักษร ถ้าต้องการรับเป็นข้อความจะต้องมีการกำหนดความยาว array ให้กับตัวแปร char หรือจะเรียกใช้ library string เพื่อให้สามารถกำหนดตัวแปรเป็น string สำหรับรับข้อความ ได้&#x20;

```cpp
#include <string>
```

**ตัวอย่าง** การใช้ cin รับค่าข้อความจากคีย์บอร์ดและแสดงผลโดยใช้คำสั่ง cout

```cpp

#include <iostream>
#include <string> 
using namespace std;
int main() {
string name;
  cout<< "what is your name " <<endl; 
  cin >> name ; // get your name from keyboard
  cout<< "Hi " << name <<endl;
  
  return 0;
}
```

ถ้าต้องการให้รับข้อความทั้งบรรทัด โดยรับค่าหลังเว้นวรรคได้สามารถใช้ฟังก์ชัน getline() ได้ ตัวอย่าง การรับข้อความทั้งบรรทัดโดยใช้ ฟังก์ชัน getline()

```cpp

#include <iostream>
#include <string> 
using namespace std;
int main() {
string name; 
 cout<< "please enter Name and Lastname " <<endl; 
 getline(cin,name);
 cout << " Hi " << name <<endl;
 return 0;
}
```

## Escape character in c ++

ในการแสดงผลข้อความถ้าต้องการใส่อักขระเราจะไม่สามารถใส่ตรงๆ เพื่อให้แสดงผลได้ ดังนั้นต้องใส่เครื่อง \ ไว้ข้างหน้าตัวอักขระพิเศษนั้น และยังมีตัวอักษรบางตัวเมื่อมีเครื่องหมาย \ นำหน้าจะกลายเป็นคำสั่งในการแสดงผล เช่น การเว้นวรรค หรือ ขึ้นบรรทัดใหม่ เป็นต้น สำหรับที่ใช้ทั่วไปมีดังนี้<br>

| Escape character | Represents                                 |
| ---------------- | ------------------------------------------ |
| \a               | Bell (alert)                               |
| \b               | Backspace                                  |
| \f               | Formfeed                                   |
| \n               | New line                                   |
| \r               | Carriage return                            |
| \t               | Horizontal tab                             |
| \v               | Vertical tab                               |
| \\'              | Single quotation mark                      |
| \\"              | Double quotation mark                      |
| \\\\             | Backslash                                  |
| \\?              | Literal question mark                      |
| \0               | ASCII character in octal notation          |
| \x hh            | ASCII character in hexadecimal notation    |
| \x hhhh          | Unicode character in hexadecimal notation. |

**ตัวอย่าง** การใช้ Escape character&#x20;

```cpp
#include <iostream>
using namespace std;
int main()
{
   
    cout << " \bhi\n"
         << "   hi\n"
         << " \thi\n"
         << " \x09hi\n"
         << "\x020\x020\x020hi\n"
         << " \x08hi\n";
    return 0;
}
```

**output**&#x20;

![](https://paper-attachments.dropbox.com/s_34C7DC72FBFDD20BEA7E0D178F0A57F6708834C7CC045486AA9B83B9CFD62CAB_1611739382119_image.png)

ถ้าต้องการใช้ printf แบบภาษา c ใน iostream library ภาษา c++  สามารถเรียกใช้ได้เลย หรือเพื่อให้สามารถนำไป complie ในภาษา c จะต้องเรียก library &#x20;

```cpp
#include <stdio.h>  
```

**ตัวอย่าง** การใช้ printf() ใน c++

```cpp
#include <stdio.h>
int main()
{
   printf ("Characters: %c %c \n", 'a', 65);
   printf ("Decimals: %d %ld\n", 1977, 650000L);
   printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
   printf ("floats: %.2f %E \n", 3.1416, 3.1416);
   printf ("%s \n", "A string");
   return 0;
}
```

**Output**

![](https://paper-attachments.dropbox.com/s_34C7DC72FBFDD20BEA7E0D178F0A57F6708834C7CC045486AA9B83B9CFD62CAB_1611740286943_image.png)

## Operator

Operator หรือตัวปฏิบัติการ แปลความง่ายๆคือเครื่องหมายต่างๆที่ใช้กับตัวแปรทางตรรกศาสตร์เช่น มากกว่าน้อยกว่า มีค่าเท่ากัน รวมถึงเครื่องหมายต่างทางคณิตศาสตร์เช่น บวก ลบ คูณ หาร ซึ่งจะอธิบายออกมาเป็นตามตารางดังนี้

| SYMBOL |        MEANING       |
| :----: | :------------------: |
|    +   |          บวก         |
|    -   |          ลบ          |
|   \*   |          คูณ         |
|    /   |          หาร         |
|    %   |   หารแล้วนำเศษไปใช้  |
|   ++   | เพิ่มค่าจากเดิมไป +1 |
|   --   |   ลดค่าจากเดิมไป -1  |
|   ==   |        เท่ากัน       |
|   !=   |      ไม่เท่ากัน      |
|    >   |        มากกว่า       |
|    <   |       น้อยกว่า       |
|   >=   |  มากกว่าหรือเท่ากับ  |
|   <=   |  น้อยกว่าหรือเท่ากับ |
|   &&   |          และ         |
|  \|\|  |         หรือ         |
|    !   |         นิเสธ        |

ตัวอย่างการใช้ operator ทางด้านคณิตศาสตร์

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

int main (){
    int A = 4;
    int B = 5;
    int C = 11;

    cout<<"A-B = "<< A-B <<endl;
    cout<<"A+B = "<< A+B <<endl;
    cout<<"B/A = "<< B/A <<endl;
    cout<<"A/B = "<< A/B <<endl;
    cout<<"A%B = "<< A%B <<endl;
    cout<<"B\%C = "<< B%C <<endl;
    cout<<"B++ = "<< B++ <<endl;
    cout<<"++C = "<< ++C <<endl;

    return 0;
}
```

ผลการรัน

![](/files/-MhCtpqRKFG3YupBrs1n)

ส่วน Operator อื่นๆนั้นจะได้พบในหัวข้อถัดไปในส่วนหัวข้อ If and Ifelse

## แหล่งอ้างอิง :

{% embed url="<https://www.codegrepper.com/code-examples/cpp/print+c%2B%2B>" %}

{% embed url="<https://preecha11th.wordpress.com/2011/11/08/%E0%B9%81%E0%B8%A3%E0%B8%81%E0%B9%80%E0%B8%A3%E0%B8%B4%E0%B9%88%E0%B8%A1%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%A3%E0%B8%B9%E0%B9%89>" %}

{% embed url="<https://www.austincc.edu/rickster/COSC1320/handouts/escchar.htm>" %}

{% embed url="<https://preecha11th.wordpress.com/2011/11/17/string-%E0%B9%83%E0%B8%99-c>" %}

{% embed url="<https://www.tutorialspoint.com/cplusplus/cpp_operators.htm>" %}


---

# Agent Instructions: 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://docs.aic-eec.com/c-c++-for-embedded-programming/principle-c-c++-programming/variable.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.
