Thread: C Macros Help.

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    22

    C Macros Help.

    Hello!

    i have this macro:

    Code:
    #define NBTEST(GroupName, TestName) int (GroupName)(TestName)()
    
    
    
    
    NBTEST(print, Hello)
    {
        std::cout << "Hello!" << std::endl;
        return 0;
    }
    And i want it to act like this:

    Code:
    int printHello()
    {
        std::cout << "Hello!" << std::endl;
        return 0;
    }
    In the definition, parenthesis obviously does not work like when you do something like this:

    Code:
    #define RETURN_NOT(x) if ( !(x) ) return 0
    So can anyone tell me how to achieve that? How can i unite the two Macro arguments together?

    Thanks.
    Last edited by babaliaris; 02-11-2018 at 04:34 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    You can use the ## preprocessor operator but for an example like this it's pretty hard to make it "nice"

    Code:
    #include <stdio.h>
    
    #define NBTEST(GroupName, TestName) int GroupName##TestName(void)
    
    NBTEST(print, hello)
    {
        printf("Hello\n");
        return 0;
    }
    
    int main(void)
    {
        printhello();
        return 0;
    }
    Edit, Some more info: The C Preprocessor: Concatenation
    Last edited by Hodor; 02-11-2018 at 04:46 AM. Reason: added link to some docs

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    22
    Quote Originally Posted by Hodor View Post
    You can use the ## preprocessor operator but for an example like this it's pretty hard to make it "nice"

    Code:
    #include <stdio.h>
    
    #define NBTEST(GroupName, TestName) int GroupName##TestName(void)
    
    NBTEST(print, hello)
    {
        printf("Hello\n");
        return 0;
    }
    
    int main(void)
    {
        printhello();
        return 0;
    }
    Edit, Some more info: The C Preprocessor: Concatenation
    Thank you so much!!! This is perfect .I really couldn't find it with google.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with macros
    By ManikSinghal in forum C Programming
    Replies: 1
    Last Post: 10-14-2013, 12:05 AM
  2. Help with macros
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 01-31-2013, 10:29 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  4. Macros Using #s
    By Krak in forum C++ Programming
    Replies: 21
    Last Post: 07-18-2005, 01:03 AM
  5. macros
    By rpc2005 in forum C Programming
    Replies: 23
    Last Post: 06-14-2005, 08:56 AM

Tags for this Thread