C Introduction
C is a fast, simple and powerful programming language that has been in use for more than 50 years.
What is C?
- C was created by Dennis Ritchie at Bell Labs in 1972
- It was first used to write the Unix operating system
- C is a compiled language: your code is turned into a program the computer can run directly
- C is close to the hardware, so it gives you direct control over memory
Where is C Used?
| Area | Examples |
|---|---|
| Operating systems | Linux kernel, parts of Windows and macOS |
| Embedded systems | Microwaves, cars, ATMs, Arduino boards |
| Databases | MySQL, SQLite, PostgreSQL |
| Other languages | Python's main interpreter is written in C |
How a C Program Runs
- You write code in a file ending with
.c, for examplehello.c. - A compiler such as
gccchecks the code and turns it into a program (an executable). - You run the executable.
Example
text
gcc hello.c -o hello
./helloOn Windows the program is called hello.exe, and you run it with hello.
Setting Up
You need two things:
- A compiler: GCC. On Windows install it with MSYS2 or MinGW-w64; on Linux run
sudo apt install gcc; on macOS install the Xcode command line tools - An editor: Visual Studio Code with the C/C++ extension, or Code::Blocks, which comes with a compiler built in
If you just want to practise quickly, an online C compiler works in the browser, but installing GCC teaches you how real projects are built.
Your First Program
Save this as hello.c:
Example
c
#include <stdio.h>
int main(void) {
printf("Assalam o Alaikum!\n");
printf("This is my first C program.\n");
return 0;
}Output
Assalam o Alaikum! This is my first C program.
C Can Calculate
Example
c
#include <stdio.h>
int main(void) {
int rent = 25000;
int months = 12;
printf("Yearly rent: %d\n", rent * months);
return 0;
}Output
Yearly rent: 300000
The %d inside the text is replaced by the number. You will learn more about this in the next chapter.
Exercise
Exercise
What does a C compiler do?
