#include variables clarification

 

Can someone please clarify how in mq4 you are supposed to declare variables. I always use the variable i for loop counting as do nost people. If I declare the variable i in my main program as "int i = 0;" and have the same declaration in a "#include <xxxx.mq4>" then I get the error message that variable i has already been declared. If i do not put the declaration in the include file the I get the error message that the variable i has not been denfined.
So then I tried to make i an external variable in the main program expecting it to be available to all functions and all includes as well. however this is not the case.
How do you do what I am trying to do - I know I can do workarounds by changing one of the variable names to j but I am sure that the rules of functions should be that the variables within a function should be available only localally and therfore an include files variables are local to that function only.
Anyone able to help clarify for me as I also thought that declaration of extern i = 0 would make i available to all includes.
thanks

 
The '#include' statement is replaced by the contents of the included file by the pre-processor before compilation. This means that there are no special rules regarding variable declarations in included files. Just imagine the included file is part of the main file code... But you must take into consideration the place in your code where the '#include' statement is added (if a variable is declared in an included file, but used in the main file code before the '#include' statement, then that won't compile...).

If you can't get it to work, then I suggest you post your code here (attach the files).
 
gordon wrote >>
The '#include' statement is replaced by the contents of the included file by the pre-processor before compilation. This means that there are no special rules regarding variable declarations in included files. Just imagine the included file is part of the main file code... But you must take into consideration the place in your code where the '#include' statement is added (if a variable is declared in an included file, but used in the main file code before the '#include' statement, then that won't compile...).

If you can't get it to work, then I suggest you post your code here (attach the files).


Thats OK now thankyou, I assumed that the include file code (including variable declarations) was placed at the point in the code where the function call for the include file is placed. Therefore I wrongly assumed that the variable name that is repeated in the include file would be local to the include file. So this means that when using included files no variables with the same name can be used as the variables are in fact effectively global. (I think!)
 
BigAl:
So this means that when using included files no variables with the same name can be used as the variables are in fact effectively global. (I think!)

Yes, just as you can't use 2 global variables with the same name in your main file. Again - the #include statement is replaced by the content of the included file just before compilation... It's as if you are compiling one big file, no matter how many files are included...

 
you should not use global variables. There is no need to declare a variable used in a for loop as global. You cannot have a for loop in the global scope anyways (in fact you cannot have any code running in the global scope), it is always inside a function, so just declare your variables inside the same functions where they are used and use global variables only as a last resort when there is no other reasonable way to persist data between the ticks.
Reason: