> For the complete documentation index, see [llms.txt](https://beej-c-zhtw.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://beej-c-zhtw.netdpi.net/3.-bian-shu-yu-chen-shu-shi/3.1.-bian-shu/3.1.2-bian-shu-xing-bie.md).

# 3.1.2 變數型別

根據您學過的語言，您可能知道型別（type）的概念。但C在型別上有點講究，所以我們應該去吃點東西。

這裡是一些型別的範例，有些是最基本的：

| 型別種類                   | 範例                | C 的型別                                                                  |
| ---------------------- | ----------------- | ---------------------------------------------------------------------- |
| 整數（Integer）            | `3490`            | `int`                                                                  |
| 浮點數（Floating point）    | `3.14159`         | `float`                                                                |
| 單一字元（single Character） | `'c'`             | `char`                                                                 |
| 字串（String）             | `"Hello, world!"` | `char *`[34](https://beej.us/guide/bgc/html/split/footnotes.html#fn34) |

如果你有需要，C 會盡力自動在大部分的數值型別之間轉換。除此之外，所有的型別轉換都是手動進行的，尤其是在字串和數字之間。 在C裡面，幾乎所有的型別都是這些型別的變體。 您必須先宣告變數，並告知C該變數所包含的型別，才能使用變數。一旦宣告了變數的型別，之後便不能在程式實際執行時變更。你要把它設定成它的原型，直到它脫離範圍再被重新吸收進宇宙。 讓我們使用之前的「Hello， world」程式碼，並加入一些變數：

```c
#include <stdio.h>

int main(void)
{
    int i;    // Holds signed integers, e.g. -3, -2, 0, 1, 10
    float f;  // Holds signed floating point numbers, e.g. -3.1416

    printf("Hello, World!\n");  // Ah, blessed familiarity
}
```

就是那裡！我們已經宣宣告了一些變數，但我們還沒有使用它們，它們也都還沒有初始化。一個代表整數、另一個代表浮點數（基本上，如果您學過數學，浮點數就是實數）。

未初始化的變數具有不定的數值\[35]，它們必須初始化，否則您必須假設它們會自動包含一些無意義的數字。

> 這是C可以「找到你」的地方之一，根據我的經驗，大多數情況下，不確定值為零……但可能會因執行而有所不同！即使您看到是零，也絕對不要假設初值是零。使用變數之前，一律要明確地將變數初始化為某個值\[36]。

這是什麼啊？你想要儲存一些數字在這些變數嗎？真是瘋了！

咱們開始吧：

```c
int main(void)
{
    int i;

    i = 2; // Assign the value 2 into the variable i

    printf("Hello, World!\n");
}
```

殺手啊，我們已儲存值了，咱們印出來吧。

我們將傳遞兩個驚人的參數給printf()函式，第一個參數是字串，用來描述要印的內容及列印方式（稱為格式化字串），而第二個參數是要印出來的數值，亦即變數 i 中的內容。 printf()會搜尋格式字串，找出以百分比符號(%)開頭的各種特殊序列，告知列印內容。例如，如果找到 %d，它會尋找下一個傳遞的參數，並將它列印為整數。如果找到 %f，就會將值列印為浮點數。如果找到 %s，就會列印字串。 所以，我們可以列印出各種型別的值，例如：

```c
#include <stdio.h>

int main(void)
{
    int i = 2;
    float f = 3.14;
    char *s = "Hello, world!";  // char * ("char pointer") is the string type

    printf("%s  i = %d and f = %f!\n", s, i, f);
}
```

而輸出如下：

```c
Hello, world!  i = 2 and f = 3.14!
```

這個方式的 printf( ) 可能類似你熟悉的其他程式語言，有各種格式化字串或是參數化的字串。

<br>


---

# 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://beej-c-zhtw.netdpi.net/3.-bian-shu-yu-chen-shu-shi/3.1.-bian-shu/3.1.2-bian-shu-xing-bie.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.
