当我在 C 中将 long int 分配给 int 时会发生什么? [英] What happens when I assign long int to int in C?

查看:23
本文介绍了当我在 C 中将 long int 分配给 int 时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的一次家庭作业中,我被告知使用 long 变量来存储结果,因为它可能是一个很大的数字.

In a recent homework assignment I've been told to use long variable to store a result, since it may be a big number.

我决定在我的系统(英特尔核心 i5/64 位 Windows 7/gnu gcc 编译器)上检查这对我来说真的很重要,并发现以下代码:

I decided to check will it really matter for me, on my system (intel core i5/64-bit windows 7/gnu gcc compiler) and found out that the following code:

printf("sizeof(char) => %d
", sizeof(char));
printf("sizeof(short) => %d
", sizeof(short));
printf("sizeof(short int) => %d
", sizeof(short int));
printf("sizeof(int) => %d
", sizeof(int));
printf("sizeof(long) => %d
", sizeof(long));
printf("sizeof(long int) => %d
", sizeof(long int));
printf("sizeof(long long) => %d
", sizeof(long long));
printf("sizeof(long long int) => %d
", sizeof(long long int));

产生以下输出:

sizeof(char) => 1
sizeof(short) => 2
sizeof(short int) => 2
sizeof(int) => 4
sizeof(long) => 4
sizeof(long int) => 4
sizeof(long long) => 8
sizeof(long long int) => 8

换句话说,在我的系统上,intlong 是相同的,并且对于 int 来说太大了,long 也太大了.

In other words, on my system, int and long are the same, and whatever will be too big for int to hold, will be too big for long to hold as well.

家庭作业本身不是这里的问题.我想知道如何在 int < 的系统上long,我应该为 long 分配一个 int 吗?

The homework assignment itself is not the issue here. I wonder how, on a system where int < long, should I assign an int to long?

我知道那里 无数密切 相关 关于这个主题的问题,但我觉得里面的答案这些并不能让我完全了解这个过程中将会发生或可能发生的事情.

I'm aware to the fact that there are numerous closely related questions on this subject, but I feel that the answers within these do not provide me with the complete understanding of what will or may happen in the process.

基本上我想弄清楚以下几点:

Basically I'm trying to figure out the following:

  1. 我应该在赋值之前将 long 转换为 int 还是 因为 long不是不同的数据类型,而只是一个修饰符, 它将是认为直接分配无害?
  2. long > 的系统上会发生什么?int?结果会是未定义(或不​​可预测),否则会导致要省略的变量?
  3. longint 的转换在 C 中是如何工作的?
  4. 当我不使用时,从 longint 的赋值如何在 C 中工作使用强制转换?
  1. Should I cast long to int before the assignment, or since long is not a different data type, but merely a modifier, it will be considered unharmful to assign directly?
  2. What happens on systems where long > int? Will the result be undefined (or unpredictable) or it will cause the extra parts of the variable to be omitted?
  3. How does the casting from long to int works in C?
  4. How does the assignment from long to int works in C when I don't use casting?

推荐答案

语言保证int至少为16位,long至少为32位,并且long 可以表示至少 int 可以表示的所有值.

The language guarantees that int is at least 16 bits, long is at least 32 bits, and long can represent at least all the values that int can represent.

如果您将 long 值分配给 int 对象,它将被隐式转换.不需要显式转换;它只会指定无论如何都会发生的相同转换.

If you assign a long value to an int object, it will be implicitly converted. There's no need for an explicit cast; it would merely specify the same conversion that's going to happen anyway.

在您的系统上,intlong 恰好具有相同的大小和范围,转换是微不足道的;它只是复制值.

On your system, where int and long happen to have the same size and range, the conversion is trivial; it simply copies the value.

longint 宽的系统上,如果该值不适合 int,则结果转换是实现定义的.(或者,从 C99 开始,它可以引发实现定义的信号,但我不知道有任何编译器实际这样做.)通常 发生的是高位被丢弃,但你不应该依赖它.(无符号类型的规则不同;将有符号或无符号整数转换为无符号类型的结果是明确定义的.)

On a system where long is wider than int, if the value won't fit in an int, then the result of the conversion is implementation-defined. (Or, starting in C99, it can raise an implementation-defined signal, but I don't know of any compilers that actually do that.) What typically happens is that the high-order bits are discarded, but you shouldn't depend on that. (The rules are different for unsigned types; the result of converting a signed or unsigned integer to an unsigned type is well defined.)

如果您需要安全地long 值分配给 int 对象,您可以在进行分配之前检查它是否适合:

If you need to safely assign a long value to an int object, you can check that it will fit before doing the assignment:

#include <limits.h> /* for INT_MIN, INT_MAX */

/* ... */

int i;
long li = /* whatever */

if (li >= INT_MIN && li <= INT_MAX) {
    i = li;
}
else {
    /* do something else? */
}

其他事情"的细节将取决于您想做什么.

The details of "something else" are going to depend on what you want to do.

更正:intlong 总是 不同的类型,即使它们碰巧具有相同的大小和表示.算术类型可以自由转换,所以这通常没有任何区别,但例如 int*long* 是不同且不兼容的类型;如果没有显式(且有潜在危险)的强制转换,您不能将 long* 分配给 int*,反之亦然.

One correction: int and long are always distinct types, even if they happen to have the same size and representation. Arithmetic types are freely convertible, so this often doesn't make any difference, but for example int* and long* are distinct and incompatible types; you can't assign a long* to an int*, or vice versa, without an explicit (and potentially dangerous) cast.

如果您发现自己需要将 long 值转换为 int,您应该做的第一件事就是重新考虑代码的设计.有时,此类转换是必要的,但更多时候,它们表明您所分配的 int 应该首先定义为 long.

And if you find yourself needing to convert a long value to int, the first thing you should do is reconsider your code's design. Sometimes such conversions are necessary, but more often they're a sign that the int to which you're assigning should have been defined as a long in the first place.

这篇关于当我在 C 中将 long int 分配给 int 时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆