3

これがコードです

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

2 に答える 2

6

関数を使用する前に、関数を宣言する必要があります。

int computeShort(); // declaration here

int main()
{
    computeShort();
}

int computeShort()
{
    // definition here
}

別の、しかしあまりお勧めできないアプローチは、定義が宣言としても機能するため、mainの前に関数を定義することです。

int computeShort()
{
    // return 4;
}

int main()
{
    computeShort();
}

ただし、一般的には、使用する関数に対して個別の宣言を行うことをお勧めします。そうすると、実装で特定の順序を維持する必要がなくなるためです。

于 2013-03-06T10:52:55.603 に答える
3

関数を呼び出す前に、関数を宣言する必要があります。これは、標準ライブラリの一部にも当てはまります。

たとえば、 次のようにprintf()して宣言します。

#include <stdio.h>

独自の機能については、次のいずれかを行います。

  • 定義を main()に移動します; 定義は宣言としても機能します。
  • 呼び出しの前、通常は直前にプロトタイプを追加しますmain()

    int computeShort();

また、注意してください

  • ローカル関数を宣言する必要がありますstatic
  • 引数を受け入れない関数には、他の意味で(void)はなく、の引数リストが必要()です。
于 2013-03-06T10:51:58.047 に答える