I am implementing an exercise in the Deitel book, How to Program C, 6th edition, the Logo problem in Chapter 6. It was an interesting question with legal logical problems etc. The only doubt of implementation may seem very primary, but it really made me thoughtful. Should I put global variables by exchanging a readable code?
The examples below demonstrate the doubt, which still uses another global variable matrix
, which unfortunately can not be local. I know it's a beginning doubt, but I really do not know how to proceed in this case. Below is my question:
Note: As the code got a bit large I posted a "sketch"
Example with% global%:
#include <stdio.h>
#define SIZE 25
/*variável global necessária para que as funções escrevam na matriz */
char matrix[SIZE][SIZE];
/* Aqui enum é global podendo ser utilizado nas funções*/
enum direction {DOWN, RIGHT, UP, LEFT};
..."Protótips de funções" ...
int main(void){
..."Aqui utiliza-se DOWN, RIGHT , UP, LEFT" ...
return 0;
}
void funcaoExemplo(int var){
if(var == DOWN) ...
... "e utilizam-se também as outras variáveis de enum"
... "como RIGTH , UP e LEFT."
}
The second case is with enum inside the main function:
Example with enum
local:
#include <stdio.h>
#define SIZE 25
/*variável global necessária para que as funções escrevam na matriz */
char matrix[SIZE][SIZE];
..."Protótips de funções" ...
int main(void){
/* Aqui enum é local podendo ser utilizado somente em main*/
enum direction {DOWN, RIGHT, UP, LEFT};
..."Aqui utiliza-se DOWN, RIGHT , UP, LEFT" ...
return 0;
}
void funcaoExemplo(int var){
if(var == 0) ... /* equivalendo a DOWN */
... "aqui se utilizam os valores correspondentes"
... "como 1 , 2 e 3 no lugar de RIGTH , UP e LEFT respectivamente"
}
How should I proceed?
enum
in global scope as in the first example? enum
only in main, and in the functions that use it do I substitute for its integer values? enum
in enum
and in all functions that use it? main
, and only enforce their respective integers?