Making sense of the manual

Jul 8, 2011 at 1:31pm
I've come to the formal explanation of literals, and the manual reads as follows.

All literal values have a data type, but this raises a question. As you know, there are several different types of integers, such as int, short int, unsigned long int. There are also different types of floating point. The question is: How does the compiler determine the type of a literal. For example, is 123.23 a float or a double.


Then it gives an answer. The problem I have is that I thought the compiler was told the type, whether it is a float or a double, when the variable was declared.

Can someone tell me why this is not the case?
(can provide more of passage from book if needed)
Jul 8, 2011 at 1:40pm
Literals aren't declared as variables.

For instance
1
2
3
4
int a;
a = 5;

a = a/2;             //Here 2 is a literal. We haven't declared a variable for it. So the compiler must recognize its Data type itself. 


I hope that clears it!
Jul 8, 2011 at 1:45pm
Yep; thanks
Jul 8, 2011 at 2:56pm
Nisheeth; I've just come across

float f=1.2F;

Why are both float and F used?
Jul 8, 2011 at 5:24pm
Because 1.2 is a double literal. 1.2F is a float literal.

Without the F, 1.2 is a double and it will be cast by the compiler to a float. This should generate a warning, and could lead to loss of data (it probably won't in this case, but it the numbers are much large or the precision greater then it will happen).
Topic archived. No new replies allowed.