10

This little code compiles with both GCC and Clang, but gives different results:

#include <stdio.h>

int main(){

  __int128_t test=10;
  while(test>0){
    int myTest=(int)test;
    printf("? %d\n", myTest);
    test--;
  }

}

With GCC this counts from 10 down to 1, the intended behaviour, while for Clang it keeps on counting into negative numbers. With Clang, if I replace test-- with test-=1 then it gives the expected behaviour as well.

__int128_t is a GCC extension, so the above results only apply to non-standard C, so maybe __int128_t is "use at your own risk" in Clang.

Is this a bug in Clang, or did I make some mistake I'm not seeing?

EDIT: I'm using gcc (MacPorts gcc48 4.8-20130411_0) 4.8.1 20130411 (prerelease) and Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn).

6
  • 1
    Right, this does just what you explained on OS X 10.7.5 with GCC 4.2 and Clang 4.0.
    – user529758
    May 8, 2013 at 18:37
  • 1
    Works fine here on my machine, "Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)".
    – Carl Norum
    May 8, 2013 at 18:38
  • @Carl Maybe Apple fixed it between clang-421.0.60 and clang-425.0.28. I should update to check. May 8, 2013 at 18:46
  • Seems that way, yup. clang is all open-source, so I'm sure you can hunt down the patch if you really want to.
    – Carl Norum
    May 8, 2013 at 18:47
  • I think the updates come through the Mac App Store these days.
    – Carl Norum
    May 8, 2013 at 18:49

1 Answer 1

9

This was a bug in Clang, which was resolved somewhere between Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn) and Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn), see the comments -- thanks Carl and H2CO3.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.