#define question: are all occurences replaced?

This is a discussion on #define question: are all occurences replaced? within the C Programming forums, part of the General Programming Boards category; If I were to put an instance of something that has been #defined inside a string, would it still get ...

  1. #1
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    1,051

    #define question: are all occurences replaced?

    If I were to put an instance of something that has been #defined inside a string, would it still get replaced? i.e.

    Code:
    #include <stdio.h>
    
    #define MYSTRING hello
    
    int main(void)
    {
        puts("MYSTRING world!");
    
        return 0;
    }
    Would this return "MYSTRING world!" or "hello world!"?

    Now, I know I could've just tested that myself, but my other question is if this is preprocessor specific behavior or if it's standards defined or what. Whichever result, can I assume that all compilers would treat it the same way?
    Last edited by Epy; 12-10-2009 at 05:38 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,618
    Quote Originally Posted by Epy
    If I were to put an instance of something that has been #defined inside a string, would it still get replaced?
    No. If you want to hear from the horse's mouth for precisely this case that you have in mind:
    Quote Originally Posted by C99 Section 6.10.3 Note 144
    Since, by macro-replacement time, all character constants and string literals are preprocessing tokens, not sequences possibly containing identifier-like subsequences, they are never scanned for macro names or parameters.
    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

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    1,051
    Thanks laserlight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  3. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM
  4. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  5. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM

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