10
I am implementing an exercise of 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 question of implementation may seem very primary, but it really left me thinking. I must put global variables in exchange for readable code?
The examples below demonstrate the doubt, which still uses another global variable matrix
, which unfortunately can not be local. I know it is a beginner doubt, but I really do not know how to proceed in this case. Below exemplify my doubt:
Note: As the code got a little big posted a "sketch"
Example with enum
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 within 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?
- I put that
enum
in global scope as in the first example? - I do as in the second example declaring a
enum
only in main, and in the functions that use it I substitute for their integer values? - The insert
enum
inmain
and in all functions using it? - I don’t use
enum
, and only enforce their respective integers?
I really didn’t know I could pass enumerations for functions. I’ll try to take
matrix
from the global scope and pass it as a parameter to functions, since integer matrices are passed by reference should be easy to implement.– Rafael Bluhm
You can pass any kind of dice. Deep down you are passing one
int
. I’m not saying you shouldn’t use ma on C an enumeration has little real advantage. It makes the code more readable but the language doesn’t help to use it in a better way. In C++ and other more modern languages the enumeration is actually typed and the compiler can detect errors.– Maniero
I actually took the enumeration out of the code, because having to treat it as you describe it, I would have to make significant changes, because I was literally accounting for the values of each one, because there is a function that "rotates" such values to change direction. The only problem is that the enumeration gave greater readability to the code, but I can live with that.
– Rafael Bluhm