Reputation: 41
#include <stdio.h>
main()
{
int i = 10;
static int a = i;
if(a==10)
printf("yes 1st comparision is equal\n");
else
printf("no 1st comparision is not equal\n");
if(i==10)
printf("yes 2nd comparision is equal\n");
else
printf("no 2nd comparision is not equal\n");
a=15;
if(a==i)
printf("yes 3rd comparision is equal\n");
else
printf("no 3rd comparision is not equal\n");
}
Why is a
re-initialized to 15, and the output is no 3rd comparision is not equal
whereas the output should be yes 3rd comparision is equal
?
Upvotes: 0
Views: 142
Reputation: 13494
In the below example static int count1 = 0;
is initialization(assigning values at declaration). And count1++;
is equivalent to count1 = count1 + 1;
and this is assignment not initialization.
While calling count
function first time static int count1 = 0;
will be executed and 0 will be assigned, and also count1++
will also assign 1
to the staitc variable count1
. While calling count
function second time static int count1 = 0;
will not be executed and count1++
will be executed and 2
will be assigned to count1
.
void count(void)
{
static int count1 = 0;
int count2 = 0;
count1++;
count2++;
printf("\nValue of count1 is %d, Value of count2 is %d", count1, count2);
}
Initialization means assigning a value to a variable during the declaration of that variable(during intial stage). Assigining values after declaration is just assignment not initialization.
For a static variable initialization will be done only once, but the assignment can be done for any number of times.
Now in your first program, you are doing a=15;
, you are thinking like =
operation is not possible in 2nd time and ++
operation is possible for many times for a static variable.
In your program a=15;
can be written as below also with ++
operation.
for(j = 0; j = 15; j++)
{
a++;
}
So assignment (=
) operation is valid on a static variable even in second time also.
Upvotes: 0
Reputation: 65
The answer to your question "why "a" was initialized to 15" is you are assigning the value to "a". It was initialized to 10 when you declared it as
static int a = i;
where i=10. When you do
a=15;
the value of "a" changes. You are not declaring "a" again. You are not changing the value of "i" either. Hence, the comparison fails. If you do not want to change the value of "a" declare it as "const" variable.
Hope this answers your query. Aditya
Upvotes: 0
Reputation: 104708
what you are comparing are two separate values.
what you seem to expect, judging by your question, is referencing (often achieved using a pointer).
this example has nothing to do with static, because the function main
is entered only once.
if reference are what you are after, this is a very simplified usage:
#include <stdio.h>
int main() {
int i = 10;
int* const a = &i;
if (*a == 10)
printf("yes 1st comparision is equal\n");
else
printf("no 1st comparision is not equal\n");
if (i == 10)
printf("yes 2nd comparision is equal\n");
else
printf("no 2nd comparision is not equal\n");
*a = 15;
if (*a == i)
printf("yes 3rd comparision is equal\n");
else
printf("no 3rd comparision is not equal\n");
return 0;
}
Upvotes: 1