#5909: 請問用short 為什麼會不行


bleeming71 (bleeming71)


我的程式如下

#include <stdio.h>

#include <stdlib.h>

int main()

{

    unsigned short n, i;

    unsigned threeK[3] = {0,0,0};

    unsigned num;

    scanf("%d", &n);

    for( i=0 ; i<n  ; i++ )

    {

        scanf("%d", &num);

        switch(num%3)

        {

            case 0:

                threeK[0]++;

                break;

            case 1:

                threeK[1]++;

                break;

            case 2:

                threeK[2]++;

                break;

        }

    }

    printf("%d %d %d", threeK[0], threeK[1], threeK[2]);

    return 0;

}

 我如果把 num 的宣告用 unsigned short 就會有問題,不知道為什麼,請問有人可以幫我解答嗎? 謝謝。

#13616: Re:請問用short 為什麼會不行


snakeneedy (蛇~Snake)


我的程式如下

#include

#include

int main()

{

    unsigned short n, i;

    unsigned threeK[3] = {0,0,0};

    unsigned num;

    scanf("%d", &n);

    for( i=0 ; i

    {

        scanf("%d", &num);

        switch(num%3)

        {

            case 0:

                threeK[0]++;

                break;

            case 1:

                threeK[1]++;

                break;

            case 2:

                threeK[2]++;

                break;

        }

    }

    printf("%d %d %d", threeK[0], threeK[1], threeK[2]);

    return 0;

}

 我如果把 num 的宣告用 unsigned short 就會有問題,不知道為什麼,請問有人可以幫我解答嗎? 謝謝。

建議是怎樣的型別,在 printfscanf 裡就要有對等的 %,如:

%d : int
%u : unsigned int
%hd: short int
%hu: unsigned short int