Conversation
jerrettl
left a comment
There was a problem hiding this comment.
A lot of really good information here. I learned a good bit about enums from reading through this. I have a few suggestions on semantics for you to make it super clear what's happening on syntax. I hope this isn't too nitpick-y, I am just a big syntax person. Further details ahead:
Committing due to weather.
Made enums folder and moved tutorial in. Wrote the first part of the introduction. More research required.
Added another section. Still a work in progress, but open to suggestions. Pretty hard topic to write on.
Added section on errors and duplicate values.
Accidentally learned a lot more about enums, so added quite a bit. Variable declaration, typedef, and some rearranging of information.
Small update with naming convention fixes. Not complete.
A handful of changes. Fixed the rest of the naming convention issues, added a note about splitting up enum variable types (gross), and adjusted the typedef name in the typedef section. Did not mention the thing about global variable best practice. This can be added if needed, however.
Hopefully the final battle.
Made a single Enum practice problem that I've had in my head since working on the tutorial. Uses typedef.
Added note about stdlib.h for the program to run without implicit declaration.
Added the `typedef` syntax for enums. I gotta stop working on this it just keeps getting more wild.
Reread through my enums tutorial and found an funky sentence. Small edit.
Lots of updates here. Some syntax corrections, a lot of best practices fixes, language updates, and a new section on what not to do. Hungry for feedback.
|
Tutorial is complete. Please provide feedback. |
jerrettl
left a comment
There was a problem hiding this comment.
Got a chance to take a look. Thank you for all the work on this particularly crunchy section. All I would say is that (as we have previously discussed), the enumeration naming conventions give me the willies. I believe some of the conventions are inconsistent within the same document, so it may want to be double-checked.
I looked up the enumeration section from the GNU C manual (probably the only thing close to an "official" style guide), and they use snake_case.
https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Enumerations
If some style guide for C can be shown that makes sense and is consistent, I will accept it, but since these pages will essentially act as a "golden standard" for how enums should be written in C, I don't know if I can accept any hint at inconsistency in good conscience. I'm just picky like that 🙃
ALSO don't forget to add these pages to the table of contents.
Thank you for all the work again and dealing with my continuous argument on how enums in C are one of humanity's worst creations.
Other than that, just some small spots that stuck out to me as I was looking through:
| enum days_of_the_week {Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; | ||
| ``` | ||
|
|
||
| This assigns `Sunday` the value of 1, and the following value will be 1 greater. In this example, the compiler assigns value to the constants based on the defined value in the form previous + 1. You can define any number of values in an enumerated list and this rule will be followed. For example, we can define the value for each enumerated constant if we choose like this: |
There was a problem hiding this comment.
| This assigns `Sunday` the value of 1, and the following value will be 1 greater. In this example, the compiler assigns value to the constants based on the defined value in the form previous + 1. You can define any number of values in an enumerated list and this rule will be followed. For example, we can define the value for each enumerated constant if we choose like this: | |
| This assigns `Sunday` the value of 1, and the following value will be 1 greater. In this example, the compiler assigns value to the constants based on the defined value in the form "previous + 1." You can define any number of values in an enumerated list and this rule will be followed. For example, we can define the value for each enumerated constant if we choose like this: |
| ``` c | ||
| // 1) | ||
| typedef enum {Const1, Const2, Const3,... ConstN} variable_name; | ||
|
|
||
| // or | ||
|
|
||
| // 2) | ||
| typedef enum name {Const1, Const2, Const3,... ConstN} variable_name; | ||
|
|
||
| // or | ||
|
|
||
| // 3) | ||
| enum name {Const1, Const2, Const3,... ConstN}; | ||
| typedef enum name variable_name | ||
| ``` |
There was a problem hiding this comment.
Small thought. Since this already has the "#)" numbering, I would either take out the "// or"s or take out the numbering, since they are both saying the same thing.
| ``` c | |
| // 1) | |
| typedef enum {Const1, Const2, Const3,... ConstN} variable_name; | |
| // or | |
| // 2) | |
| typedef enum name {Const1, Const2, Const3,... ConstN} variable_name; | |
| // or | |
| // 3) | |
| enum name {Const1, Const2, Const3,... ConstN}; | |
| typedef enum name variable_name | |
| ``` | |
| ``` c | |
| // Variation 1 | |
| typedef enum {Const1, Const2, Const3,... ConstN} variable_name; | |
| // Variation 2 | |
| typedef enum name {Const1, Const2, Const3,... ConstN} variable_name; | |
| // Variation 3 | |
| enum name {Const1, Const2, Const3,... ConstN}; | |
| typedef enum name variable_name |
| int main(void) | ||
| { | ||
| int x = 9; | ||
| typedef enum exp_value crop_experience; |
There was a problem hiding this comment.
Maybe we already talked about this but I believe typedef statements should be written next to where the enum was defined, since it is globally-defined.
|
|
||
| ## Bad-Practice Uses of Enums | ||
|
|
||
| In general, you shouldn't have global variables. However, enums provide a lot of versatility and wiggle room for things that will work. You may notice this being very similar to the [dirt grower example](#dirt-grower). The only difference is that `crops`, `plants`, and `dirt_growers` are now globally defined. |
| @. You are a half decent farmer in a video game that received a handful of seeds in the mail. With them came a note that informed you they all take the same amount of time to grow. You decided to plant them all and nurture them to maturity. The day has come to harvest, and you need to keep track of the crops you recognize. Write a program that will keep a count of each crop you have harvested, including unknowns, and print the number harvested in order of experience gain (least to greatest). Unknown crops can be last on the list. Below are code snippets so an answer key can exist. | ||
|
|
||
| ``` c | ||
| // Random seed | ||
| srand(24164132); | ||
| // Random number generated | ||
| 1 + rand() % 13; | ||
| // Crop Values | ||
| 3, 5, 6, 9, 11, 12; | ||
| ``` |
There was a problem hiding this comment.
If it will use elements of randomness, I would explicitly write in the question that you will also need to generate random crops, and perhaps which crops are the ones that are being farmed.
Work in progress. More work will be done over the course of the next few days.