-
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
-
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:
but it should be:
Code:
int main()
{
return 0;
}
-
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.
-
Type cast to get rid of the error.
-
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.
-
>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
-
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.
-
>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
-
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.
-
>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
-
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.
-
>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