Introduction to C programming pt 2 Lecture 2
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Computing for Engineers
Lecture 2
Introduction to C programming pt 2
1. Assessment
This module is assessed by
Coursework, 50%
Canvas Exam, 50%.
These will now be detailed further.
1.1. Coursework information
The labs will take place during most weeks in Semester 1. Check the timetable on Canvas for the latest schedule:
Week 2 |
Lab 1 |
50 word log summary |
Week 3 |
Lab 2 |
50 word log summary |
Week 4 |
Lab 3 |
50 word log summary |
Week 5 |
Lab 4 |
50 word log summary |
Week 6 |
Lab 5 |
50 word log summary |
Week 7 |
Assd Lab 1 (21%) |
Assessed lab (work at home)– two programs to be submitted |
Week 8 |
Lab 6 |
50 word log summary |
Week 9 |
Lab 7 |
50 word log summary |
Week 10 |
Lab 8 |
50 word log summary |
Week 11 |
Assd Lab 2 (21%) |
2-hour assessed lab (exam conditions)– two programs to be submitted |
January |
Exam (50%) |
2-hour exam under exam conditions |
At the end of each unassessed lab, you are required to submit a 50 word
summary of the work you have completed in the lab session, with a very brief indication of what was the main outcome of your lab session. Valid summaries will receive a mark which counts towards the module mark.
You do not need to submit your programs for non-assessed labs.
1.2. Assessed Labs
The assessed labs are two hours long and are open book. This means
Access to Canvas / lecture notes / slides is permitted.
You can look at your previous lab work.
You will work on your own.
The assessed lab will contain two exercises. Both exercises need to be submitted via Canvas for assessment, by the deadline given on the assessment lab sheet.
Bear in mind that credit will be given for techniques you use in your program, even if your program does not fully work.
1.3. Canvas Multiple-choice Examination
This examination will take place in January
Multiple-choice exam, containing 40 questions.
The length of the exam is 2 hours.
1 mark for a correct answer, 0 marks for an incorrect answer. Marks are not deducted for incorrect answers.
The exam will be closed-book and under full exam conditions. This means:
No books, lecture notes or any other material can be taken into the exam room
No talking
No communication devices
You are allowed to use a scientific calculator, from which all formulas and text should be deleted before the start of the exam.
Although the exam is accessed using Canvas, you will not have any access to any lecture notes or slides on Canvas.
2. Introduction to compilers / linkers
2.1. The ANSI standard
One of the strengths of C is that it has an international standardised format which is very widely used.
During the 80’s, ANSI (the American national Standards Institute) convened a committee with a brief to revise and update C. In 1989, ANSI published a new definition of C (updated in 1990), and this became the standardised international version of C.
A newer version of the ANSI standard has been brought out, called “C99” but not all of its features are implemented in all compilers. Most compilers implement the 1990 version of the ANSI standard.
When using specialist compilers (such as those for low-power microprocessors or embedded systems), it is important to remember that those systems may not be able to support all ANSI features of the C90 standard. For example, floating point datatypes may not be available.
Safety-critical applications which make use of software (such as aerospace, medical or military applications) may have additional strict software standards and specialist tools which support these. Here too it may not be possible to support all features of the ANSI standard.
2.2. The compile and link stages
We have seen that a computer can only interpret machine code (strings of binary digits). In principle, a programmer could input this machine code directly into the computer. However, in practice, it would be virtually impossible for any programmer to write anything other than very trivial programs directly in machine code.
The concept of a programming language is to allow the programmer to write instructions in something more intelligible to a human being than machine code and get a second computer program (a compiler) to convert this sequence of instructions into machine code.
Computer program |
Compiler
Machine code |
If we provide some more detail on the above diagram:
Computer program (in C) |
Linker
Machine specific assembler code |
When a C program is compiled, it is converted into an intermediate format known as object code. The programmer will need to use functions such as input / output, mathematics, file operations, graphics etc. These are usually provided by the compiler vendor and stored in a library.
A library is a collection of functions which have been developed for some particular purpose.
This is useful since it means that, for example, the programmer does not need to define their own square root or log function, and can simply use the function that already exists in the library.
The linker takes the object code and the libraries, and combines them to create a low-level version of the program in assembler. This is then assembled to create the final machine code (executable code) which can run on the processor.
Although this seems like a lot of steps, it can usually be performed usually by pressing a single button or menu option on a development system. However if there are errors, you will need to understand which stage of the process created the error, and how to fix it.
2.3. Other terminology – Debugger / IDE / API
Debugger – a program or facility within a compiler to pause a program, and analyse its variables, what it’s doing, etc.
IDE - Integrated Development Environment – a large software package which contains a compiler, linker, debugger and a text editor to enter programs. Examples of IDEs include:
Visual Studio – IDE used on Windows for developing in C, C++ and C#
Eclipse – mainly used with the gcc compiler on Linux, also used with Java and embedded systems
CodeBlocks – freeware IDE used with gcc on Windows/Linux/MacOS
XCode – main IDE used on MacOS.
The labs will make use of CodeBlocks with gcc, though some students may make use of Visual Studio during the course. Guides on how to use all of the above IDEs are available on Canvas.
API – Application Program Interface. A set of functions in a library, which enable one program or library to communicate with another. For example there are APIs which enable C programs to communicate with Java and Matlab programs.
3. Operators
We have already seen an example C program where we perform an addition. C supports all of the arithmetic operators (+,-,* and /) as well as a number of other operators to perform arithmetic. The table below lists these operators and gives an example of their use.
Operation |
Operator |
Example |
Addition |
+ |
c=a+b; |
Subtraction |
- |
c=a-b; |
Multiplication |
* |
c=a*b; |
Division |
/ |
c=a/b; |
Increment |
++ |
++c; |
Decrement |
-- |
--c; |
Modulus |
% |
c=a%b; |
The effect of the first 4 operators is fairly obvious. Thus, for example, the code:
int product; int value1=10; int value2=20; product=value1*value2; |
This simply multiplies the two variables, value1 and value2 together and assigns the result to the variable product.
Less obvious are the final 3 operators. We can give examples of their use as follows:
3.1. Increment
This adds one to the value of the variable. The increment can be applied before (prefix) or after (postfix) the variable is used.
/* post-fix increment: count=5 */ /* pre-fix increment: count=6 */ |
Note that in the above example, applying the increment operator pre- or post- fix doesn’t alter its effect. The variable is incremented by one. However, if the variable is used, then the result will depend on whether it was used before or after incrementing:
int count=4; printf("count=%d \n", count++); |
Sample program output:
count=4 |
In this case, the variable count is incremented after it is outputted. Compare with:
int count=4; printf("count=%d \n", ++count); |
Sample program output:
count=5 |
In this case, the variable count is incremented before it is outputted.
3.2. Decrement
This subtracts one from the value ofthe variable. The decrement can be applied before (prefix) or after (postfix) the variable is used.
/* post-fix decrement: count=3 */ /* pre-fix decrement: count=2 */ |
The same comments apply regarding pre- and post-fix application of the operator as in the case of the increment operator.
3.3. Modulus
The modulus operator assigns the remainder left over after a division. Thus, in the following code fragment, the variable count is assigned to the remainder of dividing 20 by 3 (which is 2).
2022-01-17