2013-03-06 58 views
2

下面的代碼錯誤:「功能隱含聲明......」我的所有功能

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; 
} 

回答

4

您必須在使用它們之前聲明的功能:

int computeShort(); // declaration here 

int main() 
{ 
    computeShort(); 
} 

int computeShort() 
{ 
    // definition here 
} 

的替代,但不太可取方法是之前的主定義的功能,自定義作爲宣言的好:

int computeShort() 
{ 
    // return 4; 
} 

int main() 
{ 
    computeShort(); 
} 

但是,通常最好有一個單獨的函數聲明我們因爲那樣你就沒有義務在執行過程中保持一定的順序。

+0

我現在看到了,非常感謝你! – papercuts 2013-03-06 10:53:16

3

稱他們之前,必須聲明的功能。即使對於標準庫的某些部分也是如此。

例如,printf()由做聲明:

#include <stdio.h> 

自己的函數,或者:

  • 移動的定義,以上述main();定義也作爲聲明。
  • 調用之前添加原型,main()之前通常右:

    int computeShort();

另外,還要注意

  • 本地函數應該聲明static
  • 不接受任何參數的函數應該有一個參數列表(void),而不是()這意味着別的東西。