warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
Asked Answered
A

0

1

I have a package with C code and a call to int checkInterrupt(); but devtools::check() says warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes].

How can I call the check interrupt function to pass that warning? I was thinking about checkInterrupt(void *dummy) but that doesn't work.

Astrakhan answered 20/1, 2023 at 20:54 Comment(10)
Who defines checkInterrupt? You, or a third-party library, or your embedded OS, or what? Theoretically, you should introduce a proper prototype for it by #including the appropriate header file, not by hand-coding an external function declaration in each/every .c file where you use it. (That's almost as error-prone as not using prototypes at all.)Domineca
Otherwise, you need to figure out what argument types checkInterrupt actually takes, and add those in the (). Don't just guess — that's much worse than not using prototypes at all! (And, by the way, void * does not mean "anything".)Domineca
It is a part of R's C API (i.e., #include <R.h>), and it's a function with no argumentsAstrakhan
int checkInterrupt(); is not a function call — it is a declaration of a function returning an int but without a defined argument list (but it isn't a variadic function like printf() — its prototype would not end with , ...). And if you compile with -Wstrict-prototypes, such declarations are not allowed precisely because they don't define a prototype for the function. If the function takes no arguments, say so: extern int checkInterrupt(void);. And define it as int checkInterrupt(void) { … } too! (GCC options: -Wold-style-definition and -Wold-style-declaration).Hauck
Note that these rules will change with C23 — it will follow the C++ rules so that a declaration with an empty parameter list will be the same as one with (void) as the parameter list, and the declaration will provide a prototype for the function.Hauck
@Astrakhan In that case, if you have #include <R.h>, you don't need the line int checkInterrupt(); at all, and you can delete it, and that should make the warning go away.Domineca
To actually call the function, rather than declare it, simply use checkInterrupt(); (no type before it). That calls the function with no arguments. As long as it has been declared before it is called, all will be well. Maybe you should check the returned value, though — it probably has some significance and maybe your code should respond to that.Hauck
@JonathanLeffler does that mean that main will become a dynamic function, in that it changes at run-time based on whether it is declared main() or main(int argc, char **argv)?Oke
@Oke — I'm not sure what you mean by a 'dynamic function'. The standard says that a hosted implementation (the normal kind) does not provide a prototype for main() and must support both int main(void) and int main(int argc, char **argv) (or equivalents) and may support other implementation-defined signatures. One such variation is listed in Annex J.3 (Common extensions). See also What should main() return in C and C++?Hauck
All I meant is that the prototype for it changes with compilation. The reason I thought this wouldn't work in C23 is because I thought main() means an undefined number of arguments, so its compatible with the one where you put arguments in. I guess I don't really understand why () means undefined number of arguments in first place and what the use case for it is, since I had thought that the main() function was the primary use-case.Oke

© 2022 - 2025 — McMap. All rights reserved.