C Tutorial menu

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?

AreaExamples
Operating systemsLinux kernel, parts of Windows and macOS
Embedded systemsMicrowaves, cars, ATMs, Arduino boards
DatabasesMySQL, SQLite, PostgreSQL
Other languagesPython's main interpreter is written in C

How a C Program Runs

  1. You write code in a file ending with .c, for example hello.c.
  2. A compiler such as gcc checks the code and turns it into a program (an executable).
  3. You run the executable.

Example

text
gcc hello.c -o hello
./hello

On 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?

Learn C with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp