From 6efdccd204bf093cb9e2e34f24585937fdb1a0f3 Mon Sep 17 00:00:00 2001
From: Erik Strand <erik.strand@cba.mit.edu>
Date: Tue, 20 Oct 2020 23:40:23 -0400
Subject: [PATCH] Add a program that adds numbers from the command line
---
02_command_line_tool/Makefile | 17 +++++++++++++++++
02_command_line_tool/README.md | 3 +++
02_command_line_tool/add_integers.c | 26 ++++++++++++++++++++++++++
03_inputs_and_outputs/.gitignore | 1 +
4 files changed, 47 insertions(+)
create mode 100644 02_command_line_tool/Makefile
create mode 100644 02_command_line_tool/README.md
create mode 100644 02_command_line_tool/add_integers.c
create mode 100644 03_inputs_and_outputs/.gitignore
diff --git a/02_command_line_tool/Makefile b/02_command_line_tool/Makefile
new file mode 100644
index 0000000..34690ff
--- /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 0000000..a5c3614
--- /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 0000000..5d99c58
--- /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 0000000..36aaa44
--- /dev/null
+++ b/03_inputs_and_outputs/.gitignore
@@ -0,0 +1 @@
+add_integers
--
GitLab