Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, AndrewAMD), 911 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 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 | chip programmers | 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