Sandeep Singh
Sandeep Singh

Reputation: 5191

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:

"conversion to non-scalar type requested"

I have also tried typecasting:

var2 = (U64bit) 100*var1;

This also gives the same error.

Upvotes: 0

Views: 2519

Answers (4)

Jens Gustedt
Jens Gustedt

Reputation: 79003

The standard fixed-width integer types in C are uint32_t and uint64_t, try with these. Then a constant of that type can be defined with UINT64_C(100).

To have these types you might have to add

#include <stdint.h>

to your includes.

Upvotes: 2

Prof. Falken
Prof. Falken

Reputation: 24937

Are you sure that this type, "U64bit" is actually defined as an integer? If not, that could be the problem, that it could be a struct which is a scalar type.

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.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60047

Try the following:

var2 = (U64bit)var1 * 100;

EDIT

Perhaps

var2 = (U64bit)var1 * 100LL;

Anyway the declaration for U32Bit, U64Bit and forfunction would be useful.

Upvotes: 0

unwind
unwind

Reputation: 400159

What is U64bit? That is a non-standard type, so you must show its declaration.

It sounds as if it's a struct.

Upvotes: 2

Related Questions