Getting Started with C
Start with a brief history of C and discover how to write the ‘‘Hello World’’ program in C.
We'll cover the following...
What is C?
C is a high-level programming language that was developed by Dennis Ritchie at Bell Labs in the early 1970s. It provides fine-grained control over hardware interaction, allowing for highly optimized code. Because of this reason, it has always been popular for systems programming.
It’s been the language of choice for writing operating systems. Unix was one of the first operating systems to be rewritten in C (initially, it was written in assembly language). The core codes of Microsoft Windows, Mac OS X, and GNU/Linux are also written in C, although these operating systems may also use other languages for various components.
C has found other uses as well. It’s useful for high performance computing. Compilers and interpreters for several high-level languages like Perl, PHP, Python, R, MATLAB, Mathematica, etc., are also written in C.
A program in C
Here’s an example of a C program:
#include <stdio.h>int main(void) {printf("Hello world");return 0;}
The above is a bare-bones C program that simply writes the text “hello world” to the screen. As you can see, it’s not scary-looking code. The code that does the work here is line 4. All the other stuff you can think of as standard boilerplate C code you need for any C program to run.
We’ll examine the details in this code later on. For now, you should just know three things about this code. First, lines 3–6 define what’s called the main
function. The main
function is essential for a C program to run. The pair of opening ({
) and closing (}
) curly brackets here includes all the statements that will be executed when the program is run. Second, the stuff inside the round brackets on line 3 (the void
) is optional. We could leave it out, and your program would still run. Same for the statement return 0;
on line 5. We include these here because it’s a recommended practice. Third, line 1 imports one of the standard C libraries, stdio.h
(standard input/output).
With this, let’s take a brief detour to see how similar programs in other programming languages are slightly less verbose. Don’t let this discourage you, though; C has its strengths, which we’ll explore next.
The bigger picture
As a comparison, here is a similar program in Python that prints the same text to the screen:
print("Hello world")
Here it is in R:
cat("Hello world")
In MATLAB/Octave:
disp('Hello world')
As you can see, what differentiates the given C code from similar codes in other languages is that in C, we have to import the stdio.h
library and define the main
function explicitly. Not such a big deal. The other difference is that the name of the standard function to write stuff to the screen differs in each language. In C, it is printf
, in Python it is print
, in R it is cat
, and in MATLAB/Octave it is disp
. Again, no big deal.
One of the things we want you to take away from this boot camp is that with few exceptions, all (procedural) programming languages share many fundamental concepts, such as the use of procedures (or functions) to perform tasks, but there are notable differences that can impact how we write and understand code in each language such as:
- Names of standard functions are different across languages.
- The rules of each language (known as syntax) are different.
- Functionality included in standard libraries can vary significantly.
- Language-specific features present in one language may not be present in others.
However, the foundational skills you develop in C, such as organizing code and thinking logically, are transferable and will help you learn other languages more quickly.
Next, we’ll explore some of the strengths and weaknesses of using C. Before we do, it’s worth noting that this is a beginner-friendly course, though some prior exposure to programming will be helpful for deeper understanding and context.