定义默认 int 为 unsigned int
如何(如果可以)将默认 int 变量(在特定程序中)设置为 unsigned int?
我的意思是,如果int
写入程序中,编译器会将其视为unsigned int
。
我的编译器是 gcc 4.6.2
编辑:我无权触摸代码。
How can I (if I can) set the default int variable (in a specific program) to be unsigned int?
I mean that if int
is written in the program, the compiler treats it like unsigned int
.
My compiler is gcc 4.6.2
EDIT: I am not authorized to touch the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法更改
int
的默认符号性,因为根据定义它是有符号类型 (§6.2.5/4)。考虑main
函数,它必须返回类型int
(§5.1.2.2.1/1),如果您以某种方式更改默认符号,则main
将返回unsigned int
,这将导致未定义的行为,使您的整个应用程序相对无用。您无法创建宏,因为如果
int
扩展为unsigned int
,那么如果您在某处声明了unsigned int
,您最终会使用unsigned unsigned int
,这不是有效类型。You can't change the default signedness of
int
because it is by definition a signed type (§6.2.5/4). Consider themain
function which must return typeint
(§5.1.2.2.1/1), if you change the default signedness somehow, thenmain
will returnunsigned int
, and this will cause undefined behaviour, rendering your entire application relatively useless.You can't create a macro, because if
int
expands tounsigned int
, then if you have declaredunsigned int
somewhere, you will end up withunsigned unsigned int
, which is not a valid type.超级邪恶
可以工作,但绝对不符合标准,你可能需要让你的编译器不那么严格(这是一件坏事)。
您可以做的其他事情是查找并替换所有出现的
int
(没有前面的unsigned
),并将其替换为my_typedefed_int
并添加Which is符合标准,但涉及更多,并且可能不可能。
A super-evil
could work, but is definitely not Standard compliant and you might need to make your compiler be less strict (which is a bad thing).
Other things you can do is find and replace all occurrences of
int
(without a precedingunsigned
) and replace those withmy_typedefed_int
and add aWhich is Standard compliant but is more involved and might not be possible.