1

While calling the printf function we were passing the length modifier for the short as h?

For Eg

short s=10;
printf("%hd",s);

What does the h means? Is there is any specific reason to give the name h?

Motive behind asking this question is standard has choosen the l for long. Since long starts with l it is okay.

Likwise long what does h represents?

VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38
  • 5
    Half maybe? What other letter would you use since `s` means something else? – Retired Ninja Sep 13 '14 at 10:40
  • @RetiredNinja I aggree – VINOTH ENERGETIC Sep 13 '14 at 10:42
  • 2
    there's no need to use `%hd` or `%hhd` in printf since all types less than int must follow defaut promotion rule in var_arg functions, hence it will be promoted to int anyway http://en.cppreference.com/w/cpp/io/c/fprintf – phuclv Sep 13 '14 at 11:02
  • http://stackoverflow.com/questions/4586962/what-is-the-purpose-of-the-h-and-hh-modifiers-for-printf – phuclv Sep 13 '14 at 11:06
  • 2
    @Luu the standard says that passing `char` for `%d` causes undefined behaviour (although common sense dictates that we should assume the phrase "after the default argument promotions are applied" occurs where in fact it doesn't) – M.M Sep 13 '14 at 11:12
  • Read the documentation of [printf(3)](http://man7.org/linux/man-pages/man3/printf.3.html). That is faster than asking here. – Basile Starynkevitch Sep 13 '14 at 11:38

1 Answers1

1

Yes, the usual understanding is that 'h' signifies 'half' or 'half word', in the same sense that 'l' signifies 'long' or 'double word'. It actually means 'short' and of course 's' was already taken.

It is useful in printf because it ensures that the value will be cast back to a short before being printed. In this case, '%hd' is not as useful as '%hu' for unsigned short int.

It is also useful in scanf, to ensure that only the right number of bits are stored into a short variable. But see What is the purpose of the h and hh modifiers for printf?.

Community
  • 1
  • 1
david.pfx
  • 10,520
  • 3
  • 30
  • 63