Thread: Defining and using a macro

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    4

    Defining and using a macro

    Hello! I am trying to create a small set of filepath functions that I intend to compile across linux and windows (I prefer not to use a big library).

    I want to have a global constant PATH_SEPARATOR that depends on the OS environment. This is what I set at the top of header file.

    Code:
    #include <stdio.h>
    const char PATH_SEPARATOR =      
    #ifdef _WIN32     
    '\\';     
    #else     
    '/';     
    #endif
    I was hoping to test this while compiling this in a linux environment using gcc, thusly:


    Code:
    int main (int argc, char const* argv[])     
    {       
        printf("OSNAME not defined. Separator is: %c\n", PATH_SEPARATOR);          
    #define _WIN32       
        printf("OSNAME defined. Separator is: %c\n", PATH_SEPARATOR);     
    #undef _WIN32 
        return 0;     
    }
    which gives me the following:


    Code:
    _WIN32 not defined. Separator is: /     
    _WIN32 defined. Separator is: /

    where apparently, I seem not to be able to "set" a part of the code to have "_WIN32" defined. I don't know if I explained this clearly. I'm not even sure I got the title correct.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    C macros are applied compiletime, not runtime. Once your PATH_SEPARATOR is defined in can't be changed.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    Thanks Yarin for your reply. However, I still get the same result. I think my problem is how to set "_WIN32" defined in that part of the code printing the PATH_SEPARATOR.

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    Quote Originally Posted by Yarin View Post
    C macros are applied compiletime, not runtime. Once your PATH_SEPARATOR is defined in can't be changed.
    I am not trying to change the value at runtime. I was just hoping to test the value of PATH_SEPARATOR set at compile time by making a #define at parts of the code. Is this possible?

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by champen View Post
    I am not trying to change the value at runtime. I was just hoping to test the value of PATH_SEPARATOR set at compile time by making a #define at parts of the code. Is this possible?
    No.
    It shouldn't be much of a problem, though. You'll need to compile it for (or on) either platform when testing, anyway.

  6. #6
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    Quote Originally Posted by Yarin View Post
    No.
    It shouldn't be much of a problem, though. You'll need to compile it for (or on) either platform when testing, anyway.
    Thanks! I got it working by compiling it with MinGW's gcc compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #Defining
    By pc_doctor in forum C Programming
    Replies: 2
    Last Post: 05-25-2010, 08:04 AM
  2. Defining Integers Etc.
    By dayved in forum C Programming
    Replies: 14
    Last Post: 03-27-2010, 01:15 AM
  3. Replies: 3
    Last Post: 10-25-2007, 09:41 AM
  4. Defining IDs
    By jmd15 in forum Windows Programming
    Replies: 3
    Last Post: 03-12-2005, 01:56 PM
  5. Defining
    By Tigerworks in forum C Programming
    Replies: 7
    Last Post: 03-09-2003, 05:34 AM

Tags for this Thread