Macro definition

This is a discussion on Macro definition within the C Programming forums, part of the General Programming Boards category; I have a question about the macro shown as follows: Code: #define _MUXSELf(a) 0##a << 30 What does 0##a mean ...

  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
    22,259
    It basically joins the tokens on its right and left, e.g., if a was 123, you would end up with 0123 << 30.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,259
    Yes, the fact that you wrote ttoken(a). You should be using octal digits instead, e.g., ttoken(71).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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,057
    And the "##" preprocessor operators are used for string catenation.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    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, 02:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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