C ++从int转换为long int奇怪的行为 [英] C++ Convert from int to long int strange act

查看:116
本文介绍了C ++从int转换为long int奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义类型称为单元格,当我尝试从int转换为单元格时,我得到了这个奇怪的结果:

  typedef   long   int  int32_t; 
typedef int32_t cell;

cell foo()
{
int xz;
xz = 150 ;
return xz;
}





预计:150



输出:32736 < - 为什么???而不是预期的150.



我希望你能理解这一点。

解决方案

不,它应该返回150(除非您还以某种奇怪的方式重新定义 int long int )。



(顺便说一下,你有没有得到这个输出?你没有显示调用 foo()的代码,问题可能在那里...)



-SA


以下代码



 typedef long int int32_t; 
typedef int32_t cell;
#include< stdio.h>
cell foo()
{
int xz;
xz = 150;
返回xz;
}

int main()
{
printf(%ld\\\
,(long)foo()); / * long int! * /
返回0;
}





输出:

150


I have custom type called cell and when i try to convert from int to cell i got this strange results:

typedef long int  int32_t;
typedef int32_t   cell;

cell foo()
{
 int xz;
 xz = 150;
 return xz;
}



Expected: 150

Output: 32736 <- why ??? and not 150 as expected.

I hope you will understand this.

解决方案

No, it should return 150 (unless you also redefined int and long int in some weird way :-)).

(By the way, hot did you obtain this output? You did not show the code calling foo(), the problem could be there…)

—SA


The following code

typedef long int  int32_t;
typedef int32_t   cell;
#include <stdio.h>
cell foo()
{
 int xz;
 xz = 150;
 return xz;
}

int main()
{
  printf("%ld\n", (long)foo());  /* long int ! */
  return 0;
}



outputs:
150


这篇关于C ++从int转换为long int奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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