Thread: warning message

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    warning message

    I am using microsoft visualc++ 6.0 for my C programming class. I am constantly gettin a warning message whenever I use decimal percentages like .40 or .30. When I define my variables I define them as float, but when my program compiles, I get the warning t
    "truncation from "const double" to "float". The program executes perfectly, but I get the error message. When I turn my disks in to my professor, I would prefer not to have errors. Also, I usually get the warning "main must return a value". Is it my compiler or what? Thanks

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    can you post an example of the problem with the percentages, also by the sounds of it the main must return a value refers to the way you declared your main function a lot of people do:
    Code:
    void main()
    {
    
    }
    but it should be:

    Code:
    int main()
    {
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    also sounds like some optimization thing. Maybe declaring something like

    Code:
    const double cd = 0.3;
    maybe it is storing the double as a float to save memory. who knows what it is doing that's microsoft for ya.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Type cast to get rid of the error.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    You get a warning such as this when you call a function that returns a double value and you are trying to store it in a float value. Hope that helps you. Or alternatively you could just disable them using
    Code:
    #pragma warning(disable : warning_number)
    just put that in your source code but it is compiler dependent so beware. Oh and you find the warning number right after MSVC complains to you.
    Last edited by xds4lx; 04-03-2002 at 01:19 AM.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I get the warning "truncation from "const double" to "float".
    A simple cast will take care of this:
    float f = (float)1.2;

    Or you could declare floating point variables as double. If you have no space constraints or other reasons to use a smaller precision then double is the way to go since constant floating point values are considered to be double by the compiler.

    >#pragma warning(disable : warning_number)
    You could do this and it would probably work, but the problem here is good practice. It's not good practice to simply disable warnings because the warnings are there to help you. They are telling you that there's something wrong with your code even though it still compiles. Disable warnings by fixing them, not hiding them.

    I also don't recommend disabling warnings because it is a bad example to others, if a novice sees you doing this and decides that it's a 'neato' idea, they may use it liberally to get a clean compilation when their program is really broken.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    But not all warnings are errors if you nkow what your doing, the compiler is just warning you that you might lose some information storing a value in a data type that is too small for it.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But not all warnings are errors if you nkow what your doing, the
    >compiler is just warning you that you might lose some
    >information storing a value in a data type that is too small for it.
    Warnings aren't there because some bozo decided to annoy us, or because the compiler writers thought that we don't know what we're doing. They are there because there is a potential problem with the program that doesn't hinder compilation.

    If you think you know what you're doing, or you are sure that the warning can be ignore then so be it. I keep my programs clean because those warnings I could ignore may hide errors that I didn't see.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Im not trying to be a smarta$$ or anything but on a warning like that if you know that your values will only be so large then there is no problem with turning off the warning using a #pragma, but I do agree that some warnings are essential, thats why I like turning the warning level all the way up on MSVC, you know warnings as errors.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Im not trying to be a smarta$$ or anything
    You're only a smartass if you think you know what you're talking about but really don't. So in this case you aren't.

    >there is no problem with turning off the warning using a #pragma
    Ignoring warnings you know are lame is one thing, but using #pragma is another altogether. I explained why it is bad practice earlier in this thread.

    >thats why I like turning the warning level all the way up on
    >MSVC, you know warnings as errors.
    Same here, but warnings as errors is a little overkill. Turn MSVC up to it's most pernickety warning level and get yourself the most vicious copy of lint you can find and you'll be all set. You'd be surprised how badly lint will burn you most of the time.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Well on some projects warnings as errors is overkill but on really huge projects like the 3D engine ive been working on it really helps out.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >on really huge projects like the 3D engine ive been working on it really helps
    I'll grant you that, I can easily imagine how warnings as errors could be useful in that case. Though if you check every warning anyway then having them come up as errors does nothing signifigant.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 06:32 PM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM