Macros - what exactly are they?

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Macros - what exactly are they?

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hmm... ok, so why use this instead of a function?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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++.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Macros in PHP? I'm afraid it's something I haven't heard of... why do you think they exist?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

(actually the wikipedia article is what started the confusion in the first place)

I think I kind of understand now.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Post 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 ....

Code: Select all

#define FOO 4

int x = FOO;
lets x = 4;

Macros in C can be used to do all sort of neat things but the most common is header file include.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply