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).
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).
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).
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!)
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.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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