Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
6 registered members (TipmyPip, Niels, dBc, Ed_Love, 3run, 1 invisible), 17,577 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Variable initialization confusion #469629
11/26/17 00:28
11/26/17 00:28
Joined: Nov 2016
Posts: 55
Taipei
ytc Offline OP
Junior Member
ytc  Offline OP
Junior Member

Joined: Nov 2016
Posts: 55
Taipei
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.

Re: Variable initialization confusion [Re: ytc] #469631
11/26/17 12:37
11/26/17 12:37
Joined: Sep 2003
Posts: 929
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 929
For keeping the variable, it must be static. Here is it explained:

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

Re: Variable initialization confusion [Re: Spirit] #469647
11/27/17 12:48
11/27/17 12:48
Joined: Nov 2016
Posts: 55
Taipei
ytc Offline OP
Junior Member
ytc  Offline OP
Junior Member

Joined: Nov 2016
Posts: 55
Taipei
Thanks, I just had some confusion about the function of INITRUN and FIRSTRUN when setting variables.


Moderated by  Petra 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1