Week 08 Tutorial Questions
1. How are you going with assignment 1?
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?
- How do you correct the program?
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; }
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);
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.