Thread: help writing a macro that returns a boolean value

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    72

    help writing a macro that returns a boolean value

    Hi,

    I need help writing a macro that would return true/false (1/0) )value. I want to check if a certain element exists in the array. The macro will accept array, its size, and the value to be compared, and must return yes or no. Here is the code that I have written:

    Code:
    #define EXISTS(T, a, n, val) do {\
        char ret=0;\
        T *a_ = (a);\
        size_t n_ = (n);\
        for (; n_ > 0; --n_, ++a_){\
    		ret = (*a_ == val);\
    	}\
    } while(0)
    How can I get the result from this macro.
    thanks,

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is not possible for a macro that expands as a compound statement (a statement composed, in turn, of multiple declarations and statements) to "return" a value. I have put "return" in quotes, since a macro is not actually code - it is a text substitution mechanism.

    If you want to return a value, implement it as an inline function, not as a macro. The type T would have to be fixed though.

    As is, your macro doesn't need the for loop, as the final value of ret is the one for the last iteration of that loop, with no relationship to previous iterations. I suspect that is a fault in your implementation though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Quote Originally Posted by grumpy View Post
    It is not possible for a macro that expands as a compound statement (a statement composed, in turn, of multiple declarations and statements) to "return" a value.
    It is possible using GCC Statement Exprs - Using the GNU Compiler Collection (GCC)

    Code:
    #define EXISTS(T, a, n, val) ({\
        char ret = 0;\
        T *a_ = (a);\
        size_t n_ = (n);\
        T val_ = (val);\
        for (; n_ > 0; --n_, ++a_){\
            if (*a_ == val_){\
                ret = 1;\
                break;\
            }\
        }\
        ret;\
    })

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by DRK View Post
    It is not possible in standard C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    72
    Quote Originally Posted by grumpy View Post
    It is not possible for a macro that expands as a compound statement (a statement composed, in turn, of multiple declarations and statements) to "return" a value. I have put "return" in quotes, since a macro is not actually code - it is a text substitution mechanism.

    If you want to return a value, implement it as an inline function, not as a macro. The type T would have to be fixed though.

    As is, your macro doesn't need the for loop, as the final value of ret is the one for the last iteration of that loop, with no relationship to previous iterations. I suspect that is a fault in your implementation though.
    thanks for your help.

    I implemented it as inline function. And, also you are right about the fault regarding return value. I fixed that as well.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    72
    thanks DRK for your help.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zahid990170 View Post
    Code:
    #define EXISTS(T, a, n, val) do {\
        char ret=0;\
        T *a_ = (a);\
        size_t n_ = (n);\
        for (; n_ > 0; --n_, ++a_){\
            ret = (*a_ == val);\
        }\
    } while(0)
    How can I get the result from this macro.
    thanks,
    Pass in another parameter ret which will capture the result. Example:

    Code:
    #define EXISTS(ret, T, a, n, val) do {\
        ret = false;\
        T *a_ = (a);\
        size_t n_ = (n);\
        for (; n_ > 0; --n_, ++a_){\
            if (*a_ == val) {\
                ret = true;\
                break;\
            }\
        }\
    } while(0)
    
    #define NELEM(arr) (sizeof(arr) / sizeof(arr[0]))
    
    int main()
    {
        int a[] = {10,40,70,80,100,200};
        bool ret;
        EXISTS(ret, int, a, NELEM(a), 100);
        if (ret) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
    Last edited by c99tutorial; 04-26-2014 at 02:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a macro to compare two numbers?
    By RichSelian in forum C Programming
    Replies: 17
    Last Post: 06-28-2011, 05:05 AM
  2. Boolean function returns
    By Jaxtomtom89 in forum C Programming
    Replies: 6
    Last Post: 10-17-2010, 09:22 AM
  3. Boolean returns in C
    By blackocellaris in forum C Programming
    Replies: 11
    Last Post: 10-23-2006, 07:42 PM
  4. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  5. Replies: 2
    Last Post: 05-05-2002, 09:27 AM