# 三、 變數宣告

## 那如何配合 變數型式顯示?

名稱稱為 "輸出格式化"

我們之所以會用到是因為他本身是不法正常寫入字串內部執行，需要有擷取方式到字串中。\
下方是我們常見的格式化輸出。

* `%d` 或 `%i`：整數
* `%f`：浮點數
* `%c`：字元
* `%s`：字串 (即字元陣列)
* `%p`：指標位址

### 參考&#x20;

{% embed url="<https://docs.microsoft.com/zh-tw/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=vs-2019>" %}

### 結論

**int out content =已經是字串不能再更改 ，%d 告訴這個擷取逗號後的 i 值到 %d 內。**\
**所以會變成 => int out content 5**。

`int i =5;`  \
`printf(" int out content %d", i );`

### 補充

下方為 stdio.h 的函數

```c
    _Check_return_opt_
    _CRT_STDIO_INLINE int __CRTDECL printf(
        _In_z_ _Printf_format_string_ char const* const _Format,
        ...)

    #if defined _NO_CRT_STDIO_INLINE
    ;
    #else
    {
        int _Result;
        va_list _ArgList;
        __crt_va_start(_ArgList, _Format);
        _Result = _vfprintf_l(stdout, _Format, NULL, _ArgList);
        __crt_va_end(_ArgList);
        return _Result;
    }
    #endif
```

## Scanf() 輸入函式

`Scanf("%f", &miles);`&#x20;

{% hint style="info" %}
:man\_mage: **說明 :**

Scanf("格式", &變數);
{% endhint %}

呼叫 scanf() 複製資料至變數 miles，miles 資料 是從標準輸入裝置而取得資料。輸入裝置可能會是鍵盤之類的輸入裝置，電腦都會將資料傳送到miles 傳送。

### :hourglass\_flowing\_sand: `&` 字元作用

scanf()，是將&字元加在變數前，**& 是 位址運算元 的意思**，會告訴scanf() 要接收新的值的變數會在何處，若沒有加上 &，scanf 只會知道現有的值，卻不能知道記憶體位置，所以無法將新的值傳入現有的變數。

```c
printf("enter two integer >> ");
scanf("%d %d",&m , &n);
m =m+5;
n = (n++);
printf("m = %d \n n =%d\n",m ,n);
```

### 補充

在執行過程中，命令管理員(Command)遇到亂碼之類的時候，打上`chcp 65001` 轉 UTF-8 可以解決 中文亂碼問題。

![cmd](https://458705340-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCwBRwq0l9aLd8U3KnL%2F-MFLQMNwyX6xp-FUszUh%2F-MFLQQaaZlHZz4-tl_uF%2Fimage.png?alt=media\&token=dfc56cf2-d458-4fff-8c9d-6531e912a7af)

## 輸出函式-練習

### 1. 寫一個長方形面積

{% tabs %}
{% tab title="C " %}

```c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    //長方形面積 (長*寬)
    float width = 2.9;
    float height = 2.9;

    printf("面積 >> %d", (width * height));
    system("PAUSE");
    return 0;
}
\
```

{% endtab %}

{% tab title="C++" %}

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

int main(int argc, char const *argv[])
{
    //長方形面積 (長*寬)
    float width = 2.9;
    float height = 2.9;
    cout << width * height;
    system("PAUSE");
    return 0;
}
```

{% endtab %}
{% endtabs %}

### 2. BMI

**BMI值計算公式:** BMI = 體重(公斤) / 身高2(公尺2)

{% tabs %}
{% tab title="C" %}

```c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    // BMI值計算公式:    BMI = 體重(公斤) / 身高2(公尺2)
    int weight = 2.9;
    float height = 2.9;
    int Bmi;
    height /=100;
    Bmi= weight/(height*height);
    printf("Bmi = %d", Bmi);
    system("PAUSE");
    return 0;
}
```

{% endtab %}

{% tab title="C++" %}

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

int main(int argc, char const *argv[])
{
    //長方形面積 (長*寬)
    int weight = 2.9;
    float height = 2.9;
    int Bmi;
    height /= 100;
    Bmi = weight / (height * height);
    cout << "BMI = " << Bmi;
    system("PAUSE");
    return 0;
}
```

{% endtab %}
{% endtabs %}

### 3.輸出格式化數字

C/C++ 中，Printf() 格式字串中，可以在% 與 d 中間加入數字。此數字表示欄寬(field width)，是顯示數值行數。

```c
printf("Int out number = %3d \n", inches);
```

inches 位置是有保留 (%3d ) 3個列印空間(欄位)，印此未超過3個位置會看得出，數值有向右對齊的效果。

因次有三個結論

1. %3d  : 如果數值未滿時候，會有向右對齊。
2. %-3d : 如果數值未滿時候，會有向左對齊。
3. 若寬度不夠顯示數值，會自動擴充欄寬。
