Huw Collingbourne

C is a general-purpose language used for programming everything from desktop utilities to entire operating systems, making it one of the most important programming languages to know. As one of the main languages used for programming microcontrollers in hardware devices and robotics, you can expect C to be popular in the dev world for quite a while.

History of C language

At first sight, it might seem odd that so many programmers choose C. After all, it is quite an old language – Dennis Ritchie first developed it in the late ’60s and early ’70s. And unlike the more modern high-level languages, (such as Java, C#, and Python), C doesn’t even have object-oriented capabilities. 

C Programming For Beginners

Last Updated August 2019

  • 76 lectures
  • All Levels
4.4 (3,786)

Learn C in ten easy steps on Windows, Mac OS X or Linux | By Huw Collingbourne

Explore Course

Still, the relative simplicity of C is one of its greatest strengths. As a result, a skilled programmer can write faster, smaller, more efficient programs in C than would be possible in a language such as Java or Python. For speed-critical applications, a microsecond can mean the difference between success and failure. In embedded systems, this may be vital. Bear in mind that C is simpler than C++ which is an ‘object oriented’ extension of C, thus I recommend learning C before C++.

What is C? A Hello World example

MacBook Pro on brown wooden table

Let’s take a look at a short and simple C program. By tradition, the first program to write when learning a new language is one that displays the text “Hello world.” Here’s what the “Hello World” code looks like when written in C: 

#include <stdio.h> main() {  printf("hello world\n"); }

This program uses (that is, it ‘includes’) code from the C ‘standard code library,’ stdio, using this statement: 

#include <stdio.h>

A code library is a collection of pre-written code aimed at performing certain related operations. Here stdio.h is a library that contains functions to deal with input and output. An example of ‘input’ would be some text that is entered at the system prompt. An example of ‘output’ would be text displayed at the prompt. Here the printf() function displays the text “Hello world” followed by a new line “\n”:

printf("hello world\n");

Header Files 

You may have noticed that the name of the code library, stdio.h, has the extension “.h”. This shows that it is a “header file.” A header file contains (among other things) definitions of the various functions inside a code library. When a header file is ‘included,’ its contents are inserted (just as though you had copied and pasted them) into your source code when you compile the program. 

Compiling C programs 

A C compiler (and an associated tool called a ‘linker’) is the program that translates your source code (the text you write in an editor) into machine code. It is the machine code that will be executed by your operating system when the program runs. C compilers are available for all major operating systems, including Windows, macOS, and Linux. 

Many modern languages, such as Java and C# compile source code into an ‘intermediate code’ or ‘bytecode’ rather than into machine code. A software program runs intermediate code, while the machine code that C compiles is run directly by the computer hardware. That is one reason why C 

programs can be so efficient. They communicate directly with the hardware rather than being run by an intermediate piece of software. 

If you are using an IDE (Integrated Development Environment) such as Visual Studio or CodeLite, it may not be obvious how your source code is translated into machine code because the IDE automates the process. Compiling a C program requires three different tools: the pre-processor, the compiler, and the linker. This is what they do: 

Step 1: The pre-processor 

The preprocessor is a tool that processes special commands preceded by a hash # character. For example, #include is a pre-processor command to include header files. 

Step 2: The compiler 

The compiler translates the source code into an intermediate code format called ‘object code.’ This object code is saved into several separately compiled files called ‘object files.’

Step 3: The linker 

Finally, all the separate files are combined in a process called ‘linking.’ This creates the final executable program, which contains machine code. The linker additionally ‘links in’ code from any object libraries required, for example, the code for the printf() function.

How does C work?

When you write a C program, you have to start with a ‘main function.’ A function is a named block of code. The main function is the first bit of code that runs when the program itself runs. The main function begins with its name (main), followed by a pair of parentheses. The code that you want to run is enclosed between a pair of curly brackets. Here is the simplest possible main function. I have not yet written any code inside the curly brackets: 

main() { 
}

In the HelloWorld program I showed earlier, I added one line of code to the main function. This code calls printf() — a function provided as standard by C – to print the string (the piece of text) between double-quotes: 

main() { 
 printf("hello world\n"); 
}

When we talk about ‘printing’ a string, we usually don’t mean that the string is printed on paper. We mean that it is ‘printed’ to the standard output. Many years ago, the standard output might have been a printer. These days it is more likely to be the computer screen. 

The anatomy of a C program 

Every C program is made up of elements such as keywords, functions, and variables. In this section, I’ll quickly summarize the main elements of a C program. 

Hello World revisited 

Let’s take a closer look at the simple “Hello world” program that I showed earlier. This time I have numbered the separate pieces of code. You will find explanations of each number below: 

#include1<stdio.h>2 
main3()4{5 
 printf6("hello world\n"7);8 
}

1. #include is a pre-processor directive. Here it causes the contents of the file stdio.h to be included in the current program. 

2. The pointy brackets, < >, around the filename tell the compiler to search for that file (here the ‘header’ file stdio.h) in the ‘C system’ directories. 

3. main is the name of the function that is run when the program starts. 

4. The empty pair of parentheses after main show that this is a function with no arguments. 

5. The curly brackets { } delimit code blocks. Here they enclose the code of the main function.

6. printf() is a function defined in the file stdio.h

7. “hello world\n” is a string of characters, passed between the parentheses to the printf() function; “\n” is the ‘newline’ character. 

8. The semicolon terminates the statement. 

Strings 

People who program in other languages are often surprised to discover that C has no string data type. In C, a string is a sequence of characters terminated by a null character, ‘\0’. This is because, as far as the computer hardware is concerned, numbers and characters are the only fundamental types. 

In C, a string is just an array – a linear list – of characters. That array is stored at an address in memory. When you are working with strings in C, you need to access the starting address and then count over all the characters until you find a null. Many other languages hide the details of dealing with memory addresses. In C, you will regularly need to access data that is stored at an address in memory. To do that, you need to understand pointers. 

Pointers 

A pointer variable in C is an identifier such as aPtr, which stores the address of a piece of data. The address is simply a number. That number indicates a location in memory. So, for example, the string “Hello world” might be stored at the memory location 2686746. The programmer could create that data as an array of characters like this: 

char str1[] = "Hello world";

The declaration above shows that the variable named str1 is an array (because it is declared with square brackets) of the char data type. The characters of the string “Hello world” are placed into that array, and they are stored at an address in memory. If I want a reference to that address, I need to store it in a pointer variable. A pointer variable is declared using a ‘star’ or asterisk like this: 

char *aPtr;

I can store the address of str1 in the pointer variable aPtr like this: 

aPtr = str1;

Pointers are one of the most difficult things to master in C. In many more modern languages such as Java, C#, and Python, you may never need to use pointers. Those languages hide pointers from the programmer. C does not hide pointers. A C programmer must use pointers all the time. 

A novice C programmer may find this quite challenging. However, pointers give an experienced C programmer the ability to access memory very quickly for extreme efficiency. The efficiency of C is the main reason why it is still preferred as a systems programming language. C is a great choice for writing operating systems and device drivers or when programming microcontrollers and hardware devices.

Summary 

I hope you now have some idea why C is an extremely powerful language to learn. It does not have the object-oriented features that are commonly found in other mainstream languages. But it does give the programmer a great deal of control over memory and hardware. That is why C is often called a low-level language. It works in a way that is very close to the way the computer itself works. And that is the reason why C continues to be one of the most important computer programming languages.

To learn more about C programming, check out our tutorials on C Data Types, C Syntax, or For Loops in C. For a more hands-on experience, check out our list on C Programming Projects. Whichever you decide to try first, you can always rely on the background knowledge you’ve learned here in your path to mastering C.

Page Last Updated: July 2021

Top courses in C (programming language)

C Programming the Basics
Frank Anemaet
4.5 (49)
C Programming For Beginners - Master the C Language
Tim Buchalka's Learn Programming Academy, Jason Fedin
4.4 (21,789)
Bestseller
C Programming from Scratch to Master
SACHIN NANDHA SABARISH J
4.7 (22)
Highest Rated
C PROGRAMMING made easy
Meganadha Reddy
4.5 (1,528)
Advanced C Programming Course
Tim Buchalka's Learn Programming Academy, Jason Fedin
4.5 (1,713)
Build Undetectable Malware Using C Language: Ethical Hacking
Aleksa Tamburkovski, Joe Parys
4.7 (851)
Highest Rated
C Programming Language: The Ultimate Guide for Beginners
Aptuts Online Coaching, Prashant Nayak
4.5 (168)
C Programming On Windows For Beginners
Daniel McCarthy
4.6 (276)
Advanced C Programming: Pointers
Huw Collingbourne
4.7 (2,362)
Highest Rated

More C (programming language) Courses

C (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo

Courses by Huw Collingbourne

Ruby Programming for Beginners
Huw Collingbourne
4 (726)
Learn C# Programming (In Ten Easy Steps)
Huw Collingbourne
4.6 (697)
Learn To Program with Pascal
Huw Collingbourne
4.3 (603)
Bestseller
Break Into The Programming Business
Huw Collingbourne
3.6 (67)
How to teach an online course
Huw Collingbourne
5 (289)
C Programming For Beginners
Huw Collingbourne
4.4 (3,786)
Java Programming – the Master Course
Huw Collingbourne
4.5 (277)
Ruby For Programmers
Huw Collingbourne
4.6 (255)
Advanced C Programming: Pointers
Huw Collingbourne
4.7 (2,362)
Highest Rated

Courses by Huw Collingbourne