Thread: Is there a way to use a #define statement inside another #define statement?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    114

    Is there a way to use a #define statement inside another #define statement?

    Hi,

    Is it possible at all to include a #define statement inside another #define statement?

    ie: (I know this doesn't work)

    Code:
    #define PATH_TO_FILE "/here/there"
    #define FILE_COMMAND "some_program " PATH_TO_FILE "rest_of_the_arguments"
    Side note, the program is not mine and I cannot change the order of the arguments.

    I know I could have two different #define s, but then I would have two entries of the file path to change each time it changes. I'd like to have the file mentioned once and always referenced from that source.
    Ty.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > ie: (I know this doesn't work)
    Then what you have isn't what you represented.
    Code:
    $ cat foo.c
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define PATH_TO_FILE "/here/there"
    #define FILE_COMMAND "some_program " PATH_TO_FILE "rest_of_the_arguments"
    
    int main(void)
    {
      printf("%s\n",FILE_COMMAND);
    }
    $ gcc foo.c
    $ ./a.out 
    some_program /here/thererest_of_the_arguments
    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
    Apr 2019
    Posts
    114
    GDI, I had a typo in one of my earlier tries. The time is gone forever.

    Ty for the smack.

    I have done this before, that's why I thought it was possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. trouble with #define statement...
    By tabl3six in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2011, 04:51 AM
  3. Replies: 11
    Last Post: 04-14-2010, 09:32 PM
  4. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  5. #define inside a function
    By shanthala.sagar in forum C Programming
    Replies: 8
    Last Post: 02-24-2009, 03:02 AM

Tags for this Thread