http://ghostcode.freeshell.org

/ Home / Unix Books / Coding / About / GhostBSD /


A little journey into C Programming on Unix, although C can be written and compiled on any operating system.

Perhaps considered by some as an ‘old’ language (Unix was written in C), it still has a place today. C programs written decades ago still function as they should.

Any simple text editor will do, but not an office application such as Word. A dedicated IDE can be used, or on a Linux / Unix system

you can use nano or gedit to write a simple script. First and foremost, the usual way is with ‘Hello World’.

Create a folder called bin and save all your scripts in there for easier access, and it will add to your $PATH on reboot.

Type in echo $PATH and you should see something like /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:~/bin

Some Basic Information.

The 32 words below are the keywords used in C programming:

auto break case char const continue default do double else enum extern float for goto if int long register

return short static signed sizeof struct switch typedef union unsigned void volatile while

The 14 escape sequences we use are below, the function is shown in brackets:

\n (newline) \t (tab) \v (vertical tab) \b (backspace) \r (carriage return) \f (form feed) \a (audible alert)

\\ (backslash) \? (question mark) \’ (single quote) \” (double quote) \xhh (heaadecimal number) \000 (octal number) \0 (null character)

Below we see <stdio.h>, this function is used in about one third of the C programming library. The ‘h’ signifies header files.

Other library header files include:

ctype.h string.h math.h stdlib.h assert.h stdarg.h setjmp.h signal.h time.h limits.h float.h

This has to be compiled in the gcc compiler and then it will be executed as a c program within the shell. Save this as hello.c

My personal preferences are Bash as the shell and either nano for a small program, perhaps Geany for a larger program.

For the record, I wrote the bulk of the pages here in Libre Office Writer and edited the HTML file with Geany.

Horses for courses really, occasionally I will simply open my text editor and write in there.

To compile, you need to write the following in the shell. If using GhostBSD do sudo pkg install gcc beforehand.

gcc hello.c -o hello

To make it executable, chmod +x hello

Now for a program that generates the Fibonacci sequence:


Below is an image of the gcc compiler commands, the various formatting will be explained further on (\n and \t).

Here we have the gcc command to change the code into a readable / interactive format.









More to come soon!