<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C 輸入輸出(I/O) > 原文: [https://www.programiz.com/c-programming/c-input-output](https://www.programiz.com/c-programming/c-input-output) #### 在本教程中,您將學習使用`scanf()`函數從用戶那里獲取輸入,并使用`printf()`函數向用戶顯示輸出。 ## C 輸出 在 C 編程中,`printf()`是主要的輸出函數之一。 該函數將格式化的輸出發送到屏幕。 例如, * * * ### 示例 1:C 輸出 ```c #include <stdio.h> int main() { // Displays the string inside quotations printf("C Programming"); return 0; } ``` **輸出** ```c C Programming ``` 該程序如何工作? * 所有有效的 C 程序都必須包含`main()`函數。 從`main()`函數的開始開始執行代碼。 * `printf()`是一種庫函數,用于將格式化的輸出發送到屏幕。 該函數在引號內打印字符串。 * 要在我們的程序中使用`printf()`,我們需要使用`#include <stdio.h>`語句包含`stdio.h`頭文件。 * `main()`函數內的`return 0;`語句是程序的“退出狀態”。 它是可選的。 * * * ### 示例 2:整數輸出 ```c #include <stdio.h> int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; } ``` **輸出** ```c Number = 5 ``` 我們使用`%d`格式說明符來打印`int`類型。 此處,報價內的`%d`將由`testInteger`的值替換。 * * * ### 示例 3:浮點和雙精度輸出 ```c #include <stdio.h> int main() { float number1 = 13.5; double number2 = 12.4; printf("number1 = %f\n", number1); printf("number2 = %lf", number2); return 0; } ``` **輸出** ```c number1 = 13.500000 number2 = 12.400000 ``` 要打印`float`,我們使用`%f`格式說明符。 同樣,我們使用`%lf`打印`double`值。 * * * ### 示例 4:打印字符 ```c #include <stdio.h> int main() { char chr = 'a'; printf("character = %c.", chr); return 0; } ``` **輸出** ```c character = a ``` 要打印`char`,我們使用`%c`格式說明符。 * * * ## C 輸入 在 C 編程中,`scanf()`是從用戶獲取輸入的常用函數之一。`scanf()`函數從標準輸入(例如鍵盤)讀取格式化的輸入。 * * * ### 示例 5:整數輸入/輸出 ```c #include <stdio.h> int main() { int testInteger; printf("Enter an integer: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0; } ``` **輸出** ```c Enter an integer: 4 Number = 4 ``` 在這里,我們使用了`scanf()`函數中的`%d`格式說明符來獲取用戶的`int`輸入。 當用戶輸入整數時,它存儲在`testInteger`變量中。 注意,我們在`scanf()`中使用了`&testInteger`。 這是因為`& testInteger`獲得了`testInteger`的地址,并且用戶輸入的值存儲在該地址中。 * * * ### 示例 6:浮點和雙精度輸入/輸出 ```c #include <stdio.h> int main() { float num1; double num2; printf("Enter a number: "); scanf("%f", &num1); printf("Enter another number: "); scanf("%lf", &num2); printf("num1 = %f\n", num1); printf("num2 = %lf", num2); return 0; } ``` **輸出** ```c Enter a number: 12.523 Enter another number: 10.2 num1 = 12.523000 num2 = 10.200000 ``` 我們分別對`float`和`double`使用`%f`和`%lf`格式說明符。 * * * ### 示例 7:C 字符 I/O ```c #include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c",&chr); printf("You entered %c.", chr); return 0; } ``` **輸出** ```c Enter a character: g You entered g. ``` 當用戶在上述程序中輸入字符時,字符本身不會被存儲。 而是存儲一個整數值(ASCII 值)。 當我們使用`%c`文本格式顯示該值時,將顯示輸入的字符。 如果我們使用`%d`顯示字符,則會打印它的 ASCII 值。 * * * ### 示例 8:ASCII 值 ```c #include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c", &chr); // When %c is used, a character is displayed printf("You entered %c.\n",chr); // When %d is used, ASCII value is displayed printf("ASCII value is % d.", chr); return 0; } ``` 輸出量 ```c Enter a character: g You entered g. ASCII value is 103. ``` * * * ## I/O 多個值 這是您可以從用戶那里獲取多個輸入并顯示它們的方法。 ```c #include <stdio.h> int main() { int a; float b; printf("Enter integer and then a float: "); // Taking multiple inputs scanf("%d%f", &a, &b); printf("You entered %d and %f", a, b); return 0; } ``` **輸出** ```c Enter integer and then a float: -3 3.4 You entered -3 and 3.400000 ``` * * * ## I/O 的格式說明符 從以上示例中可以看到,我們使用 * `int`的`%d` * `float`的`%f` * `double`的`%lf` * `char`的`%c` 這是常用的 C 數據類型及其格式說明符的列表。 | 數據類型 | 格式說明符 | | --- | --- | | `int` | `%d` | | `char` | `%c` | | `float` | `%f` | | `double` | `%lf` | | `short int` | `%hd` | | `unsigned int` | `%u` | | `long int` | `%li` | | `long long int` | `%lli` | | `unsigned long int` | `%lu` | | `unsigned long long int` | `%llu` | | `signed char` | `%c` | | `unsigned char` | `%c` | | `long double` | `%Lf` |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看