Question from a Noobie

This is a discussion on Question from a Noobie within the C Programming forums, part of the General Programming Boards category; This is my first here - i love this website. I have been teaching myself C and have a very ...

Question from a Noobie

This is my first here - i love this website. I have been teaching myself C and have a very beginner question that is probably easily answerable. Why does the following program give me garbage for the "long" type value? Will any other data type besides "float" work? Why not? Thanks in advance.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

One problem I see is that you are dividing by a number such that your result, after truncation, is likely to be outside of the range of long. A check shows that such a conversion from double to long results in undefined behaviour, hence the garbage that you observed.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.