Home > Backend Development > C++ > body text

In C language, variables are implicitly initialized to 0 or 1

王林
Release: 2023-08-25 17:13:11
forward
1480 people have browsed it

In C language, variables are implicitly initialized to 0 or 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 this.

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);
}
Copy after login

Output

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

Sometimes, if an array is initialized with only part of the values, the remaining values ​​will be set to 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]);
}
Copy after login

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
Copy after login

The above is the detailed content of In C language, variables are implicitly initialized to 0 or 1. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!