Can macro call contain any value for the argument?

Posted on 2007-07-20
Medium Priority
Last Modified: 2010-04-15
What is the range of macro argument?  For example, consider the following macro:

#define COUNTS(uS) ((int)(uS * sys_clock / (Divider)))

Can this macro be called with any uS value?    When we call a function, we know the datatype of the argument.  For macro we don't know the datatype of the argument.  Can there be any issues with that?

Question by:naseeam

Assisted Solution

asood314 earned 560 total points
ID: 19536350
The compiler will give you an error if you pass arguments to the macro that cannot be converted into a datatype suitable for the operations you perform in the macro. For instance, if in your example you tried to pass a character array to COUNTS, you would get the following error:

error: invalid operands of types 'const char*' and 'double' to binary 'operator*'

Otherwise, what ever argument you pass to COUNTS will simply be converted into a suitable datatype.  For example, you could pass a single char to COUNTS.
LVL 22

Accepted Solution

grg99 earned 1120 total points
ID: 19536447
macro parameters look a lot like function parameter, but they're very different.

The macro gets expanded  by simple text substitution, long before the compiler parses the line.

So if you write :  COUNTS(10000000)

the compiler sees :    ( (int)(10000000 * sys_clock / (Divider)))

Or you could write :  COUNTS( ten bizillioin googols )

the compiler sees :    ( (int)(ten bizillioin googols* sys_clock / (Divider)))

so there's no concept of "type" of a macro parameter.  It's just text.

You do have to consider what the resulting expansion looks like to the compiler.
You have to be careful to avoid generating invalid code.  Macros are of little help as there are no #if statements that work with arithmetic values,so yo can't easily generate (int) or (long) casts.

LVL 85

Assisted Solution

ozo earned 320 total points
ID: 19536617
> Can this macro be called with any uS value?
> Can there be any issues with that?

for example COUNTS(10+10)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question