Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (curry):

What does the following way of defining a variable mean? #define COUNT ((long) (sizeof(long) <<1))

OpenStudy (anonymous):

it's a macro that defines COUNT as the size of a long shifted one bit to the left sizeof(long) = 4 0100 moved one bit to the left 1000 or 8

OpenStudy (curry):

so how do i write macros?

OpenStudy (curry):

like what defines it, how would i write another one if I wanted to? and why not just use a 8 byte variable type.

OpenStudy (curry):

And how exactly would I use the macro? esp seince it's called a function.

OpenStudy (anonymous):

this one is a variable. macros can also be functions though ``` //taken from http://stackoverflow.com/a/3437442/2761134 #define MAX(a,b) ((a) > (b) ? a : b) #define MIN(a,b) ((a) < (b) ? a : b) ```

OpenStudy (e.mccormick):

https://gcc.gnu.org/onlinedocs/cpp/Macros.html

OpenStudy (anonymous):

Macros are executed by the preprocessor, and they only ever do text replacement. ``` #define MAX(a,b) ((a) > (b) ? a : b) int f() { return MAX(2, 4); } ``` Putting this through the preprocessor will get you something like: ``` int f() { return ((2) > (4) ? 2 : 4); } ``` Which will then later on be compiled. So obviously your macros should only do really simple things. Other languages don't even have this feature, because some consider it dangerous.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!