Error: "expected" ("before the string constant")

Work on calculating the geometric mean value in an array

The function should correctly calculate the geo value, but I get a strange error message

#include <stdio.h> #include <stdint.h> #include <math.h> extern "C" double geomean(double myarray[], int count) ////error here, expected '(' before string constant { double geomean = 1; double root = (1/(double)count); int i; for(i = 0; i < count; i++) { geomean = geomean * myarray[i]; } geomean = pow(geomean, root); return geomean; } 
+8
c
source share
3 answers

extern "C" not valid C (it is valid only in C ++). Just delete it if you are working in pure C.

+23
source share

I answer this question, trying to cover this question, which could be considered in a more detailed answer, to help the questioner or other persons who visited this page.

Error: "expected" ("before a string constant"

As mentioned in another answer of your question, extern "C" not valid C (it is valid only in C ++). You can remove it if you use only pure C.

However, if you (or someone else) have mixed C and C ++ source files, you can use the __cplusplus macro. The __cplusplus macro will be defined for any compilation unit that runs through the C ++ compiler. This usually means that the .cpp files and any files are included in this .cpp file.

Thus, the same .h (or .hh or .hpp or what-have-you) can be interpreted as C or C ++ at different times if different compilation units belong to them. If you want the prototypes in the .h file to refer to C symbol names, they must have extern "C" when interpreting C ++, and they must not have extern "C" when interpreting C (as in your case you got an error! )

 #ifdef __cplusplus extern "C" { #endif // Your prototype or Definition #ifdef __cplusplus } #endif 

Note. All extern "C" really affect communication. C ++ functions have distorted names during compilation. This makes overload possible. The function name is changed based on the types and number of parameters, so two functions with the same name will have different symbol names.

If you include a header for code that has a C link (for example, code that was compiled by the C compiler), then you must extern "C" header so you can link to the library. ( Otherwise, your linker would be looking for functions with names like _Z1hic when you were looking for void h(int, char) ).

+2
source share

The first line should be: extern C;

Another option would be to declare c outside the main function without the extern keyword ...

-3
source share

All Articles