Easy, Breezy Intro to C


Here’s a guide to writing your first C program using Terminal on OS X:

First, open a Terminal window and navigate to the directory you’d like to work in. Create and open a new file using nano or vim:

$ nano helloworld.c

Now here comes the program:

#include<stdio.h>

int main(void) {

printf("Hello, world!\n");

return 0;

}

Now use ^O to save and ^X to exit nano. Back on the command line, compile your program:

$ cc helloworld.c

Now use ls -l to print out the contents of your directory. You’ll see that a new file has been created, a.out. This is the compiled version of your program. Open it:

$ ./a.out
Hello, world!