省略数据类型(例如"unsigned"而不是"unsigned int") [英] Omitting the datatype (e.g. "unsigned" instead of "unsigned int")
问题描述
我知道,如果在C/C ++代码中以以下方式省略数据类型声明: unsigned test = 5;
,则编译器会自动将此变量设为int(在这种情况下为unsigned int).我听说这是C标准,它将在所有编译器中都可以使用.
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 int in this case). I've heard that it's a C standard and it will work in all compilers.
但是但是我也听说这样做是不正确的.
But I've also heard that doing this is considered a bad practice.
您怎么看?我应该真的输入 unsigned int
而不是仅仅输入 unsigned
吗?
What do you think? Should I really type unsigned int
instead of just unsigned
?
short
, long
和 long long
也是数据类型吗?
Are short
, long
and long long
also datatypes?
推荐答案
unsigned
是数据类型!碰巧是 unsigned int
的别名.
在编写 unsigned x;
时,不省略任何数据类型.
When you’re writing unsigned x;
you are not omitting any data type.
这与C中存在的默认 int
"(但在C ++中没有)完全不同,在C中您实际上忽略了声明的类型,C会自动推断该类型为int
.
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
.
关于样式,我个人更喜欢露骨,因此要编写 unsigned int
.另一方面,我目前正在图书馆中,习惯上只写 unsigned
,所以我这样做.
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.
这篇关于省略数据类型(例如"unsigned"而不是"unsigned int")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!