Jump to content

C programming ethics - Enums

Gat Pelsinger

I am trying to code chess in C. I have typedef enums for my pieces. But when coding, one thing clicked in my mind is that what exactly is the data type used for these enums? ChatGPT says whichever data type fits for the given values of the enum. Is this true? So that means if my enums are PAWN, QUEEN, KNIGHT, etc., I am using string for the computation? If so, surely that is not an efficient way if programming and so I might be forced to use integers for describing the pieces? And even if I do go the enum style, my board is a 2D 8x8 array of unsigned char. It won't accept the enums. So, then I have to use translation to convert the enums into values that can be stored in the array?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

ChatGPT can provide answers, rhat really already need you to understand with what you are dealing.

 

It'd be very beneficial for you to learn how google questions like this. If you want to know what something does, you usually just type in what you are looking for as well as your programming language and you are good to go.

 

If you want articles that explain what is happenening:

https://www.geeksforgeeks.org/enumeration-enum-c/

 

Geeksforgeeks is usually a good source. And you'll find the other sources you want to look out for as you go.

 

As for your question: enums have the identifier you can use in your code (in your cas Queen, ...), but internally they are handled as numbers (ints or uints), as the name states (enum = enumeration).

 

If you want to describe everything via a char array, you will have to convert them either way.

Link to comment
Share on other sites

Link to post
Share on other sites

enums in c are all integers. enums are there to make your code more readable and allow compilers to do some degree of type safety check as opposed to you just doing things like const int KNIGHT = 0; const int PAWN = 1; and then int test = KNIGHT; switch(test){case KNIGHT....} ect

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Don't blindly trust chatgpt. It doesn't know what it's talking about and any correct answer is coincidental.

 

Enum members are always integers, the label is only for your convenience. If you don't specify what number each label represents the compiler will choose for you.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×