1

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?

CircArgs
  • 570
  • 6
  • 16
  • 5
    `TYPE u[] TYPE v[]` => `TYPE u[], TYPE v[]`, just missing a comma, not a macro issue – Jean-François Fabre Aug 15 '17 at 20:58
  • 2
    You actually do need a comma: `VectorVectorDot(TYPE u[], TYPE v[])` – Michael Burr Aug 15 '17 at 20:58
  • 2
    You could also use a `typedef`, since using macros for this is generally discouraged. – Nothing Nothing Aug 15 '17 at 20:59
  • 1
    Aside: what is `n`? – Weather Vane Aug 15 '17 at 21:00
  • preprocessor does not take anything into accunt. it does not know anything about C – 0___________ Aug 15 '17 at 21:02
  • @WeatherVane whoops. just a mistake as this is just example code. see my edit comment – CircArgs Aug 15 '17 at 21:02
  • @Jean-FrançoisFabre Thanks. With the comma there is still some issue as if the preprocessor is still considering syntax – CircArgs Aug 15 '17 at 21:12
  • 1
    As a rule, you do not want to use a preprocessor macro to abstract away a type name. It works for simple types, but it won't work for pointer and array types (and combinations thereof). It's better to use the `typedef` facility for that. – John Bode Aug 15 '17 at 21:16
  • @PeterJ well, the preprocessor shares C's definition of *integer constant expression*, e.g. `#if 42 == 6 * 9` – M.M Aug 15 '17 at 21:18
  • @NothingNothing I had actually done this, with other examples I practiced with, yet I wanted to see if this would work as I saw something in a tutorial elsewhere. I was also curious about convention, but others seems to say typedef is the cleanest and most conventional way – CircArgs Aug 15 '17 at 21:22
  • @M.M same syntax. Not shared. using this logic Python shares it with the C as well – 0___________ Aug 15 '17 at 21:25
  • @JohnBode Do you mean it won't work for say a function returning a point such as `int * example(void){ return malloc(4)}` ? – CircArgs Aug 15 '17 at 21:25
  • @CircArgs [This SO question](https://stackoverflow.com/questions/1666353/are-typedef-and-define-the-same-in-c) might be of help. – Nothing Nothing Aug 15 '17 at 21:28
  • @NothingNothing Thanks for the link. I think I'll stick with `typedef` from now on. Still unsure of the final error there... – CircArgs Aug 15 '17 at 21:34
  • @CircArgs `#define` is a plain text replacement. You would get the same error if you replace all usage of `TYPE` with `float`. If you are still having trouble please post a MCVE. The code you have posted so far compiles correctly for me (using gcc for windows, and MSVC). – M.M Aug 15 '17 at 21:46
  • Also it would be good to indicate which compiler and platform you are using, since `__declspec(dllexport)` is a non-standard extension – M.M Aug 15 '17 at 21:50
  • @M.M But I've shown that I'm using gcc and dynamic link libraries are a windows exclusive thing are they not? – CircArgs Aug 15 '17 at 23:33
  • 1
    @CircArgs Perhaps the problem was that you used `__declspec(dllexport)` on a non-windows compiler where it was unrecognized. Also there are multiple different ports of gcc to Windows, and different versions of each. – M.M Aug 15 '17 at 23:58

1 Answers1

4

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[])
gsamaras
  • 71,951
  • 46
  • 188
  • 305