Omitting the datatype (e.g. “unsigned” instead of “unsigned int”)

后端 未结 4 2048
猫巷女王i
猫巷女王i 2021-01-31 08:01

I know that if the data type declaration is omitted in C/C++ code in such way: unsigned test=5;, the compiler automatically makes this variable an int (an unsigned

4条回答
  •  悲&欢浪女
    2021-01-31 09:00

    unsigned is a data type! And it happens to alias to unsigned int.

    When you’re writing unsigned x; you are not omitting any data type.

    This is completely different from “default int” which exists in C (but not in C++!) where you really omit the type on a declaration and C automatically infers that type to be int.

    As for style, I personally prefer to be explicit and thus to write unsigned int. On the other hand, I’m currently involved in a library where it’s convention to just write unsigned, so I do that instead.

提交回复
热议问题