When to use short instead of int?

前端 未结 5 1654
梦如初夏
梦如初夏 2021-02-08 11:46

Two use cases for which I would consider short come to mind:

  1. I want an integer type that\'s at least 16 bits in size
  2. I want an integer type t
5条回答
  •  余生分开走
    2021-02-08 12:20

    There is never a reason to use short in a C99 environment that has 16-bit integers; you can use int16_t, int_fast16_t or int_least16_t instead.

    The main reasons for using short is backward compatibility with C89 or older environments, which do not offer these types, or with libraries using short as part of their public API, for implementing itself, or for compatibility with platforms that do not have 16-bit integers so their C compilers do not provide int16_t.

提交回复
热议问题