|
| 1 | +# Hello |
| 2 | + |
| 3 | +Here's your first C program: |
| 4 | + |
| 5 | + #include <stdio.h> |
| 6 | + |
| 7 | + int main(void) |
| 8 | + { |
| 9 | + printf("hello, world\n"); |
| 10 | + } |
| 11 | + |
| 12 | +You have seen it in lecture, and now you're ready to try running it yourself. |
| 13 | + |
| 14 | + |
| 15 | +## Dive into your development environment |
| 16 | + |
| 17 | +### Instructions for Mac OS |
| 18 | + |
| 19 | +Go to your Terminal. You should find that its "prompt" resembles the below. |
| 20 | + |
| 21 | + jharvard@somewhere ~ % |
| 22 | + |
| 23 | +Here on out, to execute (i.e., run) a command means to type it into a terminal window and then hit Enter. Commands are "case-sensitive," so be sure not to type in uppercase when you mean lowercase or vice versa. |
| 24 | + |
| 25 | +Now execute |
| 26 | + |
| 27 | + cd ~/Documents/Programming/ |
| 28 | + |
| 29 | +to move yourself into (i.e., open) that directory. Your prompt should now resemble the below. |
| 30 | + |
| 31 | + jharvard@somewhere Programming % |
| 32 | + |
| 33 | +If not, retrace your steps and see if you can determine where you went wrong. |
| 34 | + |
| 35 | +Now, open Atom, point to the File menu and choose New. This will open a new editor with a blank "Untitled1" file in it. First thing to do is to give it a name and save it into your new directory. Press **CTRL-S** or **Cmd-S** to open the file dialog. For **Filename**, type `hello.c`. Then below, choose (click) the `Documents`, then the `Programming` folder that you just created and click on **Save**. |
| 36 | + |
| 37 | +In your new file, type the C code for "Hello, World" as seen above. Save it once more. |
| 38 | + |
| 39 | +### Instructions for Windows |
| 40 | + |
| 41 | +Go to your Terminal. You should find that its "prompt" resembles the below. |
| 42 | + |
| 43 | + jharvard@COMPUTER:~$ |
| 44 | + |
| 45 | +Here on out, to execute (i.e., run) a command means to type it into a terminal window and then hit Enter. Commands are "case-sensitive," so be sure not to type in uppercase when you mean lowercase or vice versa. |
| 46 | + |
| 47 | +Now execute |
| 48 | + |
| 49 | + cd /mnt/c/Users/<Windows User Name>/Documents/Programming/ |
| 50 | + |
| 51 | +to move yourself into (i.e., open) that directory. Your prompt should now resemble the below. |
| 52 | + |
| 53 | + jharvard@COMPUTER:Programming$ |
| 54 | + |
| 55 | +If not, retrace your steps and see if you can determine where you went wrong. |
| 56 | + |
| 57 | +Now, open Atom, point to the File menu and choose New. This will open a new editor with a blank "Untitled1" file in it. First thing to do is to give it a name and save it into your new directory. Press **CTRL-S** or **Cmd-S** to open the file dialog. For **Filename**, type `hello.c`. Then below, choose (click) the `Documents`, then the `Programming` folder that you just created and click on **Save**. |
| 58 | + |
| 59 | +In your new file, type the C code for "Hello, World" as seen above. Save it once more. |
| 60 | + |
| 61 | + |
| 62 | +## Listing files |
| 63 | + |
| 64 | +Next, type precisely the below (in lowercase), then hit Enter: |
| 65 | + |
| 66 | + ls |
| 67 | + |
| 68 | +You should see just `Makefile` and `hello.c`. That's because you've just listed the files in that same folder, this time using a command-line interface (CLI), using just your keyboard, rather than the graphical user interface (GUI) represented by that folder icon. In particular, you *executed* (i.e., ran) a command called `ls`, which is shorthand for "list." (It's such a frequently used command that its authors called it just `ls` to save keystrokes.) Make sense? |
| 69 | + |
| 70 | + |
| 71 | +## Compiling programs |
| 72 | + |
| 73 | +Now, before we can execute the program at right, recall that we must *compile* it with a *compiler* (e.g., `clang`), translating it from *source code* into *machine code* (i.e., zeroes and ones). Execute the command below to do just that: |
| 74 | + |
| 75 | + clang hello.c |
| 76 | + |
| 77 | +And then execute this one again: |
| 78 | + |
| 79 | + ls |
| 80 | + |
| 81 | +This time, you should see not only `hello.c` but `a.out` listed as well? (You can see the same graphically if you click that folder icon again.) That's because `clang` has translated the source code in `hello.c` into machine code in `a.out`, which happens to stand for "assembler output," but more on that another time. |
| 82 | + |
| 83 | +Now run the program by executing the below. |
| 84 | + |
| 85 | + ./a.out |
| 86 | + |
| 87 | +Hello, world, indeed! |
| 88 | + |
| 89 | + |
| 90 | +## Naming programs |
| 91 | + |
| 92 | +Now, `a.out` isn't the most user-friendly name for a program. Let's compile `hello.c` again, this time saving the machine code in a file called, more aptly, `hello`. Execute the below. |
| 93 | + |
| 94 | + clang -o hello hello.c |
| 95 | + |
| 96 | +Take care not to overlook any of those spaces therein! Then execute this one again: |
| 97 | + |
| 98 | + ls |
| 99 | + |
| 100 | +You should now see not only `hello.c` (and `a.out` from before) but also `hello` listed as well? That's because `-o` is a *command-line argument*, sometimes known as a *flag* or a *switch*, that tells `clang` to output (hence the `o`) a file called `hello`. Execute the below to try out the newly named program. |
| 101 | + |
| 102 | + ./hello |
| 103 | + |
| 104 | +Hello there again! |
| 105 | + |
| 106 | + |
| 107 | +## Making things easier |
| 108 | + |
| 109 | +Recall from lecture that we can automate the process of executing `clang`, letting `make` figure out how to do so for us, thereby saving us some keystrokes. |
| 110 | + |
| 111 | +Now execute the below to compile your program one last time. |
| 112 | + |
| 113 | + make hello |
| 114 | + |
| 115 | +You should see that `make` executes `clang` with even more command-line arguments for you? More on those, too, another time! |
| 116 | + |
| 117 | +Now execute the program itself one last time by executing the below. |
| 118 | + |
| 119 | + ./hello |
| 120 | + |
| 121 | +Phew! |
0 commit comments