- Explain the history of C and where it is used today
- Compile a C program with
gccand run it - Understand what
#include <stdio.h>,int main(void)andreturn 0;mean - Use format specifiers and escape sequences with
printf
Your phone, a washing machine, a car's engine control unit and the router that brings the internet into your home all have programs running inside them. A surprising number of those programs are written in a language that is more than fifty years old — C. C is fast, compact and very close to the hardware. While learning it, you will see how a computer really works with memory.
A short history
C was created in 1972 by Dennis Ritchie at Bell Labs in the USA. He needed a language for writing the Unix operating system: assembly language was tied too closely to one particular computer, while the high-level languages of the time were too slow. C was the answer — a language almost as fast as assembly, yet one that could be moved to other computers.
- 1972C is born
Dennis Ritchie creates C at Bell Labs as a successor to the B language.
- 1973Unix is rewritten in C
The kernel of the Unix operating system is rewritten in C.
- 1978The K&R book
Brian Kernighan and Dennis Ritchie publish the book The C Programming Language.
- 1989The first standard: C89
The American National Standards Institute (ANSI) approves the first official C standard.
- 1999The C99 standard
Adds
//comments, thelong longtype and declaring variables inside aforloop. - 2011The C11 standard
A modern standard with support for multithreaded programs. C17 and C23 followed later.
Where is C used today?
- Operating systems: the kernels of Linux and Windows are largely written in C.
- Embedded systems: microcontrollers in washing machines, cars, medical devices and routers — places where memory is small and speed matters.
- Tools and other languages: the standard Python interpreter (CPython), the SQLite database and the Git version control system are written in C.
- A foundation for other languages: C++, Java, C# and JavaScript took their curly braces, semicolons and operators from C. That is why these languages will look familiar after C.
Your first program
Here is the classic first program. Save it in a file called hello.c:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}Hello, World!
#include <stdio.h>brings in the header file of the standard input/output library, where theprintffunction is declared.int main(void)is the main function: every C program starts running here.intmeans the function returns an integer, andvoidmeans it takes no parameters.- The curly braces
{ }enclose the function's body. Every statement ends with a semicolon (;). printfprints text on the screen, and\nmoves the cursor to a new line.return 0;tells the operating system that the program finished successfully.
From code to program: compiling
A computer cannot run the text in hello.c directly. C is a compiled language: first a compiler translates all the source code into machine code and saves it as an executable file. The most popular C compilers are GCC, Clang and Microsoft's MSVC.
- 1Write the code
Create a file with the
.cextension in any text editor or IDE (for example VS Code) and type the code. - 2Compile it
Type
gcc hello.c -o helloin a terminal. The compiler checks the code: if there are no mistakes, it creates an executable file calledhello; if there are, it shows the line number of each error. - 3Run it
Type
./helloon Linux and macOS, orhello.exeon Windows. Every time you change the code, you have to compile it again.
gcc hello.c -o hello
./helloHello, World!
-o hello sets the name of the resulting file.printf and format specifiers
printf can do more than print fixed text. Inside the text you place format specifiers — codes that start with % — and each of them is replaced by the next value after the comma. Don't worry about the variables yet — we will study them in detail in the next lesson.
| Specifier | Used for | Value | Output |
|---|---|---|---|
%d | integer (int) | 42 | 42 |
%f | fractional number (double, float) | 3.5 | 3.500000 |
%.2f | fractional number with 2 digits after the point | 3.14159 | 3.14 |
%c | one character (char) | 'A' | A |
%s | string (text) | "Baku" | Baku |
%% | the percent sign itself | – | % |
#include <stdio.h>
int main(void) {
int age = 14;
double height = 1.62;
char grade = 'A';
printf("Name: %s\n", "Leyla");
printf("Age: %d\n", age);
printf("Height: %f m\n", height);
printf("Height: %.2f m\n", height);
printf("Grade: %c\n", grade);
printf("Progress: 75%%\n");
return 0;
}Name: Leyla Age: 14 Height: 1.620000 m Height: 1.62 m Grade: A Progress: 75%
Notice that %f always prints six digits after the point, while %.2f rounds the number to two. To print the % sign itself, you write %%.
Some characters cannot be typed inside the quotes directly. They start with a backslash and are called escape sequences: \n — a new line, \t — a tab, \" — a double quote, \\ — the backslash itself.
#include <stdio.h>
int main(void) {
printf("Line 1\nLine 2\n");
printf("She said \"Hi!\"\n");
printf("Folder: C:\\code\n");
printf("Sum: %d\n", 2 + 3);
return 0;
}Line 1 Line 2 She said "Hi!" Folder: C:\code Sum: 5
printf can print several lines, and %d shows the result of the expression 2 + 3.Key points
- C was created in 1972 by Dennis Ritchie at Bell Labs to write Unix.
- C is compiled:
gcc hello.c -o helloturns source code into an executable file. - Every program starts in
int main(void);return 0;means success. #include <stdio.h>lets you useprintfand other input/output functions.- Format specifiers:
%d—int,%f—double,%.2f— two decimals,%c— a character,%s— a string.
Check yourself
10 questions. Every correct answer earns XP.