Archived

This topic is now archived and is closed to further replies.

xstreme2000

Different number types

Recommended Posts

I have seen a lot of values in a c programs which end with an L eg 75L I guess that these are stored as LONG If I am wrong can somebody correct me. also what other ones are there? This doesn''t seem to be documented anywhere //--- Created by Tom Oram --- // tom.oram@vodafone.net

Share this post


Link to post
Share on other sites
I think your right, but I''m not to sure because I''ve never used it. But I do know that f is float, and I think d is double. Please correct me if I''m wrong.

"When people tell you they want to hear the truth, you know that their lying."

Share this post


Link to post
Share on other sites
Yes, your right. It means that the number is going to be interpreted as a long value. It does the same thing as:
(long) 45.0; or as you can do in C++ long(45.0)

It is most useful when you are dividing two values.

Share this post


Link to post
Share on other sites
Digger is right and it's true that a f means float. But there's no letter which signs float. Numbers with a comma and without an f are always interpreted as double.

Visit our homepage: www.rarebyte.de.st

GA

Edited by - ga on 4/7/00 1:08:20 PM

Share this post


Link to post
Share on other sites
The suffix L is used to tell the compiler that you want to force an integer value to be of type 'long' instead of 'int'.

Why might you need this? It has to do with the C/C++ rules for number conversions, which can be a bit complex. (Check out section C.4 thru C.6 of _The C++ Programming Language_ 3rd edition).

Integer values are promoted to 'int' before they are operated on. If on your system, sizeof(int) == sizeof(long), you might not see any problems. But let's say sizeof(int) == 2 bytes and sizeof(long) == 4 bytes. And you try to do this:

long result = 30000 * 30000;

So, what does result contain? If you answered 900000000, you're incorrect. (I think the end result would be 26880, but I might be off.)

Why? 30000 fits in an 'int', so the compiler represents it as an 'int'. Multiplying two 'int's will give you an 'int'. Problem is that 900000000 won't fit into an 'int'. The fact that you attempt to store the result in a 'long' is after-the-fact; the multiplication has already lost the value.

This IS the correct behaviour (if I've read the standard correctly) and I expect was done to maintain backwards compatibility. How do you fix it? Use the L:

long result = 30000L * 30000L;

Now, the compiler treats those 30000 values as 'long', and your result will be correct.

Now, if you're telling me that you compiled the first version (without the L's) and you got the right answer, that's probably because on your system, sizeof(int) == sizeof(long) == 4 bytes. It should be possible to perform a similar test with 'long' and 'long long'.

It's something that, coding in Windows or Mac or similar, you might never hit the problem and never need to use the L. But you need to remember it's there. I've done Palm programming which does use a 2 byte int, and if you're not careful, you will hit this problem and have to use L or whatever is appropriate.

And there's more than just L. You can use U (forces the value to be unsigned) and F (forces value to be a 'float' instead of the default 'double'). U and L can be combined.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Edited by - mossmoss on 4/7/00 1:33:34 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by mossmoss
long result = 30000 * 30000;

So, what does result contain? If you answered 900000000, you''re incorrect. (I think the end result would be 26880, but I might be off.)




perfectly right
but i must correct you that the result of the above operation is just undefined. the signed int will overflow and you never know if it just rolls over to negative values or does other fancy stuff

Share this post


Link to post
Share on other sites