macros

This is a discussion on macros within the C Programming forums, part of the General Programming Boards category; i have this in a header file: Code: ... #define IND_BIT (N) ((N) / NUM_BITS) /* Find array index for ...

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    macros

    i have this in a header file:
    Code:
    ...
    
    #define IND_BIT (N) ((N) / NUM_BITS)
    /* Find array index for Nth bit */
    
    #define POS_BIT (N) ((N) % NUM_BITS)
    /* Find position of Nth bit */
    
    #define SET_BIT (barr, N) ((barr)[IND_BITS(N)] |= (bitarr)1 << POS_BITS(N))
    /* Set Nth bit */
    ...
    and this source file:
    Code:
    #include <stdio.h>
    #include "ba.h"
    
    #define M 10
    
    int main(void)
    {
      bitarr a[SIZ_BITS(M)] = {0};
      int i;
    
    
      SET_BIT (a, 5);
    
      for (i = 0; i < M; i++)
        if ( TST_BIT(a, i) )
          printf("%dth bit set\n",i);
    
      return 0;
    }
    What's the problem?
    I get these errors:

    Code:
    bitarr.c:8: `N' undeclared (first use in this function)
    bitarr.c:8: (Each undeclared identifier is reported only once
    bitarr.c:8: for each function it appears in.)
    bitarr.c:12: `barr' undeclared (first use in this function)
    bitarr.c:15: called object is not a function
    bitarr.c:15: called object is not a function
    bitarr.c:16: syntax error before "printf"

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    remove spaces before the first (

    #define IND_BIT (N) ((N) / NUM_BITS)

    =>

    #define IND_BIT(N) ((N) / NUM_BITS)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    #define IND_BIT (N) ((N) / NUM_BITS)

    You're not allowed spaces between the macro name, and the opening (
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    thanks.
    only one error left
    bitarr.c:16: syntax error before "printf"

    heres' TST_BIT:

    Code:
    #define TST_BIT(barr, N) ((barr)[IND_BITS(N)] & ((bitarr)1 << POS_BITS(N))
    Last edited by viaxd; 07-21-2004 at 04:44 PM.

  5. #5
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by viaxd
    i have this in a header file:
    <...>
    Many things are missing in this code... Try this :
    Code:
    #include <limits.h>
    
    #define NUM_BITS CHAR_BIT
    
    #define IND_BITS(N) ((N) / NUM_BITS)
    /* Find array index for Nth bit */
    
    #define POS_BITS(N) ((N) % NUM_BITS)
    /* Find position of Nth bit */
    
    #define SET_BIT(barr, N) ((barr)[IND_BITS(N)] |= ((bitarr)1 << POS_BITS(N)))
    /* Set Nth bit */
    
    #define TST_BIT(barr, N) ((barr)[IND_BITS(N)] & ((bitarr)1 << POS_BITS(N)))
    /* Test Nth bit */
    
    #define SIZ_BITS(n) (1 + (n) /sizeof (bitarr))
    
    typedef unsigned char bitarr;
    
    #include <stdio.h>
    
    #define M 10
    
    int main (void)
    {
       bitarr a[SIZ_BITS (M)] =
       {0};
       int i;
    
       SET_BIT (a, 5);
    
       for (i = 0; i < M; i++)
       {
          if (TST_BIT (a, i))
          {
             printf ("%d%s bit set\n"
                    , i + 1
                    , (i + 1) == 1 ? "st"
                    : (i + 1) == 2 ? "nd"
                    : (i + 1) == 3 ? "rd"
                                   : "th"
                    );
          }
       }
    
       return 0;
    }
    Last edited by Emmanuel Delaha; 07-22-2004 at 02:26 AM.
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    thanks a lot, it works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 01:35 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 02:51 AM
  4. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 11:51 AM
  5. template fn replacements for msg macros
    By Ken Fitlike in forum Windows Programming
    Replies: 17
    Last Post: 10-30-2002, 06:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21