No history yet

Introduction to C

The Foundation of Modern Code

Before there was Python, Java, or JavaScript, there was C. Developed in the early 1970s by Dennis Ritchie at Bell Labs, C was designed to build operating systems. The UNIX operating system, which is the ancestor of modern systems like macOS and Linux, was written almost entirely in C.

This close link to the machine's hardware is what makes C special. It's considered a middle-level language. It's not as abstract as a high-level language like Python, but it's much easier for humans to read and write than the low-level assembly code that computers directly understand.

Lesson image

This unique position allows C programmers to write highly efficient code that runs incredibly fast. That's why C is still used today for performance-critical tasks, like in game engines, embedded systems (the tiny computers in your car or microwave), and the core components of other programming languages.

Anatomy of a C Program

At first glance, a C program can look a little cryptic. But every piece has a specific purpose. Let's break down the classic "Hello, World!" program, which is often the very first program anyone writes in a new language.

// This is the "Hello, World!" program in C.

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

Let's examine this code line by line. It might seem like a lot just to print a simple message, but these elements are the fundamental building blocks of every C application.

This structure is the basic template for all C programs. Let's dig into what each part does.

The Preprocessor and the Main Function

The first line, #include <stdio.h>, is a preprocessor directive. Before your code is compiled into an executable program, a special program called the preprocessor runs. It scans your code for lines starting with a hash symbol (#).

The #include directive tells the preprocessor to find a file and copy its entire contents into your program. In this case, it includes stdio.h, which stands for standard input/output header. This file contains the information the compiler needs to understand functions like printf().

Next is int main(void). This is the main function. Every C program must have a function named main. It serves as the starting point for your program's execution. When you run your program, the code inside main's curly braces {} is what gets executed, from top to bottom.

The int before main specifies that the function returns an integer value when it finishes. The return 0; line at the end does just that. By convention, returning 0 signals to the operating system that the program ran successfully without any errors.

Printing to the Screen

The line that does the actual work is printf("Hello, World!\n");.

printf

verb

A standard library function in C used to send formatted output to the screen (standard output).

The printf() function takes the text you want to display, enclosed in double quotes, and prints it to the console. The strange \n at the end of the string is an escape sequence. It represents the newline character, which moves the cursor down to the next line after the text is printed. Finally, the semicolon ; at the end marks the end of the statement. In C, nearly every statement must end with a semicolon.

Quiz Questions 1/6

What was the primary purpose for which the C language was originally developed in the 1970s?

Quiz Questions 2/6

In a C program, what is the role of the main function?