Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (howardR, AndrewAMD, EternallyCurious, Petra, 1 invisible), 791 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 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