![]() For example, we use macro- for words like: Macro as a combining formĪs a combining form, the word macro allows any term to imply the essence of being “ long,” “ long-term,” “ large,” “ large scale,” or just “ relatively large” in comparison to a standard size ( in health care) (1049). The word macro is an adjective, noun, or a combining form–– the latter originating from Greek makros for ‘long’ and ‘large.’ As noted by the New Oxford American Dictionary, the adjective and noun forms of macro developed independently of “ macros-” (“Macro” 1049). The main difference between macro and micro is that “ macro” means ‘large’ and “ micro” means ‘ extremely small.’ The terms are near opposites of one another, although “ micro” tends to convey minuscule sizes, and “ macro” can mean ‘ relatively large’ instead of “ giant.” What does macro mean? microeconomics.” Although these words seem complicated, we promise macro vs. They differ by one letter and occupy similar terminology, such as “macrocosm vs. One of the most common questions writers ask involves the words macro and micro. If you like to have square macro that works with various types and have C11 support, you could do this.What is the difference between macro and micro? Its possible to write macros that avoid side-effects with multiple instantiation of arguments. Noting this since its one of the most common causes of errors in macros (passing in x++ for example, where a macro may increment multiple times). Meaning you may end up with less portable code, or have to ifdef them in, so they're only taken advantage of when the compiler supports. though it still depends a lot on the context, since inline functions may be an option).Īdmittedly, some of these rely on compiler extensions which aren't standard C. (assignments to multiple variables, for a per-pixel operations, is an example you might prefer a macro over a function. ![]() When you want to manipulate variables in the callers local scope, passing pointer to a pointer works just fine normally, but in some cases its less trouble to use a macro still.func(FOO, "FOO"), you could define a macro that expands the string for you func_wrapper(FOO) Their use as wrappers to functions, in some cases you may want to avoid repeating yourself, eg.Its possible to write macros which are more exacting then a function about their input args. While its noted that functions do type checking, C will coerce values too (ints/floats for example).Or check an array meets some length condition. Inspect input args, You can do tests on input args such as checking their type, sizeof, check struct members are present before casting.check for pre/post conditions, assert on failure, or even static-asserts so the code won't compile on improper use (mostly useful for debug builds). They can optionally include local info, such as debug strings:.Variable number of arguments can map to different functions instead of using C's va_args.Generic functions, as noted below, you can have a macro that can be used on different types of input arguments.There are certain exceptional cases where there are advantages to using macros, these include: Then use macros only when there is a good reason to. You can be aware of the pitfalls and learn to avoid them. However answers here mostly explain the problems with macros, instead of having some simple view that macros are evil because silly accidents are possible. When in doubt, use functions (or inline functions). C++ has a number of features such as templates to help create complex polymorphic constructs in a typesafe way without the use of macros see Stroustrup's The C++ Programming Language for details. Where macros are deliberately used to specify polymorphic behavior, unintentional polymorphism may be difficult to avoid. Inline functions and constants help to avoid many of these problems with macros, but aren't always applicable. Print("foo %s", "bar") /* prints "foo %sbar" */ with gcc -E), because debuggers cannot step through macros, as in this example: #define print(x, y) printf(x y) /* accidentally forgot comma */ When macros contain multiple statements, you can get in trouble with control-flow constructs: #define swap(x, y) t = x x = y y = t įinally, macros can be difficult to debug, producing weird syntax errors or runtime errors that you have to expand to understand (e.g. Putting parentheses around arguments helps but doesn't completely eliminate these problems. Square(x++) -> x++ * x++ -> increments x twice Works fine when used with an integer: square(5) -> 5 * 5 -> 25īut does very strange things when used with expressions: square(1 + 2) -> 1 + 2 * 1 + 2 -> 1 + 2 + 2 -> 5 For example, this macro: #define square(a) a * a Macros are error-prone because they rely on textual substitution and do not perform type-checking.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |