Difference between revisions of "Literal expression"

From MSX Game Library

 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
  
 
For example:
 
For example:
u8 a = (10 * 5) - 12; // 38
+
<syntaxhighlight lang="c">
i16 b = 8 * 1024 / 3; // 2730
+
u8 a = (10 * 5) - 12; // 38
i8 c = 3.141593 * -10; // -31 (generate warning)
+
i16 b = 8 * 1024 / 3; // 2730
 +
i8 c = 3.141593 * -10; // -31 (generate warning)
 +
</syntaxhighlight>
  
 
To help the preprocessor to determine the type of numbers you can either:
 
To help the preprocessor to determine the type of numbers you can either:
 
* Cast the number or/and the result to the type of your choice,
 
* Cast the number or/and the result to the type of your choice,
u8 d = (u8)(255-1);
+
<syntaxhighlight lang="c">
i16 e = (i16)(66000/3);
+
u8 d = (u8)(255-1);
 +
i16 e = (i16)(66000/3);
 +
</syntaxhighlight>
 
* Use C11 postfix U, L or LL.
 
* Use C11 postfix U, L or LL.
U: Unsigned integer
+
<syntaxhighlight lang="txt">
L: 32-bits integer
+
U: Unsigned integer
LL: 64-bits integer
+
L: 32-bits integer
 +
LL: 64-bits integer
 +
</syntaxhighlight>

Latest revision as of 21:25, 13 January 2024

MSXGL is using sdcc11 option for compile C code that is described as « Generally follow the ISO C11 standard, but allow some SDCC behaviour that conflicts with the standard ».

For literal expressions, the C11 standard applies. These expressions are interpreted during the preprocessing phase and are replaced by their final value for compilation.

For example:

u8 a = (10 * 5) - 12;  // 38
i16 b = 8 * 1024 / 3;  // 2730
i8 c = 3.141593 * -10; // -31 (generate warning)

To help the preprocessor to determine the type of numbers you can either:

  • Cast the number or/and the result to the type of your choice,
u8 d = (u8)(255-1);
i16 e = (i16)(66000/3);
  • Use C11 postfix U, L or LL.
U: Unsigned integer
L: 32-bits integer
LL: 64-bits integer