-2

I am trying to define a constant that would have the following in it

ch[co].gold < 10000*100

how can I make it, something like

define x = ch[co].gold < 10000*100;

so that every time I write

if (x) {say(cn,"You need 10 000 gold coins");}

Or that is not possible?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Night
  • 23
  • 6
  • Yes this is C indeed – Night May 24 '16 at 07:59
  • Yes, it's called a boolean expression - just change "define" to a suitable type, e.g. `bool` or `int`. I recommend picking a [book from this list](http://stackoverflow.com/q/562303/253056) and doing some reading if you want to learn C properly. – Paul R May 24 '16 at 08:01
  • Thank you everyone, that notation threw me off, was a dumb question but yet something that is i am unfamiliar with in C. Thanks. – Night May 24 '16 at 08:22
  • @Night there's a tradeoff here: you can type less now and scratch your head more later ... or type a little more now and keep a clear head in the future :-) – pmg May 24 '16 at 08:22

4 Answers4

2

Function:

int x(int val) {
    return (val < 10000 * 100);
}

Usage

    // ...
    if (x(ch[co].gold)) {
        printf("You need 10 000 gold coins.\n");
    }
    // ...
pmg
  • 106,608
  • 13
  • 126
  • 198
1

Well, here is my solution

struct s {
    int gold;
};

const int co = 2;

struct s ch[] = {112,2321,3234};

#define x() ch[co].gold < 10000*100

int main(){
if (x()) {

}
return 0;
}
Roman Podymov
  • 4,168
  • 4
  • 30
  • 57
  • This one takes the cake. Nicely done, however what i posted is an excerpt from a code, ch[co].gold extracts an int from a structure, and just compares it to 10000*100. So simply asking how and if it is possible to simplify that comparison down to one character instead of typing it out (or ctrl c + ctrl v in that manner) – Night May 24 '16 at 08:14
0

Is this what you are expecting?

#define x (ch[co].gold < 10000*100)

Add this line of code before the place you use it, usually it's just below the #includes. Conventionally, we use capital letters with clearer meanings instead of x.

elricfeng
  • 364
  • 1
  • 8
0

#define is simply text substition performed by the preprocessor.

To do what you say you want use the following #define:

#define x ch[co].gold < 10000*100

Every time the preprocessor encounters the symbol x it will replace it with ch[co].gold < 10000*100.

I think that what you really want is to make it a proper function as suggested by pmg. It is a wiser choice.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82