




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
learn c clearly with c in linux on raspberry pi
Typology: Essays (university)
1 / 316
This page cannot be seen from the preview
Don't miss anything!





























































































By Andrew Johnson
Andrew Johnson grew up in Yorkshire, England and graduated from Lancaster University in 1986 with a degree in Computer Science and Physics. Following this, he mainly worked in Software Engineering for about 20 years, in the fields of telecommunications, process control and embedded systems. He has also worked full and part time in lecturing and tutoring and assessing (in Adult Education).
Since 2012, he has followed Raspberry Pi developments and worked on 2 small personal Pi projects – an aircraft tracker and a simple Python-Based media kiosk project.
In his spare time, he has widely researched a number of "alternative knowledge" topics which he has written on his website - www.checktheevidence.com. He can be contacted via that site or through [email protected].
This guide attempts to introduce the 'C' Programming Language to the novice programmer. Some readers will perhaps be more familiar with Java, Javascript, PHP or even C# - all of which have a number of keyword and syntactic features which seem to have been borrowed or derived from C. (As an aside, it is highly likely that compilers and interpreters for these languages were originally written in C or C++.)
You should find the examples easy to digest and when I originally delivered what is in this guide over 20-weeks of 2-hour evening classes (i.e. about 40 hours of study and practice), it seemed to be well-received and helped a number of people learn programming for the first time, although C is not normally considered a beginner’s programming language.
This guide will not teach you everything you need to know about C programming, nor programming in general. Neither will it teach you everything you need to know about Linux or Raspberry Pi’s. It is almost entirely devoted to teaching you the fundamentals of the C language, using Linux and/or
Raspberry Pi as a platform for doing this. The 2nd^ half of this guide does go into some of the more advanced features of the language, however.
This guide should be most useful to those who are doing lower-level programming – such as working with embedded systems, micro controllers or similar architectures. Similarly, anyone wanting to develop things like Device Drivers or work with USB handling software or networking and communications software should also find this guide of use. C has been in use in Real Time control applications since the late 1970's and continues to be used today as the language of choice.
The 'C' Language "rose to fame" in the 1980’s, having been used in the
The UNIX operating system was developed in parallel with the C programming language. Now, of course, Linux has been developed to be a working clone of UNIX, hence C can essentially be considered a "native" programming language for Linux.
'C' had its beginnings in a language called BCPL, which was introduced in the 1960's. From this, a language called 'B' was developed and this subsequently became 'C,' written by Dennis Kernighan at Bell Labs in 1972. Later in the 1970's 'C' became more widely used and an important milestone in the language's history was in 1978 when Brian Kernighan and Dennis Ritchie published a 'C' Language Reference Manual. This eventually became the recognised standard for the language, but was not official.
By 1988, the language was being so widely used that ANSI produced a standard for the language - largely based on Kernighan and Ritchie's 2nd edition of the reference manual. All 'C' compilers which are said to be ANSI compliant adhere to the standards laid down by ANSI in 1988.
The 'C' philosophy is always "the programmer knows what he / she is doing" , and there are few safeguards for the careless programmer. The language's attitude is much different than BASIC or some of the sophisticated newer languages such as Java, Python and C#. It allows you to write illegal and nonsensical code. Its syntax and type checking are weak, though default settings in modern compilers enforce more strict programming options. The main advantage of C is that you have a great deal of flexibility and, for instance, access to machine hardware is often trivially simple compared to other languages. Also, 'C' compilers can generate compact and well-optimised code which is ideal for small scale applications, but also useful for larger ones where you just need "that bit more" from your system. Time critical applications can often still benefit from being coded in C, not just because compiled code can run hundreds of times faster than interpreted code, but also because C programs can often "bypass" certain operating system features which compromise the
performance of a program/application.
Some of the example programs may seem pointless or unrealistic - in such cases, they are written to illustrate features of the language rather than being useful programs in their own right.
You can of course download all the sample programs and compile and run them, but you may find you will learn the language better if you actually type them (as was more common place when this guide was originally created in the early 1990’s). If you type and compile the programs yourself, you will learn more about what the error messages mean and you will get better at spotting errors in your code, when you start to write some!
When I originally began programming, back in 1981 on a Sinclair ZX81, I was motivated to write a simple game. A similar thing happened a year later when I got the legendary Spectrum (16K model). In this guide, then, we will adopt a similar "plan". We will be building up our knowledge to write a simple "code cracking" game – known to many as "Mastermind".
Code Cracking Game – "Mastermind"
This is a game I used to enjoy playing as a child – I even had a pocket electronic version. A sequence of digits (in the range 0 – 9) is set as a "secret code" (it is normally set by a human – the code setter, but we'll get the computer to do it). The "code breaker" then tries to guess the code as quickly as possible. He or she makes guesses by placing them on a board – typically, the user has 10 attempts at guessing the code before the game is over. The "code setter" then "marks" the guess according to certain rules:-
If, in the guess, there is a certain number which is also in the code, but it is in a DIFFERENT position, a WHITE peg is awarded.
If, in the guess, there is a certain number which is also in the code,
This is what our finished, running game will look like.
For the purpose of introducing some of the topics for this guide, we are going to consider how we might write an example Command Line/Console program. This program will be called the Sales Logger and Monitor and its function is outlined below:-
The Sales Logger and Monitor (SLM) will be a program which will carry out the following tasks:-
Store and maintain a list of customers, including their address and telephone numbers. Store and maintain a list of items purchased by each customer including details of:- Item purchased (code number). Cost of item. Number of Items. Date of Purchase.
Allow inspection of the list of customers showing their details.
Though this sort of thing is something of an artificial task, which could probably be achieved with an existing Database or Spreadsheet Application, using either of these would not help us to learn more 'C' programming techniques!
Not all the examples will refer to this SLM project – it will just be a "running theme," mixed in with other discussion. We will also cover many C language features which aren’t used in SLM.
Appendices list some very simple ideas for programs that you can write – based on the examples given in the guide. No solutions are given.
Some of the longer programs used in this guide are posted on this Blog: http://chereprogramming.blogspot.co.uk/. You can then copy and paste source code from there to save you having to type it all out. Of course, you can leave comments there too, if you wish.
If you can master what is in this guide and you understand what you have done, you will have a solid basis for developing an understanding the concepts involved in the most commonly used methods in what is called procedural computer programming. A logical next step then would be to develop an understanding of object-oriented computer programming, which is used inherently and throughout more modern programming languages such as C++, Java, Python and even in variants of BASIC.
specified, before being used. In most versions of BASIC, both the declaration and type specification are optional. With BASIC, many of the more advanced features of the language (such as those mentioned above) may be implemented in a run time support module or library. Such a library can often be quite large compared to the compiled version of a short program. In C, run time libraries are typically smaller and so the total amount of storage required to run the finished program can often be significantly smaller. C generally handles things like pointers and structures more elegantly than BASIC, though there is much less difference between contemporary versions of BASIC and C.
Comparison to Java
If you are a Java programmer, you will find much of C has a familiar look. Many features of Java were based on those of C++ (it could be argued that Java is almost a cross between C++ and perhaps Basic or Pascal). Like BASIC, Java can be both compiled and interpreted. Like C/C++, Java is designed to be a portable language. However, Java is object-oriented whereas C is procedural only.
General Note
It is a fair bet that many or most of the languages which "came after" C had their interpreters or compilers written in C. If you look at the PHP web- programming language, you will yet again see syntax commonalities with C.
Use of Structures Use of Files Use of Dynamic Memory Allocation Some system programming
Additionally, we will briefly cover topics such as
Use of Graphics Use of Sound Cross Compilation
The first 3 topics should be applicable to most, if not all implementations of 'C' - including those in different environments such as Windows and Mac OS and parts of the rest of the topics will probably be applicable, although details differ from system to system.
same ends and one may be more efficient or suitable than another.
The Programming Process involves
Considering a problem. Breaking up the problem into small pieces. Considering how to express the solution to each part of the problem in the form of a Computer Program.
A Program
Can be split into 2 main parts:-
Instructions - Code Information - Data
In solving the problem, we have to consider what Information and Instructions are required.
Can be categorised into 3 main types:-
You will see different examples of each of these types on instructions in this guide, although the Jump/Branch is often done implicitly, by structuring the program in a certain way.
Can take many different forms. e.g.
Simple whole number values Floating point values (with a decimal point) Text / Strings / Characters Graphics Sound
'C' is just another programming language and it has its own particular way of expressing Code & Data.
Once a program has been written, it has to be Compiled ("converted") before being run. If an error is seen, the program has to be, terminated, corrected, re-compiled and run from the beginning. The conversion process in only done once, unless changes are made to the program.
Once a program has been written, it can be run straight away. If an error occurs, the program may stop completely or just stop temporarily, following an "alert" message of some kind. The program can be interrupted, corrected and the program can continue running from the point it was stopped at.
The "conversion" process happens every time the program runs - this slows things down, although early in the 21st century, computer processors (CPU’s) are often thousands of times faster than they were near the end of the 20th century.