Can macro call contain any value for the argument?

Posted on 2007-07-20
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
    LVL 3

    Assisted Solution

    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

    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 84

    Assisted Solution

    > Can this macro be called with any uS value?
    > Can there be any issues with that?

    for example COUNTS(10+10)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    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 ( 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 recursion in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now