Implicit initialization of variables with 0 or 1 in C
In C programming language, the variables should be declared before a value is assigned to it.
For Example:
// 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:
#include <stdio.h> #include <stdlib.h> // implicit initialization of variables a, b, arr[3]; // value of i is initialized to 1 int main(i) { printf("a = %d, b = %d\n\n", a, b); printf("arr[0] = %d, \narr[1] = %d, \narr[2] = %d," "\n\n", arr[0], arr[1], arr[2]); printf("i = %d\n", 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.
#include <stdio.h> #include <stdlib.h> 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] = %d\n", i, arr[i]); } return 0; } |
arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 0 arr[4] = 0
Recommended Posts:
- Initialization of static variables in C
- Initialization of global and static variables in C
- Initialization of variables sized arrays in C
- Implicit return type int in C
- Implicit Type Conversion in C with Examples
- Difference between Static variables and Register variables in C
- Initialization of data members
- Initialization of a multidimensional arrays in C/C++
- Variables in C++
- Static Variables in C
- Variables and Keywords in C
- Can Global Variables be dangerous ?
- Constants vs Variables in C language
- Operations on struct variables in C
- How will you show memory representation of C variables?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


