Thread: Macro definition

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    10

    Smile Macro definition

    I have a question about the macro shown as follows:

    Code:
    #define _MUXSELf(a)      	0##a << 30
    What does 0##a mean here?

    Thanks for the help,

    aayu

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It basically joins the tokens on its right and left, e.g., if a was 123, you would end up with 0123 << 30.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    Thanks.

    aayu

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    I try to test the macro by using the following code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define ttoken(a)    0##a << 2
    
    int main()
    {
      int a = 8;
      int b;
      b = ttoken(a);
      printf("b = %d\n", b);
    
      return 0;
    }
    after using:
    Code:
    gcc -std=c99 token1.c
    the following is the compiler error:
    Code:
    token1.c:10:1: error: invalid suffix "a" on integer constant
    Anything wrong here?

    Thanks again,

    aayu

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, the fact that you wrote ttoken(a). You should be using octal digits instead, e.g., ttoken(71).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    You are correct. But why I have to use octal digits, I tryied to use hex or dec, the compiler complained except using octal digits. I must missed something here.

    Thanks,

    aayu

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In C, a number that starts with a 0 is in octal, just as a number that starts 0x is in hex.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    And the "##" preprocessor operators are used for string catenation.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by itCbitC View Post
    And the "##" preprocessor operators are used for string catenation.
    what it has to do with anything? preprocessor in any case just replaces one character sequence with another...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by vart View Post
    what it has to do with anything? preprocessor in any case just replaces one character sequence with another...
    Not if you try to catenate a number and a variable ie 0 ## a produces 0a not 07 if a=7.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by itCbitC View Post
    Not if you try to catenate a number and a variable ie 0 ## a produces 0a not 07 if a=7.
    Why not? It exactly what it does...
    as I said it replaces char sequence ttoken(a) with char sequence 0a

    or char sequence ttoken(71) with char sequence 071

    I'm not sure using word strings here is helpful as it conflicts with C-term... there are no C-strings involved here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by vart View Post
    Why not? It exactly what it does...
    as I said it replaces char sequence ttoken(a) with char sequence 0a
    And is 0a an octal number?
    Quote Originally Posted by vart View Post
    or char sequence ttoken(71) with char sequence 071

    I'm not sure using word strings here is helpful as it conflicts with C-term... there are no C-strings involved here
    Alrighty! char sequence it is then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM