Skip to content

Commit 1f6217e

Browse files
committed
Added task on command line arguments, added gitignore
1 parent e407cfc commit 1f6217e

File tree

7 files changed

+81
-0
lines changed

7 files changed

+81
-0
lines changed

01-introduction/01-hello/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
hello

01-introduction/02-make/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
hello
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
knights

01-introduction/03-arguments/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.PHONY: all clean
2+
3+
CC = gcc
4+
CFLAGS = -Wall -Wextra -pedantic -Werror -std=c99 -g -O0
5+
6+
all: knights
7+
8+
knights: knights.o
9+
$(CC) -o $@ $<
10+
11+
knights.o: knights.c
12+
$(CC) -c $(CFLAGS) -o $@ $<
13+
14+
clean:
15+
rm -f knights.o knights
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Command Line Arguments
2+
## Motivation
3+
Often specific details of the behaviour of a program cannot be configured at
4+
compile time. Instead, methods to configure a program a run time are needed.
5+
6+
Lets for example look at the shell command `cp`, which creates a copy of a file.
7+
Specifying the name of the file to copy and the name of the copy to create in
8+
the C source file of the program `cp` would be terrible for its usability.
9+
Instead, `cp` allows to specify source and destination at runtime by calling it
10+
with command line arguments, e.g. `cp orignal_file copy_of_original_file`.
11+
12+
In this task you will learn who to use command line arguments in your own C
13+
programs.
14+
15+
## Task
16+
- Create the program `knights`
17+
- The source should be placed in `knights.c`. Replace its contents with your
18+
solution
19+
- A Makefile for this program is already provided
20+
- If `knights` is called with no arguments, it should print: "We are The Knights
21+
Who Say "Ni!". We demand a shrubbery. One that looks nice. And not to
22+
expensive."
23+
- If the application is called with one or more arguments, of which at least one
24+
is "shrubbery", it should print: "We are no longer The Knights Who Say "Ni!".
25+
We are now The Knights Who Say Ekki-Ekki-Ekki-Ekki-PTANG. Zoom-Boing.
26+
Z'nourrwringmm. Bring us another shrubbery. Slightly higher than the first one.
27+
Then you now must cut down the mightiest tree in the forest - with a herring!"
28+
- If the application is called with five or more arguments, of which at least
29+
five are "it", it should print: "Arrhhg!". This has priority over the
30+
"shrubbery"-case
31+
- If no of the cases above match, the command should behave as it was called
32+
with no arguments
33+
- The current source code in `knights.c` contains some useful hints
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
int
6+
main(int argc, const char **argv)
7+
{
8+
// argc = Number of arguments provided + 1
9+
// argv = Array of the arguments provided as string.
10+
// BUT: argv[0] = Name of the program, argv[1] = The first argument,
11+
// argv[2] = The second argument, ...
12+
printf("The name of this program is %s\n", argv[0]);
13+
printf("This program was called with %i argument(s)\n", argc-1);
14+
15+
if (argc >= 2){
16+
// At least one argument
17+
if (strcmp("secret", argv[1]) == 0){
18+
puts("The first argument was \"secret\"");
19+
} else {
20+
puts("The first argument was NOT \"secret\"");
21+
}
22+
}
23+
24+
return EXIT_SUCCESS;
25+
}
26+

01-introduction/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ If you are a noob with no experience in C programming, you are right here ;-)
55
## Table of Contents
66
- [Hello World](01-hello)
77
- [GNU Make](02-make)
8+
- [Command Line Arguments](03-arguments)

0 commit comments

Comments
 (0)