<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 數組 > 原文: [https://www.programiz.com/c-programming/c-arrays](https://www.programiz.com/c-programming/c-arrays) #### 在本教程中,您將學習如何使用數組。 您將借助示例學習如何聲明,初始化和訪問數組的元素。 ![C arrays](https://img.kancloud.cn/c6/1a/c61add9b0bfcbadd56ed992d600391b8_740x400.png "C arrays") 數組是可以存儲多個值的變量。 例如,如果要存儲 100 個整數,則可以為其創建一個數組。 ```c int data[100]; ``` * * * ## 如何聲明數組? ```c dataType arrayName[arraySize]; ``` **例如,** ```c float mark[5]; ``` 在這里,我們聲明了一個浮點類型的數組`mark`。 其大小為 5。意味著,它可以容納 5 個浮點值。 重要的是要注意,數組的大小和類型一旦聲明就無法更改。 * * * ## 訪問數組元素 您可以按索引訪問數組的元素。 假設您如上所述聲明了數組`mark`。 第一個元素是`mark [0]`,第二個元素是`mark [1]`,依此類推。 ![C Array declaration ](https://img.kancloud.cn/eb/4c/eb4c062100a3ef9d0179da8aa61d7b70_362x111.png) #### **少量要點**: * 數組的第一個索引為 0,而不是 1。在此示例中,`mark[0]`是第一個元素。 * 如果數組的大小為`n`,則要訪問最后一個元素,將使用`n-1`索引。 在此示例中,`mark[4]` * 假設`mark[0]`的起始地址為`2120d`。 然后,`mark[1]`的地址將是`2124d`。 同樣,`mark[2]`的地址將是`2128d`,依此類推。 這是因為`float`的大小為 4 個字節。 * * * ## 如何初始化數組? 在聲明期間可以初始化數組。 例如, ```c int mark[5] = {19, 10, 8, 17, 9}; ``` 您也可以像這樣初始化一個數組。 ```c int mark[] = {19, 10, 8, 17, 9}; ``` 在這里,我們沒有指定大小。 但是,當我們使用 5 個元素進行初始化時,編譯器知道其大小為 5。 ![Initialize an array in C programming](https://img.kancloud.cn/0b/7a/0b7aee25ca81c203a5da9e4a70adc254_362x111.png) 這里, ```c mark[0] is equal to 19 mark[1] is equal to 10 mark[2] is equal to 8 mark[3] is equal to 17 mark[4] is equal to 9 ``` * * * ## 更改數組元素的值 ```c int mark[5] = {19, 10, 8, 17, 9} // make the value of the third element to -1 mark[2] = -1; // make the value of the fifth element to 0 mark[4] = 0; ``` * * * ## 輸入和輸出數組元素 這是如何從用戶那里獲取輸入并將其存儲在數組元素中的方法。 ```c // take input and store it in the 3rd element ?scanf("%d", &mark[2]); // take input and store it in the ith element scanf("%d", &mark[i-1]); ``` 這是打印數組單個元素的方法。 ```c // print the first element of the array printf("%d", mark[0]); // print the third element of the array printf("%d", mark[2]); // print ith element of the array printf("%d", mark[i-1]); ``` * * * ## 示例 1:數組輸入/輸出 ```c // Program to take 5 values from the user and store them in an array // Print the elements stored in the array #include <stdio.h> int main() { int values[5]; printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) { scanf("%d", &values[i]); } printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) { printf("%d\n", values[i]); } return 0; } ``` **輸出** ```c Enter 5 integers: 1 -3 34 0 3 Displaying integers: 1 -3 34 0 3 ``` 在這里,我們使用`for`循環從用戶那里獲取 5 個輸入并將它們存儲在一個數組中。 然后,使用另一個`for`循環,這些元素顯示在屏幕上。 * * * ## 示例 2:計算平均值 ```c // Program to find the average of n numbers using arrays #include <stdio.h> int main() { int marks[10], i, n, sum = 0, average; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i<n; ++i) { printf("Enter number%d: ",i+1); scanf("%d", &marks[i]); // adding integers entered by the user to the sum variable sum += marks[i]; } average = sum/n; printf("Average = %d", average); return 0; } ``` **輸出** ```c Enter n: 5 Enter number1: 45 Enter number2: 35 Enter number3: 38 Enter number4: 31 Enter number5: 49 Average = 39 ``` 在這里,我們計算了用戶輸入的`n`個數字的平均值。 * * * ### 訪問元素超出范圍! 假設您聲明了一個由 10 個元素組成的數組。 比方說 ```c int testArray[10]; ``` 您可以從`testArray[0]`到`testArray[9]`訪問數組元素。 現在,假設您嘗試訪問`testArray[12]`。 該元素不可用。 這可能會導致意外輸出(不確定的行為)。 有時您可能會遇到錯誤,而其他時候您的程序可能會正確運行。 因此,永遠不要訪問數組邊界之外的元素。 * * * ## 多維數組 在本教程中,您了解了數組。 這些數組稱為一維數組。 在下一個教程中,您將學習[多維數組(數組的數組)](/c-programming/c-multi-dimensional-arrays "C Multidimensional Array")。
                  <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>

                              哎呀哎呀视频在线观看