Introduction #
Welcome to C Programming 1.
What is Programming? #
Simply put, programming is telling a computer what to do. Computers only understand very basic instructions like:
- Add these two numbers together
- Move this number here
- Compare these two numbers
Programming in this manner is tedious and in order to be more productive, higher level programming languages have been created. The programming language that we will be using in the course is The C Programming Language
The C Programming Language #
C is a general purpose programming language created by Dennis Ritchie between 1972 and 1973. It was originally created as a systems programming language for the Unix operating system.
C gained popularity in the 80s and today it is one of the most popular and widely used programming languages.
While C was created almost 50 years ago, it is still updated. The most recent version of the C specification being C18.
C89/C90, also referred to as ANSI C is still widely in use today. Ritchie was originally inspired by the BCPL language, the specification of which was not available, and he created a simpler version of it called B. He then created an improved version of B, called C.
C is a compromise between the language and logic that we use as humans and the small instructions that machines understand. It serves to boost our productivity as we can think about code in a slightly more natural way than we would otherwise have to.
Where is C used? #
Nearly every operating system, computer program, computer game, website, phone, traffic light, washing machine, car, kindle, burglar alarm, security system…. You get the idea. It’s all written in C or involves a program written in C. You can check out the current (somewhat subjective) rankings of programming languages on the TIOBE Programming Community Index. C and Java are usually fighting it out at the top spot but Python is gaining ground as of late.
Why don’t we use Java/Python if they’re the most popular language?
C has been around significantly longer, it’s a smaller language, it runs on more systems, it requires less resources, and it is entrenched in the Electronic Engineering world as it has the ability to easily interact with hardware. Even Java and Python are themselves written in C!
Due to a component shortage we will be starting Linux later in the year
Unfortunately there is a worldwide shortage of Raspberry Pis at the moment and until we can source parts we will have to delay the Raspberry Pi/Linux portion of the class. You can skip this section for now.
Linux #
We will be learning programming in a different computer environment to the one you might be used to. We will not be using the popular Windows operating system and we won’t be using PCs. Instead we will be using Linux and instead of a PC, we will be using a Raspberry Pi.
Linux is an open source operating system, that means that unlike Windows, we can look at the code for it and even product and sell our own Linux distributions.
The Linux distribution we will be using for this class is Raspbian, a Linux distribution created specifically for the Raspberry Pi. This distribution is based on another Linux distribution called Debian. The open source nature of Linux allows the Raspberry Pi Foundation to build on the work of Linux and Debian to create a custom operating system that suits the needs of the Raspberry Pi.
Raspberry Pi #
The Raspberry Pi is a single board computer that consists of all of the components that we might need on a computer, but as one single circuit board. The Raspberry Pi contains an ARM processor, a specific type of RISC (Reduced Instruction Set) processor that is found in modern smart phones.
The original version of the Raspberry Pi contained roughly the same processor as the original iPhone.
The Raspberry Pi 3 is the version that we will be using. The Pi can function in a manner similar to a desktop computer with a mouse and keyboard, but it can also easily interface with electronics via the 40 pin General Purpose Input/Output pins. We will be connecting our Pis to electronic circuits later in the subject.
Getting Started #
As part of this subject you will be issued a person Raspberry Pi that will only be used by your for the duration of the subject.
Make note of the number on the box that you are given.
The computer screens in 4B07A should all have an HDMI cable attached for use with the Pi. The box should contain:
- The Raspberry Pi in a case (numbered)
- Keyboard and Mouse
- USB Power Cable
Setting up the Pi #
To set up the Pi, connect everything before powering it up. There is no ‘On’ switch, just plugging in the cable will power on the Pi. If you power it up before you connect the HDMI cable the screen resolution will be sub-optimal making the interface large and difficult to use.
Your Raspberry Pi was in use last year so we need to wipe the Pi and reinstall Raspbian before we begin. This will ensure that everyone starts off with exactly the same environment.
When the Pi is starting up, a white screen will appear with the Pi Logo. Press the Shift Key on the keyboard when you see this. If you miss this step, just restart the Pi from the main menu and try again.
Then press the Install button and wait for it to install.
Take a look around #
Explore the menu on the Pi and see what programs are installed.
The Terminal #
Back before Graphical User Interfaces (GUIs) were invented, Text User Interfaces (TUIs) were the main way in which users interacted with their computers. Unlike Windows, in Linux the CLI (Command Line Interface, a TUI) is still the main way to perform a lot of tasks. This has its advantages: as programmers we type a lot so we can ignore the mouse and just concentrate on typing. The CLI we will be using on the Pi is called bash.
It also has disadvantages with regard to discovery. In a GUI you can simply look through the options and see what is available, but with the CLI you have to read the manual or ‘man page’.
You can’t use the mouse with programs in the terminal!
A whirlwind tour of our first C Program #
Now, we are going to look at writing our very first C Program. This is just to give you an idea of what is involved. We will be taking a look at each element of this brief overview in more detail as we move through the course.
Our goal for this is to write a program that simply prints out “Hello, world!” to the screen. This can be broken down into a number of sub-tasks:
- Create and edit a text file
- Write some code
- Compile the code
- Run the code
Creating a text file #
We program C using a text editor, there are more powerful editors available just for C but for our first exercise we will be using ‘nano’. Nano comes pre-installed on the Pi and is one of the simplest text editors available for the CLI. It is the equivalent of the basic word processor “Notepad” on Windows.
This text is called the prompt, as it is prompting you to enter a command:
pi@raspberry:~ $
To start Nano, we simply type nano after the command prompt, but to also have it
create a file, we can pass the name of the file to it. Type the text below, after the $
sign, into the CLI and then press Enter:
pi@raspberry:~ $ nano hello.c
Editing a text file #
You should now see that nano has taken up the entire Terminal window, the top should
display the file name, the bottom of the window should display a menu of commands
that you can enter using the keyboard. You can’t use the mouse in nano!
The ^ symbol, for example in the ^W Write Out command is shorthand for the Ctrl
(Control) Key. To save or “Write Out” the file, we press Ctrl+W. That’s Ctrl and W at
the same time. It may ask you to confirm that you wish to write out the file.
To exit the program, we press Ctrl+X.
If you haven’t done so, save the file and exit the program.
Viewing folder contents #
In Linux, folders are often referred to as directories. We can look at the current directory that we are in by using the Print Working Directory command, pwd:
pi@raspberry:~ $ pwd
/home/pi
This should output /home/pi as this is the folder that we are currently working in.
To view the contents of the folder, we use the List command, ls:
pi@raspberry:~ $ ls
Desktop Documents hello.c
You will see several items listed, folders in blue, and files in grey. If you do not see hello.c,
repeat the steps for creating and editing the text file.
Writing our first C Program #
Now that we are sure that our file has been created we can re-open it for editing. To open an existing file, just type nano followed by the filename, the same way we did to create the file. If the file exists it will be opened:
pi@raspberry:~ $ nano hello.c
We will now type in the contents of our first empty C program. This program has almost no functionality, it is simply the bare minimum required to successfully run a C Program.
Carefully type in the following code, save it and exit nano. To indent (move in) the line that
begins with the word ‘return’ use the TAB key on the left side of the keyboard.
As you type your code you will notice that nano recognizes it as C code and highlights certain words to make it more readable. If nano does not do this you may not have correctly named your file.
|
|
Compiling our first program #
As we previously mentioned, C is a language for humans to write and as such it needs to be compiled (translated) into a code that the machine understands.
Compiling our code is relatively easy, there is a C compiler included with Raspbian. The compiler is gcc, The GNU Compiler Collection of which the C compiler is only one of compilers provided for various languages.
To compile our code we need to run gcc and give it the name of our file:
pi@raspberry:~ $ gcc hello.c
If all of your code was right, there should be no output and the terminal should just display a new line ready to read more input from you.
To check that our file has been compiled correctly, we need to list the contents of the directory again. You should see a file named “a.out” highlighted in green. The green highlight means that this file is marked as executable, this means that it is a program and can be run.
To run the program we need to type its name, but we also need to tell bash to look in the current directory. When we type a program name into bash, like ‘nano’ it looks in certain folders for the executable file, but not the current working folder. To force it to look into the current folder for the file we use the “./” path.
pi@raspberry:~ $ ./a.out
When you press Enter, the prompt should reappear after a brief delay indicating tha the program has run and finished successfully.
Now we want to actually make our program do something, add the line in bold to your
program. The printf command is used to print text to the screen, the \n sequence is used to
print a new line to the screen. When you have finished, save the file and exit nano.
|
|
Hello World! #
Compile the file and run it using the same commands as last time. You can use the up and down arrow keys at the prompt to cycle through a history of your previously used commands.
When you run your program you should see it print out:
Hello, world!
If it does not, carefully inspect your code and check to see if there are any differences between the code.
Common Errors #
At this point in our program we may experience some common errors. A syntax error is an error in the sequence of characters that we have written. This is the most common error that beginners experience.
Examine your code carefully against the example above, we will now take a look at our some common beginner syntax errors.
Missing semi-colon #
A missing semi-colon at the end of one of the lines, not every line needs a semi-colon after it but the ones inside the { and } should for now, in the following example a semi-colon is missing from line 4:
|
|
Not closing a bracket #
Every bracket in your C program, be it {},() or <> must always come in a pair. An opening bracket, and a closing bracket. In the example below we can see on line 6 that the closing bracket to match the opening { is missing.
|
|
Not closing a quote #
Every quote in your C program, be it "" or '' must always come in a pair. An opening quote, and a closing quote. In the example below we can see on line 4 that the closing quote to match the opening " is missing. Sometimes the syntax highlighting will offer us a clue.
|
|