diff --git a/02_command_line_tool/Makefile b/02_command_line_tool/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..34690ff2e1032e26a31fe55eb43948b424aa5dd3 --- /dev/null +++ b/02_command_line_tool/Makefile @@ -0,0 +1,17 @@ +# Change your compiler here if you're not using gcc. CC is for C, and CXX is for C++. +CC = gcc +CXX = g++ + +CFLAGS = -Wall -O3 + +.PHONY: all +all: add_integers + +add_integers: add_integers.c + $(CC) $(CFLAGS) -o add_integers add_integers.c + +# This rule deletes all the binaries. It's declared "phony" which means it doesn't actually make +# anything. Otherwise Make would get confused about when it should run this rule. +.PHONY: clean +clean: + rm add_integers diff --git a/02_command_line_tool/README.md b/02_command_line_tool/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a5c361434eaff43c16537fd7704524ee20bd62f3 --- /dev/null +++ b/02_command_line_tool/README.md @@ -0,0 +1,3 @@ +# Basic Input + +If you want to pass arguments to your program, you can add them to `main`. diff --git a/02_command_line_tool/add_integers.c b/02_command_line_tool/add_integers.c new file mode 100644 index 0000000000000000000000000000000000000000..5d99c584a6fe099c26f86d04abb3628cad2583d7 --- /dev/null +++ b/02_command_line_tool/add_integers.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <stdlib.h> + +// To accept command line input, we add two arguments to main. The first will contain the number of +// inputs on the command line, and the second is an array of C strings (pointers to characters) that +// contain what was written. Main is special and has to take no arguments, or these two arguments. +// (Other functions can take whatever arguments they like.) +int main(int argc, const char* argv[]) { + // If the user didn't give us two numbers, we exit. + if (argc < 3) { + printf("Please type two numbers.\n"); + return 0; + } + + // If the user gave us more than two numbers, we just ignore the rest. + // Can you change the program so it adds all the numbers? + if (argc > 3) { + printf("Woah, there, that's a lot of numbers.\n"); + } + + int a = atoi(argv[1]); + int b = atoi(argv[2]); + int c = a + b; + + printf("%i + %i = %i\n", a, b, c); +} diff --git a/03_inputs_and_outputs/.gitignore b/03_inputs_and_outputs/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..36aaa440a5fa32b598ff6e88b2068e4059ef113c --- /dev/null +++ b/03_inputs_and_outputs/.gitignore @@ -0,0 +1 @@ +add_integers