Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Passing enum constant as function arguments.

Posted on 2004-08-08
Last Modified: 2010-05-18

Hello Experts,

I am trying to understand the concept of using enum with functions to control program execution. Can someone please explain how can I use/pass enum constant to a function. What would be the function prototype and the syntax of function call.

Here is the code:


#define SQL1 "select username,user_id from dba_users";

/* Constants defined */
typedef enum oci {RUNSQL1=1} ociprogram;

/* Function prototypes */
int runSql(ociprogram, char *);

int main()
       runSql(RUNSQL1,SQL1);   /* Call to runSql - passing RUNSQL and SQL1 */

     return 0;

int runSql(ociprogram RUNSQL1, char *ocisql1)
        printf("RunSQL = %d\n");
        printf("SQL statment = %s\n",ocisql1);

        return 0;


Question by:mohammadzahid
  • 4
  • 2
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11749667
In your example, you can divide enumeration in 2 parts:

enum oci {RUNSQL=1};   <--- Defines an enumeration called oci with just one value (RUNSQL)

then funtion prototype but be:

int runSql(enum oci, char *)

But, to avoid mention enum keyword every time, it is define with a typedef:

typedef enum oci {RUNSQL1=1} ociprogram;

now you have a new data type: ociprogram, similar to int or char

Thus, the function declaration is now:

int runSql(ociprogram, char *)

That's means that first argument of this function can be ONLY enumerated values, in this case, unique possibility is RUNSQL1, or 1.

Inside runSql there is a bug, first line must be:

       printf("RunSQL = %d\n", RUNSQL1);

It will demonstrate that when you pass RUNSQL1 as first argument, you are really passing value 1.

Hope this help.
Good luck,
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11749675
I have not noticed another error in your function

int runSql(ociprogram RUNSQL1, char *ocisql1)

It is not convenient to name first argument as RUNSQL1, because will confuse you with enum value,
better you can name it 'value'. So your entire function must be:

int runSql(ociprogram value, char *ocisql1)
       printf("RunSQL = %d\n", value);
       printf("SQL statment = %s\n",ocisql1);

       return 0;
LVL 55

Accepted Solution

Jaime Olivares earned 300 total points
ID: 11749687
The fact is that runSql can be invoked only with valid enumerated values, it is a good method to avoid incorrect coding.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 11

Author Comment

ID: 11749704
Thanks Jaime for quick response and appreciate your help.

Now I am getting error

e:\vstudionet\Myprojects\testprograms\testprograms\main.c(13) : error C2143: syntax error : missing ')' before ';'

at the function call when calling function  

runSql(RUNSQL1,SQL1);    /* RUNSQL1 is the first const of enum && SQL1 is a string value */

from the main ( ).

Can you please provide syntax of the function call if you don't mind.

Thanks again.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 11749718
The origin of the problem is in the #define

#define SQL1 "select username,user_id from dba_users";  <---- remove semicolon here
LVL 11

Author Comment

ID: 11749739
That solved the problem. Thanks again.

Thread closed.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tidtcpserver connection lost handle 2 100
When i run adoquery my application freezes 26 178
program a usb light to turn on when needed 7 68
ejb entity bean example 2 7
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question