New programmer missing somthing -- help!

This is a discussion on New programmer missing somthing -- help! within the C Programming forums, part of the General Programming Boards category; If you did Code: int n = 1 / 1.5; You would get a warning from the compiler (conversion from ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you did
    Code:
    int n = 1 / 1.5;
    You would get a warning from the compiler (conversion from double to int), so you know you did something wrong!
    Actually -- no.
    Code:
    $ cat implicitcast.c
    int n = 1 / 1.5;
    
    int main() {
        return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic -g -c implicitcast.c
    $
    No warnings.

    That's the thing with promotion -- most of the time, it's implicit. The compiler does it without informing you it has done so; it's automatic.

    It's nearly always automatic when you're going upwards, promoting upwards, because no information will be lost. The other way around, however, can cause problems. That's why my compiler complains about this.
    Code:
    char c = 123456789;
    Code:
    implicitcast.c:2: warning: overflow in implicit constant conversion
    chars can only hold up to 127 on most platforms, so that int's way too large to fit in a char. (123456789 is an int literal, and it is being implicitly casted to a char.)

    Note, however, that my compiler does not mind "char c = 1", which is another implicit cast, though one that cannot overflow . . . .

    Yep, it's complicated all right. Welcome to C. . . . actually, in this case it's just me over-explaining things.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by dwks View Post
    Actually -- no.
    Actually, yes:

    int n = 1 / 1.5;
    Gives:
    Code:
    warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
    In Visual Studio.

    GCC just sucks
    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.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Mmm, yes, you're right. That it can give a warning, that is, not that GCC sucks.

    Though this version of GCC (4.2.3) is actually pretty bad about reporting warnings. I include one header file, say stdio.h, and I can use strcmp() (string.h), malloc() (stdlib.h), etc with no warnings.

    I'm not sure why this is. Maybe I'm missing a flag. Who knows.

    On the other hand, older versions of GCC might warn about this -- I can't tell you at the moment.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    If you aren't missing a flag, then that version would be horrible indeed.
    I don't believe the C standard actually allows for implicit conversions like this, though?
    I'm just not very good at the C standard and all these awful compilers allow allow these stupid conversions which should be an error.
    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.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    GCC doesn't seem to warn about this. http://gcc.gnu.org/ml/gcc/2005-09/msg00514.html
    As far as I can tell, gcc does not warn about narrowing assignments.
    I don't think people would object to adding such a warning, though
    probably not under -Wall.
    At least at the time of that writing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Seems like another reason to me to avoid GCC.
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21