??) displays as ]

Discussion in 'Mac Programming' started by jared_kipe, Jun 3, 2009.

  1. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #1
    UILabel.text = @"sin(??)";
    gives output: sin(]

    UILabel.text = @"??/H";
    gives compiler warning about unknown escape string

    What is going on? I could probably get around this by doing something like..
    NSString *unknownString = @"??"; // I know @"??" displays fine
    UILabel.text = [NSString stringWithFormat: @"sin(%@)", unknownString];


    BUT I don't want to, I have to work with a lot of ??s in strings, and I need them to not frack up all the time.
    I've have tried searching extensively and looking at articals on escaping strings, but cannot figure out the real reason behind this mystery. Probably because Google appears to ignore the ?? in "?? NSString" or "?? escape character"
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Maybe try escaping them?
    Code:
    UILabel.text = @"sin(\?\?)";
     
  3. thread starter macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #3
    This worked, I attempted escaping the first one and it resolved one but not all of my problems.

    \??* worked
    \?\?* works too
    \??) doesn't work
    \?\?) does work.

    What is damned odd is that if you take out one question mark
    ?) works
    What is the ?? characters actually doing?
     
  4. macrumors member

    Joined:
    Aug 26, 2008
    #4
    Trigraphs

    Check out the following page:

    http://en.wikipedia.org/wiki/Digraph_(computing)

    Never ran into this before; hmm. Not exactly sure how you turn off the trigraph feature in Xcode. Sounds like it's automatically turned on when you use the -ansi and/or -std=<> gcc flags, which I think Xcode does by default. Good luck with this one...
     
  5. thread starter macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    Thank you SOOOO much. When I got to the section on Digraphs, I remember reading about those in one of my programming book. That is, the digraphs, I've NEVER seen trigraphs before.

    What is still amazing to me is that googling "??) c string" does me no good. Oh and don't bother with the first link, NSFW!
    Googling for every combination of ??) and the like turned up nothing either.

    EDIT: Programming in Objective-C 2.0 by Stephen Kochan has Digraphs on p505, Trigraphs on p561. :(
     
  6. macrumors 6502

    Joined:
    Apr 24, 2008
    #6
    This is one of those things which probably should have been gotten rid of long ago, but then that "new" C would break some program written long ago by someone with a weird keyboard without a ] key...

    The C standard doesn't prescribe ASCII. There is a definition for the "source character set", where some characters are "optional" and can be replaced by trigraphs. Note that also the $ and @ characters are not in this set, so they are invalid in C identifiers.
     
  7. thread starter macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #7
    I should rewrite all my source code replaceing the Obj-C [x] calls with ??(x??)

    It is so much more readable don't you think. Oh and the if statements and method curlys

    if ( ??(myObject isTrue??) ) ??<
    int i=1;
    ??>

    I love it!

    I think something like this could probably fix it.
    #ifndef ??
    #define ?? \?\?
    #endif
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    I was thinking of something like that, too, but from what i read the trigraphs are handled by the preprocessor, so you'd be gaming when the preprocessor performed particular steps, and you may or may not get what you're hoping for.

    -Lee
     
  9. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #9
    I believe there's a checkbox in the build settings to turn trigraphs off.
     
  10. thread starter macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #10
    The more I think about it, the more I think trigraphs/digraphs should be removed from the C spec.

    Their ability to confuse and hurt programmers outweighs the need to keep them around.

    What is the likeliness that code requiring trigraphs/digraphs is still needing to be compiled?
    Any code requiring trigraphs/digraphs could be updated with a simple search and replace trip through an editor OR they can compile it on an older version of GCC or whatever.
     
  11. macrumors 6502

    Joined:
    Apr 24, 2008
    #11
    Good, send an email to the C standardization committee. I'm sure they have been hoping that someone would think about it some more, and will be happy to update the specification according to your insights. While you're at it, perhaps you can ask them to remove octal constants too (another fun source of confusion).

    That is not the point. C specifies the "source code character set" and that happens to be not the same as ASCII. Remember that C is also used on "obscure" embedded platforms and weird CPUs.

    I didn't want to sound harsh. I personally agree that they probably should have been removed, specifically when going to C99, but apparently the standardization committee decided otherwise and they're a bunch of much smarter guys than all of us combined.

    Note that GCC ignores trigraphs by default, and only enables them in standards-conforming modes. You can explicitly enable support for them using the -trigraphs compiler switch. You could try adding -no-trigraphs to your project settings. If I try this on the command line, gcc seems to swallow this option...
     
  12. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    It's not just about weird and obscure platforms. The gcc compiler on your Mac supports source files in Utf-8 format. Try printing strlen ("äöüÄÖÜ"). Hint: The result is not six.

    Basically the rule is: If your source code contains a string with the sequence ??, then replace it with \?\?. Except when the source code really wanted a trigraph character, then replace it with the right character. That way your code will work correctly and without warnings whatever the compiler thinks about trigraphs.
     
  13. thread starter macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #13
    This is a PRECOMPILER directive. Does not affect how the machine code will run.

    All the substitute characters are in ASCII, they are just for keyboards that didn't have # ] characters when the original source was written.

    I do not believe removing this from the C standard would make it harder to develop C software for imbedded or "weird" platforms.
     
  14. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #14
    Question 1: If this was removed from the C standard, is it possible that some program would not compile anymore?

    Question 2: If this was removed from the C standard, is it possible that some program would malfunction?

    A change like that is very, very dangerous.
     
  15. thread starter macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #15
    Yes. Though it would be simple to fix, or they could compile it with an earlier complier.

    Not the way I see it. Previously compiled programs would be fine (obviously, they are machine code already). Recompiling code that used them will not compile, thus they wouldn't get malfunctioning machine code, they would get compile errors and no machine code.

    OK so maybe not remove it, but make it so it isn't the standard/default way Xcode or other compilers operate.
     
  16. macrumors 6502

    Joined:
    Apr 24, 2008
    #16

Share This Page