0

Why am I getting a syntax error for my C header declaration?

Here is my header file, viterbi.h:

#ifndef VITERBI_H
#define VITERBI_H
void vitdec(float* , int , int ,   bool* );
#endif //VITERBI_H

And here is my implementation file, viterbi.c:

// viterbi.c   : Defines the entry point for the console application.
//
#include "viterbi.h"
#include "math.h"
//void vitdec(float* sd, int frameLen, int rate,   bool* hd);

void vitdec(float* sd, int frameLen, int rate,   bool* hd)
{
    //... The rest of the function

The errors from the Visual Studio 2010 compiler read:

viterbi.h(4): error C2143: syntax error : missing ')' before '*'
viterbi.h(4): error C2081: 'bool' : name in formal parameter list illegal
viterbi.h(4): error C2143: syntax error : missing '{' before '*'
viterbi.h(4): error C2059: syntax error : ')'
viterbi.h(4): error C2059: syntax error : ';'
viterbi.c(7): error C2065: 'bool' : undeclared identifier
viterbi.c(7): error C2065: 'hd' : undeclared identifier
viterbi.c(7): warning C4552: '*' : operator has no effect; expected operator with side-effect

As far as I have seen/can tell, this is valid syntax for a C declaration. If I compile viterbi.c as C++ code (viterbi.cpp), then the errors disappear. What is the syntax error?

Stephen Wang
  • 31
  • 1
  • 8
  • 2
    There is no such thing as `bool` in `C` (89). You want to compile `C` code using Visual Studio, then you gotta write `C` code, not `C++` code. – PaulMcKenzie Jul 27 '16 at 18:26
  • 1
    Looks like `bool` is probably the problem: http://stackoverflow.com/a/18042253 – jonhopkins Jul 27 '16 at 18:27
  • @PaulMcKenzie: `bool` is a macro mapping to the **standard type** `_Bool`. and there are recommendations not to redefine `bool` etc. in the application. And C is not C89, but standard C, which is **only** C11. – too honest for this site Jul 27 '16 at 19:11
  • C and C++ are different languages. Don't compile C code as C++ and vice versa. If you want to use C, don't use MSVC, it is not standard compliant. It is stuck with the 27 year old outdated version C89/90. A modern C compiler like gcc or clang does support the standard (to use `bool`, you have to `#include ` accortding to the standard. – too honest for this site Jul 27 '16 at 19:14
  • @Olaf Visual Studio was used, given the error message. The `C` compiler that comes with Visual Studio is only C89 compliant. – PaulMcKenzie Jul 27 '16 at 19:50
  • @PaulMcKenzie: That's why it should not be used for C projects. Even MISRA noticed there was a new version of the standard 17 years ago. – too honest for this site Jul 27 '16 at 21:33

1 Answers1

3

bool is not a native C type, but for those using C99, try adding the line #include <stdbool.h>, which contains a macro that defines bool.

Since the C compiler in all Visual Studio/MSVC products uses C89, bool is not defined at all for you, as a native C type or otherwise. Workarounds include using typedef or enum to define bool. Examples are in the below link.

For more information, see: Is bool a native C type?

Community
  • 1
  • 1
Keith M
  • 853
  • 10
  • 28
  • 1
    The macro `bool` is not native, but it maps to the native type `_Bool` in that header by a macro (mandatory). That's because there is too much badly written C code in the wild which defines their own version of `bool` (you might be surprised how creative some ppl are how to define their own boolean type). So you can use `_Bool` as the native type (I don't recommend it though). – too honest for this site Jul 27 '16 at 19:16
  • @Keith My VS 2010 compiler supports C89/90, so I must look for another solution. – Stephen Wang Jul 27 '16 at 19:31
  • @StephenWang That's what I mean, you need the header for the macro Olaf explained to be visible. Did you try it? Compiling on ideone.com failed without the include and succeeded with it. – Keith M Jul 27 '16 at 21:44
  • @KeithM Correct, makes the _Bool macro visible. The _Bool built-in type is only defined for C99 - see [bool-data-type-of-c99-c](http://stackoverflow.com/questions/24602814/bool-data-type-of-c99-c). So for installations of VS 2010, neither is installed nor is _Bool a built-in type. – Stephen Wang Jul 28 '16 at 12:58
  • @StephenWang Ah, then if you think your question is not answered by the linked Duplicate, you should edit your question to explain why it is not a duplicate, and then request for it to be re-opened. :) – Keith M Jul 28 '16 at 21:18
  • @Keith It has - one of the later answers provided a workaround. What is the best way to handle this as OP? – Stephen Wang Jul 29 '16 at 04:22
  • @StephenWang I'm not sure I understand what you mean by "handle this as OP." – Keith M Jul 29 '16 at 22:26
  • @StephenWang Edited based on your rejected edit. Thanks! In the future, leave a comment or new answer for drastic changes. – Keith M Aug 01 '16 at 22:16