编译器是否隐式转换 int 类型

发布于 2024-11-07 03:37:09 字数 71 浏览 1 评论 0原文

如果一个值设置为 int 例如 2,编译器是否将 int 类型转换为它需要的大小,例如 int8_t 或 uint16_t 等?

If a value is set to an int e.g. 2, does the compiler convert the int types to the size it needs e.g. int8_t or uint16_t etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

╰沐子 2024-11-14 03:37:09

香草 C 中不存在,不。如果您不告诉编译器,它就不可能知道您的意思。

Not in vanilla C, no. The compiler can't possible know what you meant if you do not tell it.

清旖 2024-11-14 03:37:09

如果你写的

int value = 2;

话,默认类型是signed int。编译器的操作实际上取决于平台,但它必须保证 int 的大小不小于 short int 的大小且不大于 长整型'.s

If you write

int value = 2;

the type is, by default, signed int. What the compiler does then really depends from the platform, but it has to guarantee that int's size is not less than short int's and not greater than long int'.s

星星的軌跡 2024-11-14 03:37:09

对于常量来说,这可能是正确的,通常也会进行小到大的反向转换:例如字节到 int。

它在某种程度上取决于编译器使用的实现和优化技术以及体系结构/操作系统的数据对齐要求。看看这篇写的。

For constants it may true, often the reverse conversion small to large is also done: byte to int for example.

It's somewhat dependent on the implementation and optimization techniques used by the compiler and the data alignment requirements by the architecture/OS. Have a look at this write up.

傲世九天 2024-11-14 03:37:09

编译器首先查看表达式的上下文,了解它期望的类型。上下文可以是即

  • 赋值的左侧
  • 由函数头或运算符定义提供的预期参数类型

然后计算表达式,根据需要插入隐式类型转换(类型强制)。它进行

  • 提升
  • 截断
  • 舍入

在所有位都很重要的情况下,您需要非常小心所编写的内容:类型、运算符和顺序。

The compiler first looks at the context of the expression, learning about what type it expects. The context can be i.e.

  • Left hand side of an assignment
  • Expected argument type provided by the function header or operator definition

It then evaluates the expression, inserting implicit type conversions as needed (type coercion). It does

  • Promotion
  • Truncation
  • Rounding

In situations where all the bits matter, you need to be extremely carefull about what you write: Types, operators and order.

<逆流佳人身旁 2024-11-14 03:37:09

整数是“int”类型的值。当使用运算符“=”将整数值分配给short 或char 时,int 值将转换为short 或char。编译器可能会检测到此转换并进行优化以在编译时转换整数值。

short a = 50; //50 is an int, it will be implicitly converted to short. The compiler may convert 50 to short on compile time.
int b = 60;
short c = b; //the value of b will be converted to short and assigned to c.
short d = b + 70; //b + 70 is a sum of ints and the result will be an int that will be converted to short.

int8_t 和 uint16_t 不是标准类型。很多时候,这些类型可以定义为:

typedef char int8_t;
typedef unsigned short uint16_t;

Integer numbers are values of the type "int". When you assign an integer value to short or char using the operator "=", the int value will be converted to short or char. The compiler may detect this conversion and do optimization to convert the integer value on compile time.

short a = 50; //50 is an int, it will be implicitly converted to short. The compiler may convert 50 to short on compile time.
int b = 60;
short c = b; //the value of b will be converted to short and assigned to c.
short d = b + 70; //b + 70 is a sum of ints and the result will be an int that will be converted to short.

int8_t and uint16_t are not standard types. Many times these types may be defined as something like:

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