当我分配长整型在C诠释,会发生什么? [英] What happens when I assign long int to int in C?

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

问题描述

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

我决定检查会真的重要,对我来说,我的系统上(英特尔酷睿i5 / 64位的Windows 7 / GNU GCC编译器),并发现以下code:

 的printf(的sizeof(char)的= GT;%d个\\ N的sizeof(字符));
的printf(的sizeof(短)=>%d个\\ N的sizeof(短));
的printf(的sizeof(短整型)=>%d个\\ N的sizeof(短整型));
的printf(的sizeof(int)的= GT;%d个\\ N的sizeof(INT));
的printf(的sizeof(长)=>%d个\\ N的sizeof(长));
的printf(的sizeof(长整型)=>%d个\\ N的sizeof(长整型));
的printf(的sizeof(久长)=>%d个\\ N的sizeof(久长));
的printf(的sizeof(得到long long int)=>%d个\\ N的sizeof(得到long long int));

产生下面的输出:

 的sizeof(char)的= GT; 1
的sizeof(短)=> 2
的sizeof(短整型)=> 2
的sizeof(INT)=> 4
的sizeof(长)=> 4
的sizeof(长整型)=> 4
的sizeof(久长)=> 8
的sizeof(得到long long int)=> 8

在换句话说,我的系统上, INT 都是一样的,不管会太大为 INT 持有,将是太大持有为好。

的家庭作业本身并不是这里的问题。我想知道,在系统上,其中 INT<长,我应该指定一个 INT 长?

我知道这样的事实, <一个href=\"http://stackoverflow.com/questions/1161887/what-happens-if-i-cast-a-double-to-an-int-but-the-value-of-the-double-is-out-of\">numerous <一href=\"http://stackoverflow.com/questions/2711522/what-happens-if-i-assign-a-negative-value-to-an-unsigned-variable\">closely <一href=\"http://stackoverflow.com/questions/7977265/what-happens-at-background-when-convert-int-to-float\">related关于这个问题的问题,但我觉得在这些问题的答案不为我提供什么将会或者可能会在这个过程中发生了全面的了解。

基本上我试图找出以下内容:


  1. 我应该投 INT 的转让,或,因为前长
    是不是一个不同的数据类型,而仅仅是一个调节剂,这将是
    考虑到无害直接分配?

  2. 在系统中会发生什么情况,其中长&GT; INT ?将结果是
    未定义(或未predictable),或将造成的多余的部分
    被忽略的变量?

  3. 如何从 INT 工作在C那样被铸造?

  4. 如何从不分配长 INT 工作在C当我不
    用铸造?


解决方案

语言保证 INT 至少为16位,至少是32位,而可以重新present的至少的所有的 INT 可以重新present。

如果您指定一个值到 INT 对象时,它会被隐式转换。有没有必要明确地投;它只会指定不管怎样都是发生相同的转换。

在你的系统,其中 INT 碰巧有相同的大小和范围,转换是微不足道;它只是复制的价值。

在一个系统,其中 INT 更宽,如果该值不适合在 INT ,那么转换的结果是实现定义的。 (或者,在C99开始,它可以引发实现定义的信号,但我不知道,其实做任何编译器。)什么一般的情况是,高序位都被放弃,但你不应该依赖于这一点。 (该规则是无符号的类型不同,有符号或无符号整数转换为无符号类型定义良好的结果。)

如果你需要的安全的分配值到 INT 对象,你可以检查它适合做转让前:

 的#include&LT;&limits.h中GT; / *为INT_MIN,INT_MAX * // * ... * /INT I;
龙力= / * *什么/如果(LI&GT; = INT_MIN和放大器;&安培;李&LT; = INT_MAX){
    I =立;
}
其他{
    / *做别的事情? * /
}

的别的东西的细节将取决于你想要做什么。

一个校正: INT 总是的不同的类型,即使他们碰巧具有相同的尺寸,并重新presentation。算术类型是可自由兑换,所以这往往没有任何区别,但例如为int * 长* 是不同的和不兼容的类型;你不能分配长* 为int * ,反之亦然,没有一个明确的(和潜在的危险)铸。

如果你发现自己需要一个值转换为 INT ,你应该做的第一件事就是重新考虑你的code的设计。有时候,这种转换是必要的,但更多的时候他们的 INT 来你分配应该被定义为一个标志摆在首位。

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

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\n", sizeof(char));
printf("sizeof(short) => %d\n", sizeof(short));
printf("sizeof(short int) => %d\n", sizeof(short int));
printf("sizeof(int) => %d\n", sizeof(int));
printf("sizeof(long) => %d\n", sizeof(long));
printf("sizeof(long int) => %d\n", sizeof(long int));
printf("sizeof(long long) => %d\n", sizeof(long long));
printf("sizeof(long long int) => %d\n", sizeof(long long int));

produces the following output:

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

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.

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. 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?

解决方案

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.

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.

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

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.)

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.

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.

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诠释,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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