Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am designing a C API which among other things has to provide a way to set some double-valued options. To identify the options I use the following enum:

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

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

int set_option(Option opt, double value);

or is it better to use int instead:

int set_option(int opt, double value);

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

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

like image 207
vitaut Avatar asked Feb 02 '12 20:02

vitaut


1 Answers

By using an enum you are effectively helping the user of your function to know what valid values are available. Of course the drawback is that whenever a new option is added you will need to modify the header and thus the user may need to recompile his code whereas if it is an int he may not have to do that.

like image 169
AndersK Avatar answered Oct 12 '22 12:10

AndersK