Parts of the labs require you to write C code for the Clarvi CPU. The similarity of syntax
between C and Java means you should have little difficulty in using the C language for the given tasks.
The following outlines a few facts about the standard C language. A more thorough overview is given
in the lecture course Programming in C and C++.
Note that, unlike Java, there are many popular C compilers and each has its quirks; your code should not
depend on your choice of compiler. If in doubt, refer to the official documentation.
The labs direct you to use the GCC compiler which produces machine code for the RISC-V architecture.
Overview
C is an imperative, monolithic programming language; a C program may span multiple (specified) files and is logically a
set of procedures and constants. Before a program is compiled (in the usual sense), it is passed through the C preprocessor:
preprocessor macros are separate from the main language, and result in textual manipulation of the source file:
#include ...
Substitutes the specified source file. Files from the standard library are specified as <name>; other
files are specified as a relative or absolute path in quotes.
#define A B
Causes all instances of A in the source text to be substituted with B.
#define A(x) (x+1)
Causes all instances of A(someparameter) in the source text to be substituted with
(someparameter+1).
A source file in C has the extension .c. Header files (.h) are often used to separate preprocessor
macros from the source code.
Gotchas
In general, you should assume language constructs behave as for Java. The following outline some important differences.
'for' loops
You must use a previously declared variable to loop over: i.e. int i; for( i=0; ... instead of
for( int i=0; ...
types
The C basic types are char, long, int, float and double. There is no
boolean type: 'true' is taken as any non-zero value, and conversely for 'false'. Type modifiers, such as unsigned
can be preprended to a type declaration. Types are signed by
default.
variable initialisation
Global variables (outside of a procedure) are initialised to zero by default. Local variables are uninitialised -
they could be any value of the declared type.
Bare metal restrictions
Unlike most C programming, on Clarvi we are running bare metal, without an operating system or any runtime library. This means that standard functions like printf, strlen or malloc are not present. Therefore any of these we need we have to write ourselves.