Convert code to the C99 standard
Opened this issue · 1 comments
Background info
Currently the XC16 compiler in MPLAB X standard uses the C89 standard. However it is possible with the -std=c99
option to use the C99 standard. Although this is still quite an old standard is has some 'new' features that could come in handy.
Step 1
Make/find a list of the new features. This will result in a list of features that should be changed in the code.
for (int i = 0; i < 99; ++i) { ... }
instead of having the declaration ofi
outside the for loop;- ???
Step 2
Update the code in the files below to the 'new' standard.
- Animations/Fill.h
- Animations/src/Fill.c
- CubeControl/Colours.h
- CubeControl/CubeControlData.h
- CubeControl/LayerControl.h
- CubeControl/LedCube.h
- CubeControl/LedDriver.h
- CubeControl/PanelControl.h
- CubeControl/src/CubeControlData.c
- CubeControl/src/LayerControl.c
- CubeControl/src/LedCube.c
- CubeControl/src/LedDriver.c
- CubeControl/src/PanelControl.c
- LibraryFiles/BitOperations.h
- LibraryFiles/Buzzer.h
- LibraryFiles/Debug.h
- LibraryFiles/Delay.h
- getPICConfigurationBits.h
- LibraryFiles/Interrupts.h
- LibraryFiles/IOPorts.h
- LibraryFiles/Macro.h
- LibraryFiles/MyAssert.h
- LibraryFiles/PeripheralPinSelect.h
- LibraryFiles/Ports.h
- LibraryFiles/SwFifoBuffer.h
- LibraryFiles/System.h
- LibraryFiles/Uart.h
- LibraryFiles/src/BitOperations.c
- LibraryFiles/src/Buzzer.c
- LibraryFiles/src/getPICConfigurationBits.c
- LibraryFiles/src/IOPorts.c
- LibraryFiles/src/SwFifoBuffer.c
- LibraryFiles/src/System.c
- LibraryFiles/src/Uart.c
- src/main.c
- Visualisation/LedCubeVisualisation.h
- Visualisation/src/LedCubeVisualisation.c
- version.h
Step 3
Informative announcement:
Make clear, in the wiki or somewhere else, that the C99 standard is used for programming.
The XC16 compiler in MPLAB X uses a weird kind of c standard. When not adding the -std=c99
it is surely using the C89 standard according to the __STDC__
and __STDC_VERSION__
defines. But however in C89 it is not allowed to comment a line of code using '//', which is possible using the XC16 compiler. And for example vars can be declared anywhere in a block which should not be possible using the standard C89.
So far I could only come up with the already stated difference about the int in a for loop.
Source: http://std.dkuug.dk/JTC1/SC22/WG14/www/C99RationaleV5.10.pdf
Up-to-date list of features to change:
for (int i = 0; i < 99; ++i) { ... }
instead of having the declaration ofi
outside the for loop;- Usage of
__VA_ARGS__
in macro expanstions (p. 101); %hh
and%ll
in printf functions are allowed (p. 152);snprintf
new function (p. 154);
#ifdef DEBUG
#define dfprintf(stream, ...) \
fprintf(stream, "DEBUG: " _ _VA_ARGS_ _)
#else
#define dfprintf(stream, ...) ((stream, _ _VA_ARGS_ _, 0))
#endif
#define dprintf(...) dfprintf(stderr, _ _VA_ARGS_ _)
For example,
dprintf("X = %d\n", x);
expands to
dfprintf(stderr, "X = %d\n", x);
and thus to one of
Language
102
fprintf(stderr, "DEBUG: " "X = %d\n", x);
or
((stderr, "X = %d\n", x, 0));
Progress
Found that libpic30.h is not compatible with the C99 standard
c:\program files (x86)\microchip\xc16\v1.25\bin\bin\../..\support\generic\h/libpic30.h:221:34: error: expected ')' before 'void'
And the asm
instruction is not supported as it seems.
Edit:
Those instructions are supported using -std=gnu99
instead of c99.