Learn the C Programming Language!
The first piece of code you should always write when learning a new language is called the “Hello, world” code. This code will do nothing but print out the phrase, ”Hello, world!” onto your screen.
The first thing you need to do is open your command line (Windows) or Terminal (Mac/Linux). From there, type
$ mkdir c-learning
$ cd c-learning
Note: Ignore the “$” symbol at the beginning of any line, it’s just there to tell you that you are supposed to be typing this on your command line.
mkdir made a new folder for your c code and executables. cd changes your directory from the default home one to the one you just made.
Next, open the new file:
$ vi helloworld.c
If you don’t have vi installed, I suggest installing it. We will be using it for the majority of this course.
Now, you should see a text editor on your screen. Vi is a text editor that is very bare-bones, and should come with your operating system. It works right out of your terminal.
Note: If you don’t want vi running out of your terminal, check out gvim or macvim.
From there, press i: This will put you into insert mode. Now you can add text.
From there, enter this code in:
#include <stdio.h>
main() {
printf("hello, world\n");
}
Now, you should hit the esc key, and type :wq. Adding the colon before makes it a command. wq tells the editor to write, then quit.
Now you should see your terminal again. From there, type
$ clang helloworld.c
$ ./a.out
Assuming you have done everything correctly, you should see this on your screen:
hello, world
Note: you may receive a warning on your screen after running clang. That’s because the compiler (in this case, clang) noticed something out of the ordinary. However, that’s not a big deal; we’ll learn more about this in chapter two.