Thread: how to find the value of a macro?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    how to find the value of a macro?

    Hello everyone,


    I am wondering what is the easiest and safest way to find the value of a macro? For example, if I,

    #define FOO "value1"

    The value of FOO I can get is "value1" and if then I,

    #undef FOO
    #define FOO "value2",

    then the value of FOO I can get is "value2".


    thanks in advance,
    George

  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
    You get the value by using the name
    Eg.
    printf( "Foo is %s", FOO );

    There is no way to determine if FOO will make sense in the context in which you use it.
    Eg.
    #define FOO 123
    printf( "Foo is %s", FOO ); // may get a warning, may blow up at run-time
    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
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Something like this would avoid that potential problem:
    Code:
    itsme@itsme:~/C$ cat macro.c
    #include <stdio.h>
    
    #define FOO 123
    #define BAR "blee"
    
    #define STR2(x) #x
    #define STR(x) STR2(x)
    
    int main(void)
    {
      printf("FOO: %s\n", STR(FOO));
      printf("BAR: %s\n", STR(BAR));
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./macro
    FOO: 123
    BAR: "blee"
    itsme@itsme:~/C$
    The above code allows you to print non-string macros just like you'd print string macros.
    If you understand what you're doing, you're not learning anything.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    If your compiler supports it, set it to preprocess only, and see what the macro is replaced with.
    Code:
    C:\Roys\Projects\cboard>cat hello.c
    #include <stdio.h>
    
    #define HI "Hello World!\n"
    
    int main()
    {
            printf(HI);
            return 0;
    }
    
    C:\...\Projects\cboard>gcc hello.c -E
    <snip>
    int main()
    {
     printf("Hello World!\n");
     return 0;
    }
    Always more than one way to do things.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. L macro
    By George2 in forum C Programming
    Replies: 1
    Last Post: 08-20-2007, 09:24 AM
  3. ld.exe: cannot find -l-lstdc++
    By Tonto in forum Tech Board
    Replies: 3
    Last Post: 04-10-2007, 11:20 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM