Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the format specifier for unsigned short int?

Tags:

c

scanf

People also ask

What is format specifier for short int in c?

Format specifiers define the type of data to be printed on standard output. You need to use format specifiers whether you're printing formatted output with printf() or accepting input with scanf() . Some of the % specifiers that you can use in ANSI C are as follows: Specifier.

What is %s in c?

%s is for string %d is for decimal (or int) %c is for character.

What does %lu mean c?

u is a specifier meaning "unsigned decimal integer". l is a length modifier meaning "long". The length modifier should go before the conversion specifier, which means %lu is correct.


Try using the "%h" modifier:

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

ISO/IEC 9899:201x - 7.21.6.1-7

Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to short or unsigned short.


For scanf, you need to use %hu since you're passing a pointer to an unsigned short. For printf, it's impossible to pass an unsigned short due to default promotions (it will be promoted to int or unsigned int depending on whether int has at least as many value bits as unsigned short or not) so %d or %u is fine. You're free to use %hu if you prefer, though.


From the Linux manual page:

h      A  following  integer conversion corresponds to a short int or unsigned short int argument, or a fol‐
       lowing n conversion corresponds to a pointer to a short int argument.

So to print an unsigned short integer, the format string should be "%hu".


Here is a good table for printf specifiers. So it should be %hu for unsigned short int.

And link to Wikipedia "C data types" too.