Linux C Programming Tutorial Part 17: Variable Initialization
Initialization of variables is something which we have been doing throughout this ongoing C programming tutorial series so far, but we never really discussed it explicitly. Well, that changes now as we'll be discussing variable initialize in a bit of detail here.
So what exactly is initialization? Well, as the name suggests, when a value gets assigned to a variable for the first time, that is known as variable initialization. For example:
int a = 1;
Now, this is also known as explicit initialization. Reason being, even if you don't assign a value to a variable like 'a', and try to print the variable's value, you'll see some random value.
For example, consider the following code:
#include <stdio.h>
int main()
{
int a;
printf("a = %d", a);
return 0;
}
When I executed this code, I got 'a' as '0'. But this will not always be true because automatic variables like 'a' are initialized to random values in the absence of explicit initialization. That's the reason it's always suggested to explicitly initialize them with a value, say '0', in order to avoid a garbage value being used mistakenly.
Note that it's only automatic variables that get garbage value if left uninitialized. Static and global variables get '0' as their initial value even if you don't explicitly assign a value. Here's an example:
int x;
void func();
{
static int b;
//statements
b++;
}
int main()
{
int a;
//statements
func();
return 0;
}
In the code above, it's guaranteed that variables 'x' and 'b' will be initialized to '0', but there is no guarantee of what value the variable 'a' will contain.
Moving on, you can only initialize static and global variables with constant values. In other words, it's not possible to assign values to static and global variables in terms of other variables.
Here's an example:
#include <stdio.h>
int k;
int x = k;
void func()
{
int z = 2;
static int b = z;
b++;
}
int main()
{
int a;
printf("a = %d", a);
func();
return 0;
}
When I compiled this code, I got the following errors:
main.c:12:9: error: initializer element is not constant int x = k; ^ main.c: In function ‘func’: main.c:18:20: error: initializer element is not constant static int b = z; ^
As 'x' is global and 'b' is static, you can see the compiler clearly says the initializer elements is not constant. So this reaffirms the fact that only constants can be used to initialize global and static variables.
Moving on, arrays can be also initialized in different ways. For example, one way is to initialize is:
int arr[] = {1,2,3,4,5};
This way, the size of the array is calculated by counting the number of initializer elements. So in this example, array 'arr' has a size of 5, with values 1-5 as its elements (in that order).
Next, let's say the size of array is predefined, and you initialize the array using the method above. Here's an example:
int arr[5] = {1,2,3,4};
Then, in this case, the first four elements will be picked up from the list provided within {}, and the final element will be '0'. So yes, you can provide fewer elements than the size of the array in this manner, and let the compiler take care of the rest by initializing them with '0'.
For example, the following program:
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5};
int i = 0;
for(i=0;i<10;i++)
printf("%d ", arr[i]);
return 0;
}
produces the following output:
1 2 3 4 5 0 0 0 0 0
Conclusion
In this tutorial, we discussed a bit about initialization of variables. I'd encourage you to try out the examples as well as concepts discussed here. In case you have any doubt or query, you're welcome to drop in a comment below.