-3

I read that : static variable is initialised only once (unlike auto variable) and further definition of static variable would be bypassed during runtime. from the link.

then why I am getting error that "i was not declare " in the code given below. I wrote program such that the static variable "i" initialized only first time when c is equal to 0 after that c increases. I just want to know how the static variable really works and that's why i am declare static variable only once. My question is if static variable only declare once in each function call then why my code is not running and if it is necessary to declare it in each calling then why it does not initialise in each function call.

#include<stdio.h>

int c=0;

int test()
{
    if(c==0)
       {
           static int i=0;
       }
    c++;
    i++;
    if(i==5)
        printf("%d\n",i);
    else
        test();
}
int main()
{
    test();
    return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

6 Answers6

4

You define the variable inside the local scope of if. It has no scope outside. static only deals with the extent (aka lifetime).

Use curly braces and indention. Then you will see your mistake.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • Not the real problem. It looks like the OP is trying to declare i as a static variable, and wants to do so only for the first time through the function. It is not necessary to do that. – FredK Mar 02 '17 at 06:13
  • 1
    That might be. Solving that will lead to the solution of the assumed real problem. – Amin Negm-Awad Mar 02 '17 at 06:16
  • I just want to know how the static variable really works and that's why i am declare static variable only once. My question is if static variable only declare once in each function call then why my code is not running and if it is necessary to declare in each calling then why it does not intiallise in each function call. – Piyush Mittal Mar 05 '17 at 09:04
  • As said: You have a problem with the scope, not with the extent. `static` does not change the scope, so your problem has nothing to do with `static`. Repair the problem with the scope. (If you do not know the terms *scope* and *extent*, please read something about this, because it is important for C. – Amin Negm-Awad Mar 05 '17 at 14:45
1

Because the scope of i is just the if block in following code:

if(c==0)
    static int i=0;

same as

if(c==0)
{
    static int i=0;
}

i is not available outside the if block

sameerkn
  • 2,209
  • 1
  • 12
  • 13
0

If you put static variable inside of block or function then that variable will not longer available out of scope because without a curly brace {} only first statement after the if is executed. So it cannot be used anywhere else and compiler gives an error.

msc
  • 33,420
  • 29
  • 119
  • 214
0

To fix your compilation error, declare I outside the condition. And set it to zero inside the condition -

static int i;
if ( c == 0 )
    i = 0;

This will make the error go away.

But what you are trying to achieve doesn't require you to have a global variable c.

You can simply do

static int i = 0;
i++;

The first time it will be set to zero, hence forth it will not be initialized.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
-1

Its because , scope of variable 'i' is only inside (if) . So when you are incrementing 'i' , Compiler will throw error.

aks7
  • 431
  • 3
  • 12
-2

Adding the braces that others have mentioned will not fix the problem. The real solution is to just remove the if (c == 0) line completely. The static declaration of i should be the first line of the function.

What you want is this:

int test()
{
    static int i=1;

    if(i==5)
        printf("%d\n",i);
    else
        test();
}
FredK
  • 4,094
  • 1
  • 9
  • 11
  • 1
    Nobody said, that adding braces wil solve the problem. – Amin Negm-Awad Mar 02 '17 at 06:12
  • @Amin Of course they did. Even you said the answer was "Use curly braces and indention. " – FredK Mar 02 '17 at 06:16
  • 1
    No, nobody said that. – Amin Negm-Awad Mar 02 '17 at 06:17
  • I am quoting directly from your answer – FredK Mar 02 '17 at 06:17
  • 3
    The sentence in your quote does not say, that this will solve the problem. Moreover the next sentence is: "Then you will see your mistake." It is not: "then your mistake will be away." – Amin Negm-Awad Mar 02 '17 at 06:18
  • I just want to know how the static variable really works and that's why i am declare static variable only once. My question is if static variable only declare once in each function call then why my code is not running and if it is necessary to declare in each calling then why it does not intiallise in each function call. – Piyush Mittal Mar 05 '17 at 08:58