Is a macro just basically a token that corresponds with a value somewhere? Isn't that basically a variable?
Macros - what exactly are they?
Moderator: General Moderators
Macros - what exactly are they?
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?
Is a macro just basically a token that corresponds with a value somewhere? Isn't that basically a variable?
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
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.
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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
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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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
For right now, I'll just keep askin' questions. It's worked for me so far
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.
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;
Macros in C can be used to do all sort of neat things but the most common is header file include.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.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.
(#10850)