Thread: Passing types to the preprocessor.

  1. #1
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103

    Passing types to the preprocessor.

    Is it possible to pass types to a macro? Can I pass types like int, char, long to a macro and have them expanded into C source code that include the passed types?
    Last edited by G4143; 03-16-2022 at 09:01 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sure can.

    #define DECLARE(type,var) type var


    then later
    DECLARE(int,foo)

    The pre-processor doesn't know about C in particular.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by Salem View Post
    Sure can.

    #define DECLARE(type,var) type var


    then later
    DECLARE(int,foo)

    The pre-processor doesn't know about C in particular.
    Then that really swings the door wide open for a whole lot of preprocessor possibilities.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The pre-processor is infinitely abusable - yes.

    Wait till you discover the # and ## operators
    Concatenation (The C Preprocessor)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-19-2020, 05:50 AM
  2. problem passing cstring, incompatible types
    By c_weed in forum C++ Programming
    Replies: 12
    Last Post: 10-26-2010, 05:33 PM
  3. Passing different object pointer types to the same arg?
    By see the big C in forum C Programming
    Replies: 13
    Last Post: 06-17-2010, 10:24 PM
  4. prototypes for passing custom types
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-04-2002, 12:51 PM
  5. Replies: 2
    Last Post: 03-31-2002, 12:34 AM

Tags for this Thread