Week 08 Tutorial Sample Answers
1. How are you going with assignment 1?
Please remind students that the assignment is due at the end of this week.
2. Consider the program below:
#include <stdio.h> int main(void) { char str[10]; str[0] = 'H'; str[1] = 'i'; printf("%s", str); return 0; }
-
What will happen when the above program is compiled and executed?
The above program will compile without errors.
printf
, like many C library functions, expects strings to be null-terminated.In other words,
printf
expects the arraystr
to contain an element with value '\0' which marks the end of the sequence of characters to be printed.printf
will printstr[0]
('H'),str[1]
then examinestr[2]
.Code produced by
dcc --valgrind
will then stop with an error becausestr[2]
is uninitialized.The code with gcc will keep executing and printing elements from
str
until it encounters one containing '\0'. Oftenstr[2]
will by chance contain '\0' and the program will work correctly.Another common behavior will be that the program prints some extra "random" characters.
It is also possible the program will index outside the array, which would result in it stopping with an error if it was compiled with
dcc
.If the program was compiled with gcc and uses indices well outside the array, it may be terminated by the operating system because of an illegal memory access.
-
How do you correct the program?
#include <stdio.h> int main(void) { char str[10]; str[0] = 'H'; str[1] = 'i'; str[2] = '\0'; printf("%s", str); return 0; }
3. Name 3 errors in this program:
#include <stdio.h> #define MAX_LINE 4096 int main(void) { char line[MAX_LINE]; int i; while (fgets(line, MAX_LINE, stdin) != NULL) { i = MAX_LINE; while (line[i] != '\n') { i = i - 1; } printf("the line is %d characters long\n", i); } return 0; }
- On the first execution of the inner while loop, it accesses
line[MAX_LINE]
, which is an illegal array index. - It accesses uninitialized array elements - fgets only assigns to the array elements necessary to hold the characters of a line plus a '\0'.
- There may not be a '\n' in the array. fgets won't put a '\n' in the array if the line is too long to fit in the array. If there is no '\n' in the array, the code will access a non-existent array element (
line[-1]
).
4. Strlen is a function that returns the length of a string. Write your own C function to do the same.
int myStrlen(char *string);
int myStrlen(char *string) {
int i = 0;
while (string[i] != '\n' && string[i] != '\0') {
i = i + 1;
}
return i;
}
5. parts of a pointer
Look at this C code:
int n = 42;
int *p, *q;
p = &n;
*p = 5;
*q = 17;
q = p;
*q = 8;
Explain what does actually happen in the memory after each line in order?
6: Command line arguments
Tutor demo!
In this section we'll have a look at command line arguments and how they can be used to pass input to a C program when it is executed.
We'll be using this starter code:
#include <stdio.h>
int main(int argc, char *argv[]) {
return 0;
}
The following diagram may be useful as a guide for explaining how command line arguments work:
Your turn!
In groups we will write pseudocode or a flowchart for one of the following programs:
Sum of Command Line Arguments: Write a C program that takes multiple integers as command-line arguments and prints their sum.
Count Characters in Command Line Arguments: Write a C program that counts the total number of characters in all the command-line arguments passed to it.
Reverse Command Line Arguments: Write a C program that prints all the command-line arguments passed to it in reverse order.
Check for Command Line Arguments: Write a C program that checks if any command-line arguments were provided except for the program name. If none were provided, print a message indicating so; otherwise, print the number of arguments.
Voice of the Student
✨ Help Us Improve Your Learning Experience ✨
Your feedback helps us understand what’s working well and what might need improvement.
This quick, anonymous check-in has just two questions and takes less than a minute to complete.
Please answer honestly — your input makes a real difference.