Thread: GCC vs CC

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    GCC vs CC

    My program compiles fine in CC but incorrectly in GCC:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
    double x, i, S;
    
    // 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21
    
    for (x = 1; x <= 21; x+=2) {
    i = 1 / (x * x);
    S += i;
    }
    
    printf ("S = %.5f \n\n", S);
    
    }
    When I compile it using gcc q1.c, I get the following result:
    S = -22452648517046770147310326100015052338938211161632 56053844213213800554480952418149539187719731500677 82115693990359088513302094872312653146249325474798 85186409959787744993289388394969833238213987598470 26418386525679599720569829779511389690075766458348 07024348080598449351276885195608688031639648498086 04135424.00000

    When I compile my program using the cc q1.c command, I get the correct result:

    S = 1.21099


    Anyone know what's causing the disparity and how to get my code to compile correctly in GCC?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    gcc (4.0.1) gives
    Code:
    S = 1.21099
    although it does warn about the missing return value at the end of main. That's a bit of a shame, really, since you never initialized S to 0.

    Edit: If I use -O3 so that it can catch that sort of thing, gcc comes through:
    Code:
    $ gcc -Wall -Wextra -O3 -o q1 q1.c
    q1.c: In function ‘main’:
    q1.c:5: warning: ‘S’ may be used uninitialized in this function
    q1.c:16: warning: control reaches end of non-void function

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Hmmm. I don't see how the above result of zillions of digits can ever be excused. I smell a compiler or C library bug.

    Whether S is initialized or not, there is no way it should be able to blow up the 5 digit limit that was asked for.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It prints five decimal digits at the end, just as requested. (Note the 5 after the period not before; also recall that precision in output is minimum, not maximum.)

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    Hmmm. I don't see how the above result of zillions of digits can ever be excused. I smell a compiler or C library bug.

    Whether S is initialized or not, there is no way it should be able to blow up the 5 digit limit that was asked for.
    Since the value is not initialized, it will contain "anything" - in this case, it appears to be a very large number. If you do:
    Code:
    double f = 1.0E100; 
    printf("&#37;.2f\n", f);
    then you will get a 1 and 100 zeros printed. However, if the value is not one that can be clearly represented in binary, then the value will be infinite (e.g. 0.7 will actually be 0.699999999, and there are many other such values - essentially anything that is 2^n / 10).

    And of course, the behaviour/value of uninitialized variables would be very much dependant on the way the runtime works, the way the compiler generates the code, and several other factors, so it is no surprise that two different compilers come up with different results - just like two drivers driving across a road against a red-light could experience completely different situations.

    --
    Mats
    Last edited by matsp; 10-15-2008 at 02:20 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Compiles on gcc 3.3 but not on gcc 4.0.3
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2007, 12:24 PM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. CC and GCC compiler problems
    By gayomard_mehta in forum Linux Programming
    Replies: 3
    Last Post: 12-07-2004, 03:49 PM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM