Page 1 of 2
Macros - what exactly are they?
Posted: Tue Jun 12, 2007 2:03 am
by Luke
Can somebody explain to me what a macro is, in comparison to say a variable or an entity? I'm confused about the difference.
Is a macro just basically a token that corresponds with a value somewhere? Isn't that basically a variable?
Posted: Tue Jun 12, 2007 5:23 am
by volka
If you mean an assembler or C macro it's a replacement mechanism like "I type xyz and you replace it by abcdefghijklm"
E.g. the php "constant" __FILE__ is also usable in C. The preprocessor replaces __FILE__ by the file path of the current file before the compiler gets the code.
Macros can be parameterized, like e.g. FD_ZERO (fd_set *set). Looks like a function but actually is a macro in in the gnu c library.
Posted: Tue Jun 12, 2007 7:18 am
by Chris Corbyn
Macros, in the sense I have worked with them, can contain subroutines/expressions. Variables contain values.
In a completely generic sense, a macro could be something like:
MY_MACRO = something fixed but with an expression() in it
That way everytime you refer to it you're not strictly getting the same values back.
Posted: Tue Jun 12, 2007 7:23 am
by jayshields
I thought a macro was like a fixed piece of code that could be executed.
I remember in MS Access there was a Macro bit and if you put something in it it would execute it on launch of the database, so you would generally put a macro in there to launch the switchboard.
Posted: Tue Jun 12, 2007 10:07 am
by Luke
hmm... ok, so why use this instead of a function?
Posted: Tue Jun 12, 2007 1:27 pm
by Weirdan
hmm... ok, so why use this instead of a function?
Macros in C? One reason to use them is that they are type-ignorant, so they could be used like templates are used in C++.
Posted: Tue Jun 12, 2007 1:29 pm
by Luke
OK, so because PHP is a loosely-typed language and that's what I know, I probably just don't see the importance of them huh?
Posted: Tue Jun 12, 2007 1:45 pm
by Weirdan
Macros in PHP? I'm afraid it's something I haven't heard of... why do you think they exist?
Posted: Tue Jun 12, 2007 1:55 pm
by Luke
I'm sorry, I didn't mean to imply there are macros in PHP. I simply meant, that since I come from a PHP background ( a loosely-typed language ) that may be why I'm having a hard time understanding the point of macros.
Posted: Tue Jun 12, 2007 4:55 pm
by Ambush Commander
Usually, you find macros in more lower level languages like C and TeX. In some implementations, a macro actually results in a bunch of code being inlined at that location by the preprocessor.
I think macros and functions serve the same purposes in different manners. Maybe the Wikipedia article will also help:
http://en.wikipedia.org/wiki/Macro
Posted: Tue Jun 12, 2007 5:40 pm
by Luke
(actually the wikipedia article is what started the confusion in the first place)
I think I kind of understand now.
Posted: Tue Jun 12, 2007 7:56 pm
by feyd
Function calls in most languages take a certain amount of time to call. This is due to the need to create a stack for the function, shift the execution pointer, then destroy the stack and finally rewind the pointer. Often macros (and various compiler switches) are used to alleviate this performance hit when the code is fairly simple in nature. They are similar to compiler options to unwind short loops.
Posted: Wed Jun 13, 2007 12:28 am
by Luke
Some day... I'll be the shizzle just like feyd and AC... some day.
For right now, I'll just keep askin' questions.
It's worked for me so far
Posted: Fri Jun 22, 2007 12:00 am
by ev0l
I am going to assume you are talking about C preprocessor macros. Wikipedia has a great article on it
http://en.wikipedia.org/wiki/C_preprocessor.
Basically think of a C macro as something the compiler does to the code before it is compiled. Sort of like a fancy copy and paste. The text in the macro is placed in the location of the defined token where it appears in the code. So a simple example ....
lets x = 4;
Macros in C can be used to do all sort of neat things but the most common is header file include.
Posted: Fri Jun 22, 2007 12:15 am
by Christopher
feyd wrote:Function calls in most languages take a certain amount of time to call. This is due to the need to create a stack for the function, shift the execution pointer, then destroy the stack and finally rewind the pointer. Often macros (and various compiler switches) are used to alleviate this performance hit when the code is fairly simple in nature. They are similar to compiler options to unwind short loops.
Yeah ... macros are also often use to encapsulate commonly used snippets of code with a name. It is just another way to share code in a team for snippets rather than functions. I a sense they are templates that are filled in by the pre-processor with the values provided. The compiler then gets the expanded code.