Is compile time initialization only happens when we give value to the variable during variable declaration.
To be more specific,
What is the difference between initializing variable during declaration int a=2
and after declaration int a; a=2
?
With the MicroC profile, you can specify that elements should be initialized at compile time. Compile-time initialization provides the following benefits: Ability to allocate data to ROM. Saving of CPU cycles at application startup. Ability to allocate data to specific memory segments.
Initializing a variable means specifying an initial value to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.
Run time Array initialization Using runtime initialization user can get a chance of accepting or entering different values during different runs of program. It is also used for initializing large arrays or array with user specified values. An array can also be initialized at runtime using scanf() function.
Initialization of static variables happens in two consecutive stages: static and dynamic initialization. Static initialization happens first and usually at compile time.
What is the difference between initializing variable during declaration
int a=2
and after declarationint a; a=2
?
The difference is that the second one is not an initialization. It's an assignment. When it comes to your very simple example, there is no difference in practice.
A big difference is that some initialization tricks are not available on regular assignments. Like for instance array and struct initialization. You'll need C99 or higher.
int x[5] = {1, 2, 3, 4, 5}; // Ok
x = {1, 2, 3, 4, 5}; // Not ok
struct myStruct {
int x;
char y;
};
struct myStruct a = {1, 'e'}; // Ok
a = {1, 'e'}; // Not ok
a = (struct myStruct) {1, 'e'}; // But this is ok
struct myStruct b = {.y = 'e'}; // Also ok
b = (struct myStruct) {.y = 'e'}; // Even this is ok
b = {.y = 'e'}; // But not this
You can assign arrays with a little trick. You wrap the array in a struct. However, I would not recommend this for this purpose. It's not worth it.
struct myArrayWrapper {
char arr[5];
};
struct myArrayWrapper arr;
arr = (struct myArrayWrapper) {{1,2,3,4,5}}; // Actually legal, but most people
// would consider this bad practice
And if you have declared a variable as const
then you cannot change it later, so they "have" to be initialized.
const int x = 5; // Ok
x = 3; // Not ok, x is declared as const
const int y; // "Ok". No syntax error, but using the variable is
// undefined behavior
int z = y; // Not ok. It's undefined behavior, because y's value
// is indeterminate
Constant variables are not strictly required to be initialized, but if you don't, they become completely worthless.
It also affects static variables in a special way.
void foo() {
static int x = 5; // The initialization will only be performed once
x = 8; // Will be performed everytime the fuction is called
So this function will return 1 if it has been called before and 0 otherwise:
int hasBeenCalled() {
static int ret = 0;
if(!ret) {
ret = 1;
return 0;
}
return 1;
}
Another thing about static variables is that if they are not explicitly initialized, they will be initialized to zeros. Local variables (variables with automatic storage) will contain garbage values. Also, do note that global variables are automatically static.
static int x; // Will be initialized to 0 because it's static
int y; // Is also static, because it's global
auto int z; // Not ok. You cannot declare auto variables in global space
int main(void)
{
int a; // Will be auto as b
auto int b;
static int c;
int d;
d = a; // Not ok because b is not initialized.
d = b; // Not ok
d = c; // Ok because static variables are initialized to zero
d = x; // Ok
d = y; // Ok
In practice, you NEVER use the auto
keyword in C. Actually, there are no situations where auto
is legal to use where it would not default to auto
if you omit it. That's not true for static
.
You can read about initialization in chapter 6.7.9 in the C standard. Assignments are found in chapter 6.5.16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With