2

The make difference between double and float data, we add f. But when i tried to write:

float Value = 255f;

The compiler diplays the following error.:

  line 50: error (dcc:1633): parse error  near 'f'                    
  line 50: error (dcc:1206): syntax error                             
  line 50: fatal error (dcc:1340): can't recover from earlier errors  

Why ?

user207421
  • 305,947
  • 44
  • 307
  • 483
Stack Over
  • 425
  • 2
  • 8
  • 18

3 Answers3

5

According to draft n1570, §6.4.4.2, paragraph ¶2

Description

A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. The components of the significand part may include a digit sequence representing the whole-number part, followed by a period (.), followed by a digit sequence representing the fraction part. The components of the exponent part are an e, E, p, or P followed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part has to be present; for decimal floating constants, either the period or the exponent part has to be present.

I made the relevant part bold, so you can see why it doesn't work.

Note that this also implies that

float value = 255e0f;

works.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
2

You need a period in addition, in order the compiler to accept it:

float Value = 255.f;

It's part of the standard specification but I guess it was chosen this way in order to simplify the implementation of the lexical analyzer and also improve the readability of the number, since it's easy to think it's a hex number otherwise.

JuniorCompressor
  • 19,631
  • 4
  • 30
  • 57
  • It doesn't simplify the lexical analyzer to any non-trivial degree. – user207421 Feb 17 '15 at 22:36
  • @EJP If you were allowed to write *hexadecimal* floating-point literals with only a mantissa, there would be a genuine lexical ambiguity because 'f' is a hex digit. (This isn't why the spec is how it is, though, since the 'f' suffix predates hexadecimal floating point literals.) – zwol Feb 17 '15 at 22:43
0

My guess would be that you are missing a period in your code.

 float Value = 255.f;

Add a period before f and you should be good to go. If this doesn't work, please tell us what Error you're receiving.

Ryan
  • 3
  • 2
  • 7
  • 1
    The OP clearly states that the problem is the **lack** of the period, the question was why? – Iharob Al Asimi Feb 17 '15 at 18:29
  • The compiler doesn't understand it if you don't add the period. It's just the proper way to type the code. – Ryan Feb 17 '15 at 18:32
  • 1
    It has nothing to do with the compiler, it's the standard that specifies this, so the compiler is just compilant with the standard, but someone could write a compiler which doesn't care about this, note that you didn't specify which compiler requires this. – Iharob Al Asimi Feb 17 '15 at 18:35