declaring floating-point type variables

I am learning C++ through the book C++ Primer Plus (5th edition). I'm at the part where you are learning the different types of variables and constants, and escape sequences, bits and bytes, all that stuff. So far I have got it pretty good, but am becoming confused on floating point types and constants.

I know that when you are declaring a constant you can add suffixes to the end of the declaration to change the type. Like this:
1
2
 1.234f       // a float constant
2.345234E28L // a long double constant  

And I get that. But some example code in the book shows this:
1
2
float a = 2.34E+22f;
float b = a + 1.0f;

I know that that code means it is declaring two variables of the type float. Variable a is 2.34e22 (2.34 X 10^22 in scientific notation). Variable b is variable a + 1. That I understand. What I don't get is why there is an f at the end of each number. I know that with constants that changes the type to float. But the type is already float because of the declaration. The word float in front of the variable name creates a variable of the type float. So why do you need an 'f' at the end. The numbers aren't considered constants.
Last edited on
1.0f is a constant literal.
Wait, I thought the only constants were bare numbers or the symbolic constants (not including characters from char and stuff like that), like this:

1
2
const int MONTHS = 12;       // Creates a symbolic constant
cout << 12;         // displays a normal(?) constant 


Do the numbers you assign to variables count as a constant. Are all plain numbers constants?

Even with it being a constant, why do you need the f after it. It is already considered a float type due to the word float in front of the variable name in the declaration.
I'm not sure I understand your confusion.

1.0f is a constant literal value. Anywhere you have such text it is a constant literal value by definition. It doesn't necessarily exist anywhere in memory.

const int FOO declares a constant value. It necessarily exists somewhere in memory, and is 'constant' only in the sense that your program cannot change its value (at least not directly).

The 'f' (and other) suffixes qualify the type of a constant literal. Nothing else does.
1
2
3
4
5
int x = 12.5e9f;  // assign a floating point (constant) value to an integer variable

float y = 5;  // assign an integer (constant) value to a floating point variable

const float pi = 3.14159265f;  // initialize a (constant) variable with a constant literal value of the same type 


By default, 3.14159265 is a double, so

const float pi = 3.14159265; // initialize a float with a double

which is somewhat wasteful... hence:

const float pi = 3.14159265f; // initialize a float with a float

Hope this helps.
So a constant would be any plain number like:
int a = 165; // assign a constant of type integer (because by default, it is type integer) to integer variable a
So the 165 is considered a constant until it is assigned to the variable a, because it is a plain number. So any number that is placed in the open like:

cout << 36 + 14;

is a constant. The statement above would be adding the constant 36 to the constant 14, and then displaying the answer. Right?

So the reason for the 'f' (in the original question) is to change the number to type float. Because it is type double until it is assigned to the float variable. So when I do something like this:
float a = 468;

I am actually assigning a type int to a float variable (a).

So does the type change to the variable type after it is assigned to the variable. Like:
1
2
3
float a = 468
a * 2
a* 2.1


After 468 is assigned to a, does that mean the number is now a type float, or is it still a type int, until it is multiplied by 2.1. Then, since type int cannot store fractions, does the number become a float?
Am I making sense?
When you say this

float a = 468;

you're telling the compiler to convert the integer literal 468 to its corresponding float and store the result in variable a.

This means that after 468 is assigned to a, a will hold 468.0f.
When you write a literal value into the code, the compiler needs to know how much memory to store the value in. So it needs to know its type.

A float will typically occupy less space than a double. So if I just say 2.34E+22 the compiler will set aside enough memory for a double and store the literal value in there in double format. However, if I say it with an f like this, 2.34E+22f, then the compiler stuffs that value into a memory location the size of a float in float format.

So what the compiler does with the literal is *different* depending on what qualifiers it has (such as f).
I read your guys' replies and the section about constants in the tutorial on this site, and I think I get it now. Thanks for the help.
Topic archived. No new replies allowed.