Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ decimal separator syntax mistake

I recently made a syntax mistake by writing this:

b = (float)a*0,1;

instead of:

b = (float)a*0.1;

I am surprised to find out that the first line don’t even put a compilation error. I am even more confuse to see that the following line produce compilation error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

float b = (float)a*0,1;

Can someone explain me why the third line is a syntax error but not the first (in fact it is) ?

>Solution :

This …

b = (float)a*0,1;

… is a statement. Specifically, an expression statement. In the expression within, the comma (,) is an operator, with operand expressions b = (float)a*0 and 1. The overall statement is equivalent to

b = (float)a*0; 1;

, and the comma expression itself evaluates to the value of the second operand (1), which happens to be ignored in this case.

On the other hand, this …

float b = (float)a*0,1;

… is a (malformed) declaration. The comma here is not the comma operator, but rather a separator between items being declared. This declaration is erroneous because the second item does not contain a declarator designating the item being declared. (The declarator in the first item is b.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading