How to portably convert a string into an uncommon integer type?

前端 未结 3 865
臣服心动
臣服心动 2020-12-30 07:46

Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t, I’d use SCNu1

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 08:34

    If you are really concerned you can _assert(sizeof(pid_t) <= long) or whatever type you choose for your '%' stuff.

    As explained in this answer, the spec says signed int. If 'int' changes, your '%u' by definition changes with it.

提交回复
热议问题