Search code examples
objective-ccocoacocoa-touchc-preprocessorstringification

C preprocessor on Mac OSX/iPhone, usage of the '#' key?


I'm looking at some open source projects and I'm seeing the following:

NSLog(@"%s w=%f, h=%f", #size, size.width, size.height)

What exactly is the meaning of '#' right before the size symbol? Is that some kind of prefix for C strings?


Solution

  • The official name of # is the stringizing operator. It takes its argument and surrounds it in quotes to make a C string constant, escaping any embedded quotes or backslashes as necessary. It is only allowed inside the definition of a macro -- it is not allowed in regular code. For example:

    // This is not legal C
    const char *str = #test
    
    // This is ok
    #define STRINGIZE(x) #x
    const char *str1 = STRINGIZE(test);      // equivalent to str1 = "test";
    const char *str2 = STRINGIZE(test2"a\"");  // equivalent to str2 = "test2\"a\\\"";
    

    A related preprocessor operator is the token-pasting operator ##. It takes two tokens and pastes them together to get one token. Like the stringizing operator, it is only allowed in macro definitions, not in regular code.

    // This is not legal C
    int foobar = 3;
    int x = foo ## bar;
    
    // This is ok
    #define TOKENPASTE(x, y) x ## y
    int foobar = 3;
    int x = TOKENPASTE(foo, bar);  // equivalent to x = foobar;