#include <stdio.h> #define FATAL_ERROR {int* a=NULL; *a=1;} int main(int argc, char** argv){ if(1==0) FATAL_ERROR return 0; }
It was easier to open up the Allegro.cc forums to ask this rather than putting it into a compiler?
Define "work".
Also, theres an issue with the {}s. If you try and put it in certain places, you'll start to get confused.
I edited the first post before your reply. Is it ok now?
It was easier to open up the Allegro.cc forums to ask this rather than putting it into a compiler?
It compiles.
Define "work".
No.
Append: That's not coz I don't want to.
My initial thought was no...
{"name":"599918","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/7\/87aa5d17258a1186c51946c1ded42232.jpg","w":1024,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/7\/87aa5d17258a1186c51946c1ded42232"}
EDIT-- ah, but now now I see this has to do with Matthew...
http://www.allegro.cc/forums/thread/602288
I edited the first post before your reply. Is it ok now?
It looks the same to me 
You generally don't want to put bare {}s in a define.
do something like: #define BLAH do{ codehere }while(0) instead.
Then you can use it like so: if(foo) BLAH;
Yes.
And no, you don't want to do it.
Hmmm, made me think, what if:
if (1 == 0) { }
Actually returned true? What would that mean?
It will never return true because it's the same if "a=1;b=0; if(a==b)" would have returned true: nothing would have worked as expected.
What if I were a millionaire? What would that mean?
That you are still asleep and dreaming, BAF ;-)
Actually returned true? What would that mean?
That would mean a FATAL ERROR I suppose..
Hmmm, made me think, what if:
if (1 == 0) { }
Actually returned true? What would that mean?
That would basically mean the compiler malfunctioned, and given such a compiler, god knows what FATAL_ERROR would do.
You generally don't want to put bare {}s in a define.
I guess last question in this thread.. Thomas, is there a real reason not to put a {} in a define?
No real one except it's unneeded.
They're needed in case of a if/do/while/for statement inside the #define, but you gain nothing adding {} in some simple statements as int *it; it=NULL, except huge problem spotting out the bug if you forgot to close/open a {}
I've used similar code (writing at NULL) to test my signal handler.
Audric: I use kill -signal for that kind of things.
Actually, I DO need the {} in the define, as otherwise the above code won't even compile. As the macro is just replaced with it, and only one line is scoped in to an if statement(and the a won't be declared identifier).
You DO need it because of your implementation.
Keep in mind that #define are just preprocessing substitution.
Doing so would also work:
#include <stdio.h> #define FATAL_ERROR int* a=NULL; *a=1 int main(int argc, char** argv){ if(1==0) { FATAL_ERROR; } return 0; }
Plus your macro is evil.
That's what I meant.. But I wanna be referring to a single line MACRO as to a single line.. I still wonder what did Thomas mean though.
Append:
Plus your macro is evil.
Is my if statement less evil?
All this thread was sort of a joke.. Bit smelling with philosophy- as of what an actually working code is.
I still wonder what did Thomas mean though.
Well for one, when you look at a bare macro do you always remember it has {}s surrounding it? What if you put it some place that causes a compiler error? Will you remember then that it is a block?
I already gave the proper alternative. a do{CODE HERE}while(0) construct. That way its a statement, and not a block. And can be used in any place a regular statement can be, without any problems.
That way its a statement, and not a block.
I have a feeling you are correct, but I am curious for an example as well. An example of where his implementation would cause an error, and yours would not.
Example (google on "define while 0"):
http://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro
Well for one, when you look at a bare macro do you always remember it has {}s surrounding it? What if you put it some place that causes a compiler error? Will you remember then that it is a block?
That's it.. I can't imagine such a case. Unless I decided to do some assignment works out of scope of a function..
However, is not this:
#define FATAL_ERROR {int* a=NULL; *a=1;};
Easier?
I didn't check this one, but I'm quite sure that MSVC(latest one) at least won't give errors to that.. Furthermore, I encountered a ;; in my code, without any warnings, and of course without side effects as well. a ; is a ;, it doesn't harm if doubled, unless really misplaced.
Don't put the ; in the define.
Thank you for the link to Stack Overflow, that explains everything.
It compiles.
Doesn't mean sh* with macros.
#define foobar Hello there, you don't know me but I compile! Yay!@@#$%$QWQU int main() { return 0; }
There is a difference between just defining a macro and actually using it.
Well, I did compile it and did put it in to the code(binary code was generated using the Macro) hence your notes seem to be irrelevant.
Okay, have fun then. It's not like I'm going to have to deal with your messes... at least I hope not. I'll watch out for you on the daily wtf.