logo

C에서 0 또는 1로 변수의 암시적 초기화

C 프로그래밍 언어에서는 변수에 값을 할당하기 전에 변수를 선언해야 합니다. 예를 들어:
 // declaration of variable a and // initializing it with 0. int a = 0; // declaring array arr and initializing // all the values of arr as 0. int arr[5] = {0}; 
However variables can be assigned with 0 or 1 without even declaring them. Let us see an example to see how it can be done: C
#include  #include  // implicit initialization of variables a b arr[3]; // value of i is initialized to 1 int main(i) {  printf('a = %d b = %dnn' a b);  printf('arr[0] = %d narr[1] = %d narr[2] = %d'  'nn' arr[0] arr[1] arr[2]);  printf('i = %dn' i);  return 0; } 
산출:
a = 0 b = 0 arr[0] = 0 arr[1] = 0 arr[2] = 0 i = 1 

In an array if fewer elements are used than the specified size of the array then the remaining elements will be set by default to 0. Let us see another example to illustrate this. C
#include  #include  int main() {  // size of the array is 5 but only array[0]  // array[1] and array[2] are initialized  int arr[5] = { 1 2 3 };  // printing all the elements of the array  int i;  for (i = 0; i < 5; i++) {  printf('arr[%d] = %dn' i arr[i]);  }  return 0; } 
산출:
arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 0 arr[4] = 0 
퀴즈 만들기