首頁 >後端開發 >C++ >在C語言中,變數的隱式初始化為0或1

在C語言中,變數的隱式初始化為0或1

王林
王林轉載
2023-08-25 17:13:111510瀏覽

在C語言中,變數的隱式初始化為0或1

We know that we need to declare variables before using it in our code. However, we variables can be assigned with 0 or 1 without declaration. In the following example we can see or 1 without declaration. In the following example we can see or .

Example

#include <stdio.h>
#include <stdlib.h>
x, y, array[3]; // implicit initialization of some variables
int main(i) {
   //The argument i will hold 1
   int index;
   printf("x = %d, y = %d</p><p></p><p>", x, y);
   for(index = 0; index < 3; index++)
      printf("Array[%d] = %d</p><p>", i, array[i]);
      printf("The value of i : %d", i);
}

Output

x = 0, y = 0
Array[0] = 0
Array[1] = 0
Array[2] = 0
The value of i : 1

有時候,如果某個陣列只初始化了一部分值,那麼剩下的值就會被設定為0。

#include <stdio.h>
#include <stdlib.h>
int main() {
   //The argument i will hold 1
   int index;
   int array[10] = {1, 2, 3, 4, 5, 6};
   for(index = 0; index < 10; index++)
      printf("Array[%d] = %d</p><p>", index, array[index]);
}

Output

Array[0] = 1
Array[1] = 2
Array[2] = 3
Array[3] = 4
Array[4] = 5
Array[5] = 6
Array[6] = 0
Array[7] = 0
Array[8] = 0
Array[9] = 0
#

以上是在C語言中,變數的隱式初始化為0或1的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除

相關文章

看更多