What are macro in c and how they are defined
What are macro in c and how they are defined
Macros are a preprocessor directive that replaces the macro name with the code in the definition before compilation.There's about a dozen and a half reasons not to use macros, but that's how they are defined if you wished to use them. Consider using inline functions instead.Code:#include <stdio> #define myMacro printf("Hello World") int main() { myMacro; return 0; }
Sent from my iPadŽ
Hellow !!! this is a c forum not c++.Originally Posted by SlyMaelstrom
You can inline functions in C, genius.
(Right?)
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Yes, that's correct, my bad. Either way, avoid macros.
No, I don't believe it's standard. Some compilers will take it, but I think it just ignores the keyword when you compile a C source.Originally Posted by ahluka
EDIT: Off of a little research it seems inlining was fully accepted into the C99 standard, so it may actually be applied in C code. I'm not positive, though.
Last edited by SlyMaelstrom; 05-02-2006 at 08:05 AM.
Sent from my iPadŽ
Ok I didn't know that.
EDIT: You must be able to. I swear I've done it before. Unless I tricked myself into thinking I was using C. Hmm.
EDIT2: You have to stop editing at the same time as me.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
c99 does support function inlining. Unfortunately, many people use microsoft compilers which do not handle c99.
Gcc does, though - just compile with -std=c99.
Last edited by Ken Fitlike; 05-02-2006 at 08:30 AM. Reason: typo
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
Code:#include <stdio.h> #define SIX 1+5 #define NINE 8+1 int main () { printf("%d times %d equals %d", SIX, NINE, SIX*NINE); return 0; }
> printf("%d times %d equals %d", SIX, NINE, SIX*NINE);
I wonder what this will print.
A demonstration of the precedence traps which can happen with macros no doubt
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.