Week 06 Tutorial Questions

Part 1: Tutorial Performance Hurdle,

So far in this course we have only used scanf and printf to scan input and print output in our programs.

#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?

int myStrlen(char string[]);

Part 2: 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:

Command line arguments

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.