Is it a good idea to use the enum parameter in a public API function in C?

I am developing a C API that, among other things, should provide a way to set some two-digit parameters. To determine the parameters, I use the following enumeration:

typedef enum { OptionA, OptionB, ... } Option; 

Is it possible to use Option as a parameter type in a public API function:

 int set_option(Option opt, double value); 

or better to use int instead:

 int set_option(int opt, double value); 

considering that I may need to add additional options in the future?

Also, are there any good examples of existing APIs that demonstrate any approach?

+7
source share
2 answers

Using enum , you effectively help the user of your function know which valid values โ€‹โ€‹are available. Of course, the disadvantage is that whenever a new option is added, you will need to change the title, and therefore, the user may need to recompile his code, whereas if it is int , he probably shouldn't.

+7
source

Enums give you the advantage that compile time checks the value is correct. You will then need to add new parameters to the new version of your header file if you add options in the future.

+1
source

All Articles