C Programming For Beginners

Learn C in ten easy steps on Windows, Mac OS X or Linux

Last updated 2022-01-10 | 4.5

- Master C programming concepts from the ground up
- Use the source code examples to learn step-by-step
- Understand the special features of C: pointers
- header files
- null-terminated strings
- buffers
- IO

What you'll learn

Master C programming concepts from the ground up
Use the source code examples to learn step-by-step
Understand the special features of C: pointers
header files
null-terminated strings
buffers
IO
Videos explain everything in minute detail
Read the supplied eBook
The Little Book Of C
to explore the topics in even more depth
Test your understanding with end-of-section quizzes

* Requirements

* A C compiler and code editor (both are available free)

Description

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:

  • The fundamentals of programming – from the ground up
  • How to program on a Mac or on Windows
  • The nitty-gritty details of the C language
  • Advanced topics such as memory allocation, the stack and heap, and binary file IO

Who should take the course

  • Beginners – if you've never coded before, you can learn C step by step
  • Programmers switching to C from some other language such as Java, Ruby or Python
  • Cross-platform developers – there are C compilers for all major operating systems
  • Anyone who needs to program C++ or Objective-C. The C language is the place to start

Who this course is for:

  • Newcomers to programming
  • Programmers of other languages who want a fast way into C
  • Anyone who needs to master C as a basis for using C++ or Objective-C

Course content

10 sections • 86 lectures

Welcome to the course Preview 04:08

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.

C Editors and IDEs Preview 01:59

In order to write C programs you will need a suitable editor or IDE (Integrated Development Environment). Here are a few suggestions…

FAQ - Read This First! Preview 29 pages

This is a PDF document containing answers to a number of common questions that have been asked by students. Please download and read this BEFORE asking any questions in one of the discussion threads. 

Please note that I cannot deal with technical issues relating to CodeLite. You should post question on CodeLite in the CodeLite forum: https://forums.codelite.org/

Install CodeLite (Windows or Mac) Preview 02:12

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.

Install a C compiler on a Mac Preview 03:18

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.

Introduction to CodeLite Preview 05:18

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.

Using C++Builder Preview 03:43

Embarcadero’s C++Builder is a great environment for both C and C++ programming on Windows – and the free edition has everything you need to follow this course.

Importing C Projects into C++Builder Preview 04:43

Let’s see how to use C++Builder to create a project from the C code in the source code archive.

Programming C with NetBeans Preview 02:12

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.

Importing Projects Into NetBeans Preview 05:55

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.

Compiling C programs at the system prompt Preview 09:39

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.

Visual Studio 2019 Preview 00:05

If you are a Visual Studio 2019 user, be sure to download and read this document before creating a project.

Using Visual Studio Preview 06:31

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 -- (download) Preview 96 pages

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.

C Source Code Archive Preview 00:21

C Basics

Getting ready for C programming

A first program - Hello world Preview 06:06

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.

Arguments and return values Preview 04:10

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.

Passing commandline arguments to your program Preview 05:13

Here I explain how to pass arguments from the commandline (or Terminal) to your program.

How to open a command prompt on Windows or OS X Preview 04:35

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.

printf Preview 05:17

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.

Comments Preview 01:44

You can document your code with comments that are ignored by the compiler. Here I explain two types of comment.

C Programs

Fundamental features of a C program

Variables and types Preview 03:47

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.

Integers and floating point numbers Preview 04:32

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!

Constants Preview 03:42

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.

More about constants Preview 04:44

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.

Naming conventions Preview 05:29

What should you call your variables and constants? Here I consider some of the naming conventions adopted by man C programmers.

Variables and constants

How to declare and use variables and constants

Operators – equality and assignment Preview 02:47

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.

Operators – tests and comparisons Preview 03:31

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.

Compound assignment operators Preview 03:35

Some assignment operators in C perform a calculation prior to assigning the result to a variable. These are called ‘compound assignment operators’.

Increment and decrement operators Preview 03:52

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!

if and else tests Preview 06:32

There are times when you need to take different actions according to some test condition. Here I explain how to use if..else tests.

Problems with gets() and fgets() Preview 07:46

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.

Input, buffers and flushing Preview 09:32

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.

My own line-reading function Preview 08:41

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.

Logical Operators Preview 05:00

If you need to chain together conditions when making tests, you need to use C’s ‘logical operators’.

Operators and tests

Testing and assigning values

Functions Preview 09:26

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.

Arguments Preview 06:19

You can pass data to functions are arguments that are assigned to ‘named parameters’. Here I explain the nitty-gritty details of arguments .

Switch statements Preview 06:55

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.

Switch statements in more detail Preview 05:16

In this lesson I look at more examples of switch statements, including some options that are only available with some C compilers.

Functions and switch

Declaring functions and making multi-part tests with switch statements

Arrays Preview 03:26

Arrays are sequential collections. You can use arrays to store lists of chars, ints and other types of data. Here I explain the fundamentals.

Initializing arrays Preview 06:36

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.

‘while’ loops Preview 04:26

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.

‘do..while’ loops Preview 02:53

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.

break Preview 05:19

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.

break and continue Preview 05:18

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’.

Multidimensional arrays Preview 07:09

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.

Arrays and loops

Creating and iterating over sequential lists

Strings, pointers and addresses Preview 03:50

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.

Null-terminated strings Preview 04:11

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.

Char arrays and pointers Preview 07:49

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.

Arrays, pointers and assignment Preview 06:06

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.

Strings and functions, stack and heap Preview 06:58

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.

String functions Preview 09:39

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.

char functions Preview 03:27

There are also functions that let you analyse individual characters in order to determine to which category each char belongs.

chars and strings Preview 02:19

What is the difference between ‘x’ and “x”? They may look almost identical but, in fact, they are completely different – as this lesson explains.

Pointers and Strings

Strings and memory addresses

structs Preview 05:44

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.

typedef Preview 03:10

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 Preview 07:46

Enums let you created groups of named constants that can help to document your code. Here I explain their value – and their limitations.

Header files Preview 08:12

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.

A custom header file Preview 02:26

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 Preview 04:15

‘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.

Scope and external files Preview 02:45

What is the scope of functions declared in external files – that is, functions that are in different files but the same project?

Static functions and variables Preview 06:03

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.

Compiling from the commandline Preview 06:59

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.

Structs, enums, headers and scope

Delving deeper into C

Opening and closing files Preview 03:11

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.

File modes Preview 02:44

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.

Reading and writing a text file Preview 05:05

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.

Counting lines in a text file Preview 04:17

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.

Search in a text file Preview 04:03

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.

Files

Opening and closing files for reading and writing

Binary files Preview 05:30

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.

Allocating and freeing memory Preview 05:54

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.

Types and type casts Preview 07:48

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’.

Creating a CD database Preview 02:59

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.

Saving and loading records in a binary file Preview 07:17

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.

Adding records to a binary file Preview 04:54

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.

Modifying records in a binary file Preview 05:39

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.

Memory and pointers

More on how C deals with pointers and memory allocation

And finally… Preview 02:00

We’ve covered a lot of ground in the course on C programming. Now it’s time to move on…