c - 错误 : "Implicit declaration of function..." on all my functions

标签 c

这是代码

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
}

另一种不太可取的方法是在 main 之前定义函数,因为定义也用作声明:

int computeShort()
{
    // return 4;
}

int main()
{
    computeShort();
}

但一般而言,更好的做法是为所使用的函数单独声明,因为这样您就没有义务在实现中保持一定的顺序。

关于c - 错误 : "Implicit declaration of function..." on all my functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245284/

相关文章:

c - 为什么我只写了一行来打印两行 "Enter their marks"?

ios - 如何将 C int 放入 NSArray?

c - ELF Parser 分离程序数据

检查一个数字是否暗淡或以有效的方式得到支持

c - 指向共享内存中数组的共享指针,指针似乎不共享?

android - 当传递给 JNI 层下的 C 函数时,指针的地址发生变化

c - 如何检查C中字符串中的重复单词?

php - 类似于 PHP 中的 in_array 的 C 函数

c - 数组在循环中如何工作?

字符指针在附加字符串字符时添加随机值