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 this.
example#include <stdio.h>#include <stdlib.h>x, y, array[3]; // implicit initialization of some variablesint main(i) { //the argument i will hold 1 int index; printf("x = %d, y = %d
", x, y); for(index = 0; index < 3; index++) printf("array[%d] = %d
", i, array[i]); printf("the value of i : %d", i);}
outputx = 0, y = 0array[0] = 0array[1] = 0array[2] = 0the 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
", index, array[index]);}
outputarray[0] = 1array[1] = 2array[2] = 3array[3] = 4array[4] = 5array[5] = 6array[6] = 0array[7] = 0array[8] = 0array[9] = 0
以上就是在c语言中,变量的隐式初始化为0或1的详细内容。