Variable initialization confusion

Posted By: ytc

Variable initialization confusion - 11/26/17 00:28

I want to keep a variable's value to next call of run(), if I ran the code below for 2 bars period

Code:
function run(){
  //1. if(is(INITRUN)) int x=0;
  //2. if(is(FIRSTRUN)) int x=0;
  //3. int x=0;
  //4. static int x=0;
  
  printf("%i",x);
  x = 1;
  printf("%i",x);
}


#1, 2 and 3 got me 0101, implying x was set again at each bar run.
#4 got me 0111, which is what I want.

I thought if(is(INITRUN)) and if(is(FITSTRUN)) would only set variable x once.
Posted By: Spirit

Re: Variable initialization confusion - 11/26/17 12:37

For keeping the variable, it must be static. Here is it explained:

http://www.geeksforgeeks.org/static-variables-in-c/
Posted By: ytc

Re: Variable initialization confusion - 11/27/17 12:48

Thanks, I just had some confusion about the function of INITRUN and FIRSTRUN when setting variables.
© 2024 lite-C Forums