CircArgs CircArgs -3 years ago 122
C Question

C Macro type variable

I am relatively new to C and am trying out different things.

I have a C

test.c
script which I think makes my expectations and goals clear:

#define TYPE float

__declspec(dllexport)
TYPE VectorVectorDot(TYPE u[], TYPE v[]){
TYPE result = 0.0;
for (int i = 0; i < 10; i++){
result += v[i]*u[i];
}
return result;
}


I know
float
is a keyword, yet I made the assumption that the C preprocessor was more or less a template processor whereby macro variables are effectively considered text and prior to compilation the preprocessor parses the script and simply replaces the a #define variable with it's string value and would not care about keywords and whatnot saving that for the compilation stage. This however doesn't seem to be the case I've learned.

Attempting
gcc -c test.c
results in
warning: useless type name in empty declaration
followed by
error: unknown type name 'u'


How is this commonly done (setting type in some manner before compilation)?

EDIT: having placed a comma between the function arguments resolved the initial error. Now, with the code (updated above) I get
warning: useless type name in empty declaration
followed by
error: unknown type name 'u'
. So, it seems to me that the preprocessor is looking at something to do with syntax instead of just naively repplacing all instances of
TYPE
with
float
. Is this the case?

Answer Source

The problem does not lie in the macro, but in your function prototype, where you miss a comma between the parameters.

So change this:

TYPE VectorVectorDot(TYPE u[] TYPE v[])

to this:

TYPE VectorVectorDot(TYPE u[], TYPE v[])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download