C Introduction
C is a general-purpose programming language created by Dennis Ritchie at Bell Labs in the early 1970s. It was designed to write the Unix operating system, and more than fifty years later it is still one of the most widely used languages in the world. When you open a web browser, run Python, or plug in a USB device, there is almost certainly C code doing work underneath.
C is often called a low-level high-level language. You write readable code with variables, loops and functions, but you also get direct access to memory through pointers, and the compiler turns your program into very fast machine code with almost no hidden overhead.
What is C used for?
- Operating system kernels (Linux, the core of Windows and macOS)
- Embedded systems: microcontrollers, cars, routers, medical devices
- Databases such as SQLite, PostgreSQL and Redis
- Language runtimes: the reference Python interpreter (CPython), Ruby, Lua and PHP are written in C
- High-performance libraries for graphics, compression, cryptography and networking
Why learn C?
| Reason | What it means for you |
|---|---|
| Small language | The core fits in your head: a few dozen keywords and a compact standard library |
| Close to the hardware | You learn how memory, addresses and bytes really work |
| Fast | Compiled C runs at nearly the speed of hand-written assembly |
| Portable | A C compiler exists for practically every processor ever made |
| Foundation | C++, Java, C#, JavaScript and Go all borrow C's syntax — learning C makes them easier |
Note: C gives you a lot of freedom and very few safety nets. If you read past the end of an array or forget to free memory, the compiler usually will not stop you. This course points out these traps as they come up.
C vs. C++
C++ started as "C with classes" and is almost a superset of C. Plain C has no classes, no templates, no exceptions and no std::string. In exchange it is simpler, compiles quickly, and its rules are easier to predict. Many projects still choose C on purpose for exactly that reason.
Your first C program
Every C program starts running in a function called main. Press ▶ Run to compile and run the program below, then change the text and run it again.
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}Hello, World!
Here is what each part does:
#include <stdio.h>pulls in the standard input/output header, which declaresprintf.int main(void)defines the entry point.intsays it returns a whole number;voidsays it takes no arguments.printf("...\n");prints text.\nis a newline character.return 0;tells the operating system the program finished successfully.
Tip: In every playground on ExcelTAD you can edit the code and press Ctrl+Enter to run it.
Printing more than one line
Each printf call continues exactly where the previous one stopped, so you decide where the line breaks go by placing \n.
#include <stdio.h>
int main(void) {
printf("C is small.\n");
printf("C is fast.\n");
printf("C is ");
printf("everywhere!\n");
return 0;
}C is small.
C is fast.
C is everywhere!
How a C program is built
C is a compiled language. Before your program can run, a compiler (such as gcc or clang) translates the source file into an executable in a few stages:
- Preprocessing — lines starting with
#(like#include) are handled, pasting in header files. - Compiling — the C code is translated into machine instructions for your processor.
- Linking — your code is combined with library code (for example the real implementation of
printf) into one executable file.
The playgrounds on this site send your code to a real GCC compiler (C17 standard), run the result, and show you the output.
Test yourself
Which function does every C program start running from?
- start()
- main()
- printf()
- run()
Why does a program need #include <stdio.h> before calling printf?
- It makes the program faster
- The header declares
printfso the compiler knows about it - It starts the main function
- It is required in every C file, even without output
What does return 0; at the end of main usually mean?
- The program crashed
- The program printed nothing
- The program finished successfully
- Restart the program