错误:“功能隐含声明......”我的所有功能
问题描述:
main()
{
short sMax = SHRT_MAX;
int iMax = INT_MAX;
long lMax = LONG_MAX;
// Printing min and max values for types short, int and long using constants
printf("range of short int: %i ... %i\n", SHRT_MIN, SHRT_MAX);
printf("range of int: %d ... %d\n", INT_MIN, INT_MAX);
printf("range of long int: %ld ... %ld\n", LONG_MIN, LONG_MAX);
// Computing and printing the same values using knowledge of binary numbers
// Short
int computed_sMax = computeShort()/2;
printf("\n Computed max and min short values: \n %i ... ", computed_sMax);
int computed_sMin = (computeShort()/2 + 1) * -1;
printf("%i\n", computed_sMin);
//Int
int computed_iMax = computeInt()/2;
printf("\n Computed min and max int values: \n %i ... ", computed_iMax);
int computed_iMin = computeInt()/2;
printf("%i", computed_iMin);
return 0;
}
int computeShort()
{
int myShort = 0;
int min = 0;
int max = 16;
for (int i = min; i < max; i++)
{
myShort = myShort + pow(2, i);
}
return myShort;
}
int computeInt()
{
int myInt = 0;
int min = 0;
int max = 32;
for (int i = min; i < max; i++)
{
myInt = myInt + pow(2, i);
}
return myInt;
}
答
您必须在使用它们之前声明的功能:
int computeShort(); // declaration here
int main()
{
computeShort();
}
int computeShort()
{
// definition here
}
的替代,但不太可取方法是之前的主定义的功能,自定义作为宣言的好:
int computeShort()
{
// return 4;
}
int main()
{
computeShort();
}
但是,通常最好有一个单独的函数声明我们因为那样你就没有义务在执行过程中保持一定的顺序。
答
称他们之前,必须声明的功能。即使对于标准库的某些部分也是如此。
例如,printf()
由做声明:
#include <stdio.h>
自己的函数,或者:
- 移动的定义,以上述
main()
;定义也作为声明。 -
调用之前添加原型,
main()
之前通常右:int computeShort();
另外,还要注意
- 本地函数应该声明
static
。 - 不接受任何参数的函数应该有一个参数列表
(void)
,而不是()
这意味着别的东西。
我现在看到了,非常感谢你! – papercuts 2013-03-06 10:53:16