1

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:

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) ?

3
  • Sometimes you'll get a compiler warning (usually something like blah has no effect), but when the compiler sees syntactically valid code, no matter how wrong-headed it may be logically, it compiles it. Commented Jul 31, 2024 at 20:44
  • Example of compiler configured to issue a warning: godbolt.org/z/jrrE7bnEW Commented Jul 31, 2024 at 20:54
  • 1
    offtopic: if a is an integer you do not need the cast. And when you do need the cast you should rather use a proper one static_cast<float>(a) Commented Jul 31, 2024 at 21:08

1 Answer 1

5

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.