unsigned short int のフォーマット指定子は何ですか? 質問する

unsigned short int のフォーマット指定子は何ですか? 質問する

私は次のプログラムを持っています

#include <stdio.h>

int main(void)
{
    unsigned short int length = 10; 

    printf("Enter length : ");
    scanf("%u", &length);

    printf("value is %u \n", length);

    return 0;
}

これをコンパイルすると、gcc filename.c次の警告が発行されます (行内scanf())。

warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 2 has type ‘short unsigned int *’ [-Wformat]

次に を参照しましたが、データ型に長さ修飾子 ( 、など)C99 specification - 7.19.6 Formatted input/output functionsを使用する場合の正しい書式指定子を理解できませんでした。shortlongunsignedint

%u指定子は正しいですかunsigned short int? もしそうなら、なぜ上記の警告が表示されるのですか?

編集: ほとんどの場合、試してみました%uhが、それでも警告が表示されました。

ベストアンサー1

次の修飾子を使用してみてください"%h":

scanf("%hu", &length);
        ^

ISO/IEC 9899:201x - 7.21.6.1-7

後続の d 、 i 、 o 、 u 、 x 、 X 、または n 変換指定子が型の引数に適用されることを指定します。short または unsigned short へのポインタ

おすすめ記事