Basic example hello world

This commit is contained in:
Leonora Tindall 2019-03-13 23:29:52 -05:00
parent 7f6bb71b9d
commit 1cf4a8c67e
Signed by: nora
GPG Key ID: 99041B68DBC02DAC
2 changed files with 68 additions and 0 deletions

6
00-helloworld.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(int argc, char** argv) {
printf("Hello, world!\n");
}

View File

@ -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 <stdio.h>
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 <stdio.h>
```
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~