Thread: int = double. No warning from GCC?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    int = double. No warning from GCC?

    Seems strange to me that
    Code:
    int a = 1.5;
    Produces no warnings on GCC 4.2.3 even with "-Wall -pedantic -ansi".

    I thought GCC used to warn me about that? (possible loss of precision)... Or am I remembering stuff from Java?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Do you have compiler warnings turned off in the build options? I know codeblocks has an option for "inhibit all warning messages". Just a thought.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I'm calling gcc(g++) directly from the command line.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the gcc documentation says this falls under a category enabled with "-Wextra" (and -Wall does not include this)

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's strange. "-Wexta" doesn't work either... I've also never heard of this flag.
    Code:
    #include <stdio.h>
    
    int main(void) {
       int a = 1.5;
    
       fprintf(stdout, "&#37;f\n%d\n", a, a);
    
       return 0;
    }
    Code:
    bill@bill-laptop:~$ gcc -Wextra -pedantic -ansi test.c
    bill@bill-laptop:~$ ./a.out
    0.000000
    1
    Last edited by SlyMaelstrom; 09-16-2008 at 10:52 PM.
    Sent from my iPad®

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A large number of programmers don't bother with an explicit cast when causing a floating point truncation. There are huge amounts of code out there which would produce zillions of warnings if this were always reported. The GCC creators have decided that in this case, the programmer knows best, and does not warn.

    Visual C++ does warn for this implicit conversion. Maybe that is what you are recalling.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah, I see.

    I am pretty sure I have never used Visual C++ in my life.

    Guess it must've been from Java.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    With Mingw (3.4.2) the warnings seem different for C and C++: one warns about the conversion, the other warns about the wrong parameters to fprintf (with -Wall and -Wextra).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    why is it listed in their documentation that these warnings will be displayed if "-Wextra" is used? is it a bug? do you have to somehow request or build your own "verbose" gcc?

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I know this is somewhat of a broader interpretation of the standard, but in affect this sort of behavior is compliant. A language called D exists, and in D a variable becomes a float in this circumstance. The attitude about this situation in D is that what the programmer wrote knows best, even if it was a typo.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    GCC 4.1.2 gives me a warning for all three truncating assignments with -Wall -Wextra -pedantic -ansi:
    Code:
    int main()
    {
            double d = 1.5;
            int i1 = 1.5;
            int i2 = d;
            i1 = d;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I am surprised to tell the truth! Think of this, compiled with -Wall -Wextra in gcc:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
    double d; int i;
    i = 21231231231; //line 7
    d = 21231231231;
    i = d;
    printf("%d",i);
    return 0;
    }
    Warning for overflow on line 7. Correct. Now delete line 7. No warning!! And wrong result of course on printf() ...

    Why would it let you do this? Aside for truncating the decimals from a floating number, double can store bigger numbers than the maximum integer. Why not HAVE to cast at least? You will have the same result, don't get me wrong. But lets say that you write a huge program and you don't think that i is a double. You will get an error. If you HAD to cast then you would get a compiler error.
    It is also the whole idea. Types are types, they shouldn't be mixed that easily, no matter the logic behind it.

    Anyway, g++ gives you warnings

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    GCC 4.1.2 gives me a warning for all three truncating assignments with -Wall -Wextra -pedantic -ansi:
    Code:
    int main()
    {
            double d = 1.5;
            int i1 = 1.5;
            int i2 = d;
            i1 = d;
    }
    cyberfish@cyberfish-desktop:/tmp$ gcc -Wall -Wextra -pedantic -ansi test.c
    test.c: In function ‘main’:
    test.c:5: warning: unused variable ‘i2’
    test.c:7: warning: control reaches end of non-void function
    cyberfish@cyberfish-desktop:/tmp$ gcc -v
    Using built-in specs.
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
    cyberfish@cyberfish-desktop:/tmp$
    Strange...

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by C_ntua View Post
    I am surprised to tell the truth! Think of this, compiled with -Wall -Wextra in gcc:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
    double d; int i;
    i = 21231231231; //line 7
    d = 21231231231;
    i = d;
    printf("%d",i);
    return 0;
    }
    Warning for overflow on line 7. Correct. Now delete line 7. No warning!! And wrong result of course on printf() ...

    Why would it let you do this? Aside for truncating the decimals from a floating number, double can store bigger numbers than the maximum integer. Why not HAVE to cast at least? You will have the same result, don't get me wrong. But lets say that you write a huge program and you don't think that i is a double. You will get an error. If you HAD to cast then you would get a compiler error.
    It is also the whole idea. Types are types, they shouldn't be mixed that easily, no matter the logic behind it.

    Anyway, g++ gives you warnings
    To be more precise, I compile with -Wall -Wextra and -std=gnu99. If I don't include -std=gnu99 I get one warning "int constant is too large for "long" type". Don't know what that means.
    If I change the value of d to 1 and exclude line 7 and compile with -Wall -Wextra -ansi -pedantic I get no warnings...

    I just kind see the "logic" behind it. And, seriously, I cannot see the "logic" for not having a warning when you give -Wall and/or -Wextra...

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "int constant is too large for "long" type"
    My guess on the warning is: you are trying to assign (or initialize) a number which is too large for the long data type to hold. Like trying to assign a 64-bit number to a 32-bit data type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM