Declaration type

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Declaration type

876 Views
DavidoBG
Contributor II

I don't understand why theses following operations could be OK or NOK when i run program with simulator.

 

void main()

{

long number;

number = 1000*33; //The result is -32536

}

 

void main()

{

long number;

number = 1000*3.3*10; //The result is 33000

}

 

Do you have any explanations? In fact i would like do a big operation what king of type number declaration i will use?

Thanks a lot

david

Labels (1)
0 Kudos
Reply
1 Reply

355 Views
CrasyCat
Specialist III

Hello

 

According to the standard an ANSI C compiler will evaluate expression like  1000*33 on an int (16-bit) value and then result will be signed extended to long.

 

You need to tell the compiler the expression should be evaluated on 32-bit value.

In this purpose use the L prefix in one of the operant.

 

Your expression should be written 1000L * 33

 

This is compliant with ANSI C language definition.

 

CrasyCat

0 Kudos
Reply