以前老以为,函数的声明与定义,样子必须一致.今天看了一个源代码,发现可以不这样.
比如:
在str.h中有如下代码
#ifndef STR_H #define STR_H int str_start(); #endif
str.c中
#include "str.h" int str_start(char *s, char *t)/* s t 相等返回 1, 否则返回0 */ { char x; for (;;) { x = *t++; if (!x) return 1; if (x != *s++) return 0; } }
在main.c 中
#include <stdio.h> #include "str.h" main() { printf("%d\n", str_start("tang", "tang")); }
这样函数调用没问题.函数的声明的定义的参数个数,可以不一样.
这个原因,应该是编译器对于函数编译时,函数名的规定.生成的函数名和形参没关系,所以C不支持重载.