diff --git a/00-helloworld.c b/00-helloworld.c new file mode 100644 index 0000000..6b50176 --- /dev/null +++ b/00-helloworld.c @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) { + printf("Hello, world!\n"); +} + diff --git a/README.md b/README.md index 10c5fbf..c8cfd39 100644 --- a/README.md +++ b/README.md @@ -85,3 +85,65 @@ it will get the address of the start of the array `c`, compute the memory addres later, and try to see what's there. This will certainly be gibberish and might cause the operating system to kill your program. +# Setting Up a Development Environment + +To develop C, you need a _compiler_, and it's nice to have a _build system_. I'll assume +you're on Ubuntu; to install GCC, the GNU Compiler Collection, and make, a build system, +on Ubuntu, run: + +```bash +sudo apt install gcc make +``` + +Now make a directory to work in and open up a file. I'm going to call it `00-helloworld.c`. +Into that file, type: + +```c +#include + +int main(int argc, char** argv) { + printf("Hello, world!\n"); +} +``` + +(I'll explain it all in a sec!) + +Now, in the terminal, in that folder, type: + +```bash +# GCC, please compile the code in 00-helloworld.c +# and write the executable out to 00-helloworld.64 +gcc 00-helloworld.c -o 00-helloworld.64 +``` + +This should print out "Hello, world!" in your terminal. Let's dissect that. + +```c +#include +``` + +Beginning with `#` means that this is a preprocessor directive, telling the compiler +to look up and include a file called `stdio.h` in the standard search path (because of +the `<` and `>`, as opposed to `"` and `"` which mean look in the current directory) +and include its text here. + +That file defines, among other things, the function `printf` that is used later. + +```c +int main(int argc, char** argv) { +``` + +This is the definition of the magic function `main`, which is run when the program starts. +It returns an `int`, which should be zero for success or anything else for failure, and +takes the number of command line arguments `argc` and an array of arrays of characters +(an array of strings) `argv`. `c` stands for "count" and `v` for "vector" in this case. + +```c +printf("Hello, world!\n"); +``` + +This calls the `printf` function, which `print`s a `f`ormatted string, although we're +not using any of the formatting options here. + +Congratulations! This is your first C program~ +