引用某些权限,MISRA-C:2004规则8.1强制执行具有外部链接的函数的原型,但也提到了内部链接:
"为具有内部联系的功能提供原型是良好的编程实践."
我认为这是一种很好的做法,因为它使得内部/外部链接功能之间的编码风格保持一致.正如其他人在答案中提到的那样,它也很方便.
例如,如果您需要确保函数具有某种类型,那么它可能很有用.
考虑一下:
// Define how the functions should be defined.
typedef void * jobfunc(void *);
// Use case
void addjob(jobfunc *);
// Ensure they have the right type. Without this, you only get a warning
// for addjob(job2) (see below) - with it, you get an error for the mismatch.
static jobfunc job1;
static jobfunc job2;
// Define one job
static void * job1(void * arg) {
return NULL;
}
// Define another job - accidentally wrong.
static void * job2(int accidentally_wrong) {
return NULL;
}
Run Code Online (Sandbox Code Playgroud)