Article Summary
C programming is a general-purpose language used to build everything from operating systems to embedded hardware systems. This article covers C's history, core structure, how compilation works, and key concepts like pointers and strings. You'll gain a solid foundation in what C programming is and why it remains essential for systems-level development.
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.
Recommended Udemy 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
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 worldn"); }
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 worldn");
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 worldn");
}
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 worldn"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 worldn” 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, ‘