Declaration type

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Declaration type

1,205件の閲覧回数
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

ラベル(1)
0 件の賞賛
返信
1 返信

684件の閲覧回数
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 件の賞賛
返信