Thread: Declaring '#define's that build on each other?

  1. #1
    Registered User
    Join Date
    Apr 2019

    Declaring '#define's that build on each other?


    When I create a bash shell script, and there are user defined constants needed, I like to create constants that build on each other. For example:

    readonly base_path="/home/somehere/"
    readonly user_filename="${base_path}some_file.txt"
    readonly log_filename="${base_path}log_file.txt"
    Which allows the user to change 'base_path' to what they need, but updates two (or more) variables. Less chance the user screws up or forgets one of them.

    Is this at all possible in C with '#define'? And if so, how? I'm not having much luck with my searches.


  2. #2
    Registered User
    Join Date
    Apr 2019
    Found it. Can someone delete this post please?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Adjacent string literals are concatenated, and recall that the preprocessor does fairly simple substitution.
    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

  4. #4
    Registered User
    Join Date
    Dec 2017
    Yes, that is commonly done. The trick is that consecutive string literals, separated by only whitespace (incl. newlines) are "pasted" together into one string literal.
    #define BASE_PATH     "/home/somehere/"
    #define USER_FILENAME (BASE_PATH "some_file.txt")
    #define LOG_FILENAME  (BASE_PATH "log_file.txt")
    Ordinary language is totally unsuited for expressing what physics really asserts.
    Only mathematics can say as little as the physicist means to say. - Bertrand Russell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-23-2020, 12:03 PM
  2. Replies: 0
    Last Post: 01-19-2017, 11:05 PM
  3. Replies: 2
    Last Post: 07-31-2015, 02:11 PM
  4. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  5. need help declaring an msg
    By kwm32 in forum Windows Programming
    Replies: 6
    Last Post: 03-24-2004, 02:49 PM

Tags for this Thread