integer literals - compatible data types

Hello, I am reading a beginners guide and it says "By default, the c++ compiler fits an integer literal into the smallest compatible data type that will hold it. Therefore, assuming 16bit 10 is int by default but 103, 000 is a long."

But then it says, "By default, floating point literals are assumed to be double". Thus, the value 123.23 is type double"

So I am not sure if doubles and ints work differently or if I am not understanding what it is saying. The way I thought it would work would be that 123.23 would become a float since float use less memory than doubles. Does the memory not matter when it comes to floats and doubles, does that not get taken into account and thats why it says all floating points become doubles?
You're reading it correctly. They are treated differently.

A good rule of thumb is to always use double unless you need to use float to save space. Calculations are generally done using the double format anyway, IIRC.
The basis of the question isn't entirely correct. For example, although the literal 123 fits into the integral type short, it has type int.

To write a literal with type float, write the letter f at the end of the literal:
1.23f
There are similar suffixes for the various integer literals.

mbozzi wrote:
The basis of the question isn't entirely correct. For example, although the literal 123 fits into the integral type short, it has type int.

Good point. See the table about halfway down for the details:
https://en.cppreference.com/w/cpp/language/integer_literal
123 fits into char as well, not that it really matters here.

The only time I used floats after college (where profs seem to love them) was talking to hardware that expected the smaller format.

Memory: it matters, eventually. a billion floats vs a billion doubles is significant on a low end computer. Standard/low end home computers have 32gb of memory most of the time, some older ones, 16, top end gamer systems 64gb. So 1) if float will do the job and 2) you have more than say 4gb worth of them to handle, you may want to start thinking about either memory use by cutting down to floats or better management (solving the problem in pieces instead of one big blob of memory). To many 'it depends' to really answer beyond 'yes, memory matters at some point'. Typically though the folks needing large numbers of doubles in memory all at once are doing scientific computing or handling big data and have the hardware to back it up (a server or science box, unlike a home system, can have a TB or more of ram...)

another answer is that the language has to default to *something* or have some rules to choose what to pick, and so long as you can over-ride that default its fine. You can do that; there is a 'what is this' for literals of every type that will over-ride. In the case of 64 bit ints, my compiler won't take them without the over-ride, which is ULL for unsigned long long.
Last edited on
float is commonly used in computer graphics because they are faster to transfer to the GPU; the smaller memory footprint reduces bus pressure.
Last edited on
Topic archived. No new replies allowed.