定义默认 int 为 unsigned int

发布于 2024-12-24 16:38:05 字数 194 浏览 3 评论 0原文

如何(如果可以)将默认 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-12-31 16:38:05

您无法更改 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 the main function which must return type int (§5.1.2.2.1/1), if you change the default signedness somehow, then main will return unsigned int, and this will cause undefined behaviour, rendering your entire application relatively useless.

You can't create a macro, because if int expands to unsigned int, then if you have declared unsigned int somewhere, you will end up with unsigned unsigned int, which is not a valid type.

空袭的梦i 2024-12-31 16:38:05

超级邪恶

#define int unsigned int

可以工作,但绝对不符合标准,你可能需要让你的编译器不那么严格(这是一件坏事)。

您可以做的其他事情是查找并替换所有出现的 int (没有前面的 unsigned),并将其替换为 my_typedefed_int 并添加

typedef unsigned int my_typedefed_int

Which is符合标准,但涉及更多,并且可能不可能。

A super-evil

#define int unsigned int

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 preceding unsigned) and replace those with my_typedefed_int and add a

typedef unsigned int my_typedefed_int

Which is Standard compliant but is more involved and might not be possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文