Question 10
Defensive Programming in C - fundamental C programming structures
(a) What will be the value of counter printed when the following code runs? Explain your answer
int increment(int counter) {
counter++;
return 0;
}
int main(void) {
int counter = 4;
if (counter > 0) {
int counter = increment(counter);
} else {
increment(counter);
}
printf("%d\n", counter);
}
(b) Write a C program that asks the user to enter up to 20 characters and then prints the characters the user entered in reverse. The program should stop accepting characters when the user enters a non-alphabetic character (ie anything other than a, b, c .... z or A, B, C .... Z)
(c) Explain two risks to running C programs and defensive programming techniques you can use to mitigate them in your code.
[Total for Question 10: 17 marks]