#define in c

Hi,

I had a head file, looks like
#define MIN_NUM 10
#define MAX_NUM 10

is there any way to get "MAX_NUM" from 10?

thanks.

peter

Hi.

I'm not sure I entirely understand your question.

Could you please elaborate.

Thanks.

#define MAX_NUM 10
  printf("MAX_NUM == %d\n", MAX_NUM);

Or. You add an extra define like this:

#define MAX_NUM 10
#define show_MAX_NUM  printf("MAX_NUM=%d", MAX_NUM)

show_MAX_NUM;

If you need a string:

#define is_MAX_NUM(x,y) strcpy(y, (x == MAX_NUM)? "MAX_NUM" : "NOT_MAX_NUM");
char string20={0x0};
int val=10;
is_MAX_NUM(val, string);

header file,
#define MIN_NUM 10
#define MAX_NUM 30

I had a table

tag,authLevel
MIN_NUM, 3
MAX_NUM, 5

a function
checkAuthLevel(int) {

}

-----------------

in function checkAuthLevel(), I only have 10, I need to retrive authLevel from table using 'MIN_NUM', but I only 10 instead of 'MIN_NUM', how can I get '3 by using 10?

Thanks.

peter

Tell us more about this table. Is it a file? A data structure?

Fine. What sort of database table? :wall: What DBMS? What database? What table? Is it stored locally?

defines are a one way street only...when you use them in your program every instance of them will be replaced by the substitution text. In this case MIN_NUM is replaced by 10 everywhere before compilation...but the number 10 itself wont point back to symbol MIN_NUM. So instead of defines use variables with those names in your jump table and then you can index into that table using the variable name instead of a symbolic constant.

Which isn't to say you couldn't just make MIN_NUM an ordinary int and give it a number assigned from a database.

Hi Peter,

A preprocessor directive such as you see "#define" or "#include" is translated by the preprocessor (cpp) before the compiler gets its hands dirty with the code. For example if you write,

#define NUM 5

Then every instance of NUM gets replaced by "5" before the code is compiled. The compiler knows nothing about what NUM is so you cannot use a logic to get NUM from 5.

My question is why would you want NUM which is a variable from 5 which is constant. You can only do the reverse that is get the constant from a variable.

Thanks and Regards,
Gaurav.