这是代码
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;
}
答案 0 :(得分:5)
您必须在使用之前声明函数:
int computeShort(); // declaration here
int main()
{
computeShort();
}
int computeShort()
{
// definition here
}
另一种方法,但不太明智的方法是在main之前定义函数,因为定义也用作声明:
int computeShort()
{
// return 4;
}
int main()
{
computeShort();
}
但通常最好对所使用的函数单独声明,因为那样你就没有义务在实现中保持某个顺序。
答案 1 :(得分:3)
您必须在调用函数之前声明函数。即使对于标准库的某些部分也是如此。
例如,printf()
通过执行以下方式声明:
#include <stdio.h>
对于您自己的职能,可以:
main()
;这些定义也可以作为声明。在通话之前添加原型,通常在main()
:
int computeShort();
另外,请注意
static
。(void)
的参数列表,而不是()
,这意味着其他的。