The fastest, easiest way to learn to program C on a Mac or Windows. This course will teach you to program the C language from the ground up. You will learn everything from the very fundamentals of programming right through to the complexities of pointers, addresses and File IO. Maybe you've tried to master C before but failed. Or maybe you are new to C or new to programming. If so, this is the course for you!
C is one of the most important of all programming languages. It is used to program desktop applications, compilers, tools and utilities and even hardware devices. The C language is fast and efficient – but it can be hard to learn. Unless you use this course. This course begins with a gentle introduction to C but quickly moves on to explain some of its most confusing features: everything from C's 'scoping' rules to the curious connection between arrays and memory addresses. By the end of the course you will have a deep understanding both of the C language itself and also of the underlying 'architecture' of your computer.
What you will learn:
Who should take the course
C is an important cross-platform programming language. In this video, I provide a quick introduction to the language and how to learn it using this course.
In order to write C programs you will need a suitable editor or IDE (Integrated Development Environment). Here are a few suggestions…
This is a PDF document containing answers to a number of common questions that have been asked by students.
In this course I will generally use the free CodeLite C editor which is available for OS X, Windows and Linux. This video shows how to get CodeLite installed on your computer.
If you are using a Mac you may need to download some additional tools in order that an editor such as CodeLite is able to find a compiler to build and run your programs.
Let’s take a quick look at the features of the CodeLite editor – from syntax colouring to keyboard shortcuts. CodeLite makes light work of creating C projects on Windows and OS X.
Maybe you are already use the NetBeans IDE for Java programming. Or maybe you'd just like to use NetBeans as your C environment. Here I explain how to get up and running.
There are various ways of importing source code into NetBeans. Here I show a simple way of creating a NetBeans C project using the files from one of my sample projects.
You don't need to use an IDE at all. If you are having problems installing an IDE or if you'd prefer to use a simple text editor, you can do so – and compile your programs in a System or Terminal window.
If you are using Windows, Microsoft’s Visual Studio (even the free edition) may be used to create C projects. Here I explain how to do that.
The Little Book Of C is the course eBook. There is a chapter for each step of the course. Use the book, the source code and the videos together for a full understanding of the topics discussed.
Getting ready for C programming
Here I take a look at a simple program that just displays “Hello world” and discover that even a few lines of C code illustrate a number of important features of the C language.
Some “Hello world” programs are more complex than others. Here I look at a program that takes some data as ‘arguments’ and returns a value.
Here I explain how to pass arguments from the commandline (or Terminal) to your program.
If you don’t know how to open a command window on Windows or the Terminal on OS X and use it to run your programs, this lesson explains all.
The printf() function lets you display the output from your program. It’s a very useful function but must be used with care – as I explain here.
You can document your code with comments that are ignored by the compiler. Here I explain two types of comment.
Fundamental features of a C program
Variables are identifiers whose values may vary during the running of your program. This video explains the basics of variables and their types in C.
You may do calculations with both full numbers – integers – and fractional numbers – floating points. But be careful: the end results may not be what you expect!
If you want to create identifiers to store values that are not expected to change during the execution of a program, you can #define them.
Identifiers that are created using #define are often used as ‘constants’ – but, in fact, there is an alternative – using the keyword ‘const’. Here I explain the difference.
What should you call your variables and constants? Here I consider some of the naming conventions adopted by man C programmers.
How to declare and use variables and constants
There are two ‘equals’ operators in C – one uses a single equals sign to assign a value to a variable. Another uses two equals signs to test for equality. Here I explain the difference.
You will frequently need to make comparisons between one value and some other value. C has a number of ‘relational operators’ to help you do this.
Some assignment operators in C perform a calculation prior to assigning the result to a variable. These are called ‘compound assignment operators’.
You can use ++ and – to add and subtract 1 from a variable. But be careful – you can put these operators either before or after a variable and the position matters!
There are times when you need to take different actions according to some test condition. Here I explain how to use if..else tests.
It may seem easy to get input with gets() but this may cause problems. The fgets() function is a safer alternative – but that too may cause its own problems, as I explain here.
Sometimes you may have more data lurking in the dark corners of your computer’s than you are expecting. Here I explain some of the mysteries of buffers and why they need to be flushed.
Here I look at two possible ways of writing a function that safely reads in characters entered at the command prompt and also flushes any unneeded characters from the buffer.
If you need to chain together conditions when making tests, you need to use C’s ‘logical operators’.
Testing and assigning values
We’ve used functions from the very start of this course. In this lesson I explain more about what functions are and how they really work.
You can pass data to functions are arguments that are assigned to ‘named parameters’. Here I explain the nitty-gritty details of arguments .
There may be times when you need to take many different possible actions depending on the value of some variable. The switch statement can help out.
In this lesson I look at more examples of switch statements, including some options that are only available with some C compilers.
Declaring functions and making multi-part tests with switch statements
Arrays are sequential collections. You can use arrays to store lists of chars, ints and other types of data. Here I explain the fundamentals.
You can add elements to an array at the same time the array is declared. Here I show how to do this and I also explain how the results of calculations may change according to the ‘precedence’ of operators.
Sometimes you might want to run some code not for a predetermined number of times but for just as long as some condition remains true. You can use a ‘while’ loop to do this.
In some circumstances the code in a ‘while’ may never be run. If you want to ensure that your code is always run at least once, use a ‘do..while’ loop.
Sometimes it is useful to break out of a loop even if the loop’s test condition is not false. Here I explain how to use break in a ‘while’ or ‘for’ loop.
Sometimes you may want to break from a loop once but then continue running the loop afterwards. Here I explain the difference between ‘break’ and ‘continue’.
Arrays can have multiple dimensions to let you star arrays inside arrays. Here I explain how you can think of a two-dimensional array as being a bit like a spreadsheet with intersecting rows and columns.
Creating and iterating over sequential lists
In order to understand strings in C you need to understand how computer memory can be represented by ‘addresses’ and how pointer variables can refer to those addresses.
Many programming languages have a dedicated string data-type. Here we revise the essential features of C strings and explain the significance of its lack of a string type.
At first sight there may seem to be no difference between an array of chars such as char str and a char-pointer such as char *str. In fact the difference is profound and important.
Here I look at some more examples of using arrays and pointers and consider why you can assign to a pointer variable but not to an array name.
How do you return strings from functions And we also look at the importance of understanding the ‘stack and ‘heap’ in your computer’s memory.
C compilers come with ready-to-use string functions. Here I look at some of the traditional functions as well as some more modern alternatives.
There are also functions that let you analyse individual characters in order to determine to which category each char belongs.
What is the difference between ‘x’ and “x”? They may look almost identical but, in fact, they are completely different – as this lesson explains.
Strings and memory addresses
Let’s imagine that you want to create a catalogue of your CD collection in which each record contains a name, the artist name, the number of tacks and a user rating. Here I explain how structs can help.
C lets you define your own named types. This makes it possible to create type names for everything from an int to a string to a custom record or struct.
Enums let you created groups of named constants that can help to document your code. Here I explain their value – and their limitations.
What is the purpose of the ‘.h’ header files that most C programs include? Here I explain why header files are useful and how they are used during the compilation of your programs.
Here I look at an example of a header file that provides access to a set of functions and constants that I have written.
‘Scope’ defines the visibility of functions and variables to your code. Here I explain local and global scope and look at the scoping of two variables with the same name.
What is the scope of functions declared in external files – that is, functions that are in different files but the same project?
Sometimes you may want your functions to be ‘private’ – hidden from code in other files. Here I explain how static functions can do this, and I also explain static variables.
To understand better how the compiler and linker work and how the compiler may rely on information from header files, try compiling your projects at the system prompt.
Delving deeper into C
In this step we look at file operations. In this video I explain how to open and close disk files in order to save and load data to and from them.
When you open a file you can use a short string to indicate the file ‘mode’. A file mode may make a file available for reading, writing or appending in text or binary format.
Here I go through the code in a sample project to show how text can be saved to and loaded from a file, how the file contents can be erased and how the file itself can be deleted.
Once you’ve opened a text file you may want to do something with the text it contains. In this video I show how to count the number of lines in a file.
Now you know how to read and write text files you can write programs to process the text in a variety of ways – for example, to search for words in a file or encrypt its contents.
Opening and closing files for reading and writing
Not all files contain plain text. Some files may contain binary data – for example, if I were to save a CD database to disk, the data stored in each CD struct would have a binary representation. This video explains the basics.
Sometimes you need to allocate memory dynamically. But once you’ve finished with that memory you need to free it. This lesson gives an example of code that does this.
The C language provides a number of standard data types. Sometimes it is useful to treat one type as another type. In this lesson I explain the hows and whys of ‘type-casting’.
The final project in this course creates a database of CD structs that are saved in a binary file on disk. This video introduces you to this project.
Here I explain how to save a collection of records (structs) into a binary data file and how to calculate the number of records stored before allocating memory when reading them in again.
Here I explain how to create a new CD struct in memory and then append its data to the end of an existing binary file storing CD records.
Finally I show an example of how to find a record in a binary file and modify the data it contains. You can use the sample program as a basis for your own data-saving application.
More on how C deals with pointers and memory allocation
We’ve covered a lot of ground in the course on C programming. Now it’s time to move on…
Huw Collingbourne is the technology director at SapphireSteel Software, developers of programming tools for Microsoft Visual Studio. He is author of The Book Of Ruby from No Starch Press. He runs Bitwise Courses and teaches courses on a range of programming topics.
Huw has been a programmer for more than 30 years. He is a well-known technology writer in the UK. For over ten years he wrote the Delphi and Java programming column for PC Plus Magazine. He has also written numerous opinion and programming columns (including tutorials on C#, C++, Smalltalk and Ruby) for a number of computer magazines, such as Computer Shopper, PC Pro, and PC Plus.
In the 1980s he was a pop music journalist and interviewed most of the New Romantic stars, such as Duran Duran, Spandau Ballet, Adam Ant, Boy George, and Depeche Mode. He is now writing a series of New Romantic murder mysteries.
At various times Huw has been a magazine publisher, editor, and TV broadcaster. He has an MA in English from the University of Cambridge and holds a 2nd dan black belt in aikido, a martial art which he teaches in North Devon, UK. The aikido comes in useful when trying (usually unsuccessfully) to keep his Pyrenean Mountain Dogs under some semblance of control.