No history yet

Introduction to C Programming

A Quick Look Back

The C programming language has a rich history. It was developed in the early 1970s by Dennis Ritchie at Bell Labs. Ritchie and his colleague Ken Thompson were working on the Unix operating system, and they needed a more efficient and portable language to write it in. C was their solution.

Lesson image

This close relationship with Unix is key to C's significance. Because Unix was built with C, the language became incredibly influential. It provided a powerful combination: the readability of a high-level language with the low-level memory access of assembly language. This blend of power and control made it a go-to choice for system programming, like creating operating systems, drivers, and software that needs to be fast and efficient.

Many languages you might have heard of, including C++, Java, C#, and Python, have borrowed heavily from C's syntax and concepts. Learning C is like learning the blueprint for much of modern programming.

Anatomy of a C Program

Let's look at the basic structure of a C program. By tradition, the first program anyone writes in a new language is one that simply prints "Hello, World!" to the screen. It's a simple way to see all the essential pieces working together.

// This is a simple C program

#include <stdio.h>

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

This small program has a few key parts. Let's break them down.

  • #include <stdio.h>: This is a preprocessor directive. Before the program is actually turned into machine code, a preprocessor scans the file. This line tells it to include the contents of the stdio.h file, which stands for "standard input-output header." This file contains declarations for functions that handle input and output, like printf.
  • int main(): This is the main function. Every C program must have a main function. It's the starting point—the first piece of code that runs when you execute your program.
  • { ... }: The curly braces define the beginning and end of the function's code block. Everything inside them is part of the main function.
  • `printf(

Hello, World!\n");

This line calls the printf function to print the text inside the quotation marks. The \n is a special character that represents a newline. The semicolon ; at the end marks the end of the statement. Most lines of code in C must end with a semicolon.

  • return 0;: This line exits the main function. The 0 is a status code that tells the operating system the program ran successfully. A non-zero value typically signals an error.

From Source Code to Execution

The code you write, like the example above, is called source code. A computer's processor can't understand it directly. It only understands machine code, which is a series of ones and zeros. The process of turning your human-readable source code into machine-executable code is called compilation.

Here's a simplified look at the steps:

  1. Preprocessing: The preprocessor handles the directives, like #include. It essentially copies the contents of stdio.h into your file.
  2. Compilation: A compiler takes the preprocessed code and translates it into machine code that the processor can understand.
  3. Linking: If your program uses code from libraries (which printf does), a linker combines your compiled code with the necessary library code to create a final, single executable file.

Once you have this executable file, you can run it. When you do, the operating system loads it into memory and tells the CPU to start executing the instructions, beginning with the main function. And just like that, "Hello, World!" appears on your screen.

Quiz Questions 1/5

Who developed the C programming language and for what primary purpose?

Quiz Questions 2/5

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

That's a quick tour of what a C program is and how it comes to life. You've seen the basic building blocks and the journey from a text file to a running program.