actually, i just have a ton of other things to look up...
forgive this elementary question
can i make this any shorter?
#define isOp(x) (x=='-'||x=='+'||x=='/'||x=='*')
actually, i just have a ton of other things to look up...
forgive this elementary question
can i make this any shorter?
#define isOp(x) (x=='-'||x=='+'||x=='/'||x=='*')
Shorter in what - time or space?
I can make it shorter (by source code character count), but it will take longer to execute.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
either or......i was a bit worried that that way was a bit inefficient (because i have a couple of much large macros that are the same, uh, format as that)
...i guess the question is: what's the best way to do that? (define 'best' on your own terms)
since its hardly that big i would go with the one that gives better performance... rather than smaller code
If you have to ask this question, I think the answer is:Originally Posted by misplaced
The "best" way is the code that you can see, at a glance, is doing exactly what you want it to. Making a function that requires fewer keystrokes or cleverly using some technique that is not at first obvious to you is OK if you like puzzles, but could bring you grief some time in the future.
For example: This is part of a complicated program that doesn't work. As you are debugging the program, you keep coming back to your "clever" code for this simple function. The problem is actually somewhere else, but this is such a distraction that you can't actually see the real problem. Finally, you go back and plug in some obvious code in place of your cleverness so that you can continue debugging. Why not just put the obvious (to you) code in place from the start?
Just my opinion. Your Mileage May Vary.
Regards,
Dave
p.s. widescale use of macros (especially complicated ones) has caused me lots of wasted debug time. Lots of times I have taken macros (from other people's code) and made functions of them so that I can find places where they were invoked with inappropriate arguments. Use of functions rather than macros allows type checking of arguments by the compiler. Sometimes this makes errors obvious.
Last edited by Dave Evans; 10-02-2004 at 08:23 AM.
Well in the first instance you should be going for readability.
> #define isOp(x) (x=='-'||x=='+'||x=='/'||x=='*')
Eg.
#define isOp(x) (strchr("+-*/",x)!=NULL)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Why not use inline functions instead of such kind of macros.
mfg JJ