[英]How To Convert a 32-bit Integer into a 64 bit value In C
I am getting compilation error in a Project Code where the situation is as follows: 我在情况如下的项目代码中遇到编译错误:
typedef unsigned int U32bit;
typedef unsigned long long U64bit;
U32bit var;
U64bit var2;
var = function(); /* Function returns a 32-bit value, which is stored in var */
var2 = 100*var1; /* 100*var1 is very Big & can be stored only in U64bit variable */
For the Above Line: var2 = 100*var1
I am getting the following Compilation Error on Solaris: 我在Solaris上收到以下编译错误:
"conversion to non-scalar type requested"
I have also tried typecasting: 我也尝试过类型转换:
var2 = (U64bit) 100*var1;
This also gives the same error. 这也给出了相同的错误。
What is U64bit
? 什么是
U64bit
? That is a non-standard type, so you must show its declaration. 这是非标准类型,因此您必须显示其声明。
It sounds as if it's a struct
. 听起来好像是一个
struct
。
The standard fixed-width integer types in C are uint32_t
and uint64_t
, try with these. C语言中标准的固定宽度整数类型为
uint32_t
和uint64_t
,请尝试使用这些类型。 Then a constant of that type can be defined with UINT64_C(100)
. 然后可以使用
UINT64_C(100)
定义该类型的常量。
To have these types you might have to add 要拥有这些类型,您可能需要添加
#include <stdint.h>
to your includes. 包括在内
Are you sure that this type, "U64bit" is actually defined as an integer? 您确定这种“ U64bit”类型实际上定义为整数吗? If not, that could be the problem, that it could be a
struct
which is a scalar type. 如果不是,那可能是问题所在,它可能是标量类型的
struct
。
You also don't specify which compiler or OS you are using. 您也没有指定要使用的编译器或操作系统。 If it's anything following the C standard, you should
#include <stdint.h>
and use uint32_t
and uint64_t
instead of your U32bit
and U64bit
. 如果遵循C标准,则应
#include <stdint.h>
并使用uint32_t
和uint64_t
而不是U32bit
和U64bit
。
Try the following: 请尝试以下操作:
var2 = (U64bit)var1 * 100;
EDIT 编辑
Perhaps 也许
var2 = (U64bit)var1 * 100LL;
Anyway the declaration for U32Bit
, U64Bi
t and for function
would be useful. 无论如何,对于
U32Bit
, U64Bi
t和function
的声明都是有用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.