0

I am watching a YouTube tutorial and this code was in the video, and worked fine for them.

But I get the 'error: expected expression' error when I build / run it.

This is the error message line I get it on :

../main.c:33:12: error: expected expression
        average = float(total) / float(howMany);
                  ^

This is the video I was following : https://www.youtube.com/watch?v=gWppLYaCICM

I couldn't find any solutions that fitted this exact problem, nor that made sense to me.

Thanks in advance.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
A. Lord
  • 157
  • 1
  • 3
  • 10
  • Possible duplicate of [What exactly is a type cast in C/C++?](http://stackoverflow.com/questions/7558837/what-exactly-is-a-type-cast-in-c-c) – phuclv Mar 18 '17 at 04:05
  • `float(total)` is a C++-style cast so obviously it won't work in C. Try compiling in a C++ compiler and you'll see. Other duplicates: [code compiles with g++ but not gcc](http://stackoverflow.com/q/24583476/995714), [Explicit type casting operator in C/C++](http://stackoverflow.com/q/39888189/995714) – phuclv Mar 18 '17 at 04:06

1 Answers1

2
average = float(total) / float(howMany);

You got the syntax for the casting wrong; it is the data type being cast to the variable that goes inside parenthesis, not the variable itself.

Do this:

average = (float)total / (float)howMany; // "float" goes in parenthesis here, not "total" or "howMany"

As an aside, you do not need to cast the dividend and the divisor; even if you cast only one of them to a float, the end result will be saved in average as a float value, assuming that average is of type float itself.

So doing this:

    average = (float)total / howMany; // Here, only one of the variables involved in the mathematical operation is casted to float.

Is the same as casting both variables to float.

Good luck!


EDIT: As an aside, The following syntax which did not work in C is valid in C++:

average = float(total) / float(howMany);
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • try `float(total)` in a C compiler to see how it "works". But wait, you said "You got the syntax for the casting wrong" which means it's wrong in C and now you say it works in C? – phuclv Mar 18 '17 at 16:32
  • Can you rewrite your comment? It makes absolutely no sense at all. – BusyProgrammer Mar 18 '17 at 16:33
  • "You got the syntax for the casting wrong" => you said the syntax is wrong. Then "It also works in C" => now you mean the syntax works in C. You are the one that didn't make sense – phuclv Mar 18 '17 at 16:37
  • No, I meant that the __corrected version__ that I included in my post: `average = (float)total / (float)howMany;` is what works in C – BusyProgrammer Mar 18 '17 at 16:40
  • what I mean is `float(total)` is one correct way in C++. In C the only way is `(float)total` – phuclv Mar 18 '17 at 16:41
  • Ah, I see. I thought you meant that `average = (float)total / (float)howMany;` would have only worked in C++. A slight misunderstanding there, but thanks for the clarification. – BusyProgrammer Mar 18 '17 at 16:44