C Programming in 2021

by Ray Dall

################################################################################
Section 1
################################################################################


From this point on, I'm going to assume that you are using some form of UNIX based system to write your code on. I'm hoping you took the Raspberry Pi route. You won't be disappointed.

To begin with, you'll need to know a bare minimum of 5 very simple UNIX commands:

In UNIX, the cd command means "Change Directory". It selects what directory or folder you'll be working in. The mkdir command means "Make Directory". It creates new directories. Also, the ls command means "List". ls allows you to see the contents of the directory you are in.

Between these three commands, you should be able to maneuver your way around the operating system enough to do C coding.

Finally - you'll need to know the "nano" and "cc" commands. We'll discuss them more in a bit.

Let us begin with a clean slate. Open up a terminal window and create a directory to contain all of your C programs in.

NOTE: the [ENTER] below represents the "enter" key on your keyboard. Some keyboards may have a "return" key instead. Also - UNIX is CASE SENSITIVE. That means if you see upper case (big letters), you must type upper case. If you see lower case letters (small letters) you must type small letters. Using lower case when upper case is called for (or vise-versa) will cause your program to not function properly.

In your terminal, type the following commands EXACTLY:


cd [ENTER]
mkdir ./CODING [ENTER]
cd ./CODING [ENTER]



You should have just created an empty directory (aka: a folder for ex-windows people) called "CODING", and you should now be in that directory.

If you put all your programs that you write in this directory, you should be able to find them whenever you want to start coding again.

There are 3 basic steps in working with C:


WRITE the code (using some kind of simple text editor).
COMPILE the code (using a compiler).
TEST/RUN the program that is spewed out by the compiler.

In order to write code, you will need a text editor... NOT A WORD PROCESSOR - there is a difference. You will need to use the editor to write your code, then save it out to a standard ascii text file using a ".c" file extension (more on this later). In Linux/Unix you can use nano, ed, vi, vim, emacs, pico, nano or many others. Note that I said nano twice. That wasn't a typo, that was a subliminal suggestion.

If you are already familiar with one of the editors I mentioned above, feel free to use it. Otherwise, I suggest you use nano, as I find that to be the easiest for new students to learn. You will want to learn vi or ed at some point, as they are available on ANY Unix system without modification, but it isn't necessary to learn C and can be an unnecessary learning hurdle when first learning C. I do, however, give extra credit to any student who goes through this entire course using ed. It can be a wee bit challenging.

One final note on text editors for "purists" - I have found systems that did NOT have vi or vim, and only had ed.

Do NOT use a "Word Processor" that is capable of italics, bold, or other font formatting, as this will mess you up big time. Stick to a simple text editor, not a word processor.

In your text editor, type the following program (DO NOT CUT AND PASTE IT!). Plagerism is cheating, and Repetition is the key to learning, so if you want to learn this well enough to get gainful employment - type it out. You won't learn it as well if you simply copy what I did.

MOST instructors start you out with a "Hello World" program. I am intentionally going to take you a different route. Open a command line interface (aka: cli, terminal or shell) on your computer and initiate your text editor. If you were smart and got that Raspberry Pi I suggested, simply type:


nano first.c



This will open up your nano text editor allowing you to begin writing code. It should look like this:

(Click to open Full Size in another tab)


Keep in mind that this nano was written in the 70's. It is a bit terse, but still better than many others - and frankly, it is probably easier to use for beginning students than any other text editor.

Instructions for using nano are printed right at the bottom of the page! Just READ it! The carrot symbol "^" stands for the [CONTROL] or [CTRL] button at the bottom of your keyboard (Usually the lower left hand corner button). So to exit nano, you would simply type [CTRL] X (Control X) and it exits.

One oddity peculiar to nano is that instead of using the word save, it uses "Write Out" ([CTRL] O), which was not uncommon for software written during that era. They tended to use the word write instead of save. (You'll encounter this more later).

Writing code in nano is simple. You just start typing. WYSIWYG (What you see is what you get). When you are done you "write it out" ([CTRL] O) and exit ([CTRL] X).

Let's give it a go....

Making absolute certain you don't make ANY mistakes...type the following into nano:


// THIS IS A TEST

/*
There are two kinds of COMMENT statements in C
Single line comments which begin with //
...
and multiple line comments that start with a slash asterisk, and
end with an asterisk slash.
*/

// Comments don't actually DO anything.


Now save the program using write out ([CTRL] O), and exit using [CTRL X]. You have just written your first nano file. To reopen that file, simply type the same thing in you did to create it...


nano first.c



Like it said in the file you wrote, Comments don't actually do anything - they are simply there to keep you, the programmer, on track. They sure help out 5 years later when you are trying to figure out why you wrote a particular bit of code the way you did. Comments = understanding. Now that you know how to use nano... let's actually write a program. Reopen your first.c program ( nano first.c ), and remove all your comments, then write the following code:

// type	name	arguments	endpunc
   int	main	()		{}



Once you have typed this into nano, write it out as "first.c", then exit. You have just WRITTEN your first C program. It doesn't do much... but it does work. Before you can run the program, you have to COMPILE it. Do this by typing:


cc first.c



The "cc" stands for (are you ready for this?) C Compiler. Wow.
If you have made no mistakes, it will simply return to the command prompt with no error messages.

Think of the compiler as the cruelest, meanest, most critical grammar teacher you will ever have. It doesn't give you any credit for doing a good job, and it yells at you for even the slightest punctuation error. With that thought in mind.... if it didn't yell at you - you did fine.

If you now type:

ls [ENTER]


You should see a new file has been created called "a.out"


$> ls 
   a.out	first.c
$> 


So you have WRITTEN your program (first.c), and COMPILED it ... now all you have to do is test/run it.


$> ./a.out 
$> 


"It didn't do anything!?"

I know that is what you are thinking. Fact is, you really didn't tell it to do anything. You wrote a blank program. The purpose of the exercise was to teach you two things:
1) How to write, save, compile, and run a program. So far - you are a glorious success!
2) The necessary parts of a FUNCTION (aka: statement).


Now click on 2 to go to section 2...
################################################################################
1 2 3 4 5
################################################################################