ECAD and Architecture Practical Classes
Introduction to C
Parts of the labs require you to write C code using the Eclipse IDE. 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 compiler available in Eclipse, which produces machine code for the NIOS II processor.
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.
A source file in C has the extension .c. Header files (.h) are often used to separate preprocessor macros from the source code.
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; ...
- The C basic types are char (unsigned), 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.
- 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.