No history yet

Introduction to C

What is C?

C is a foundational programming language that has been around since the early 1970s. Think of it as a blueprint for many modern languages. It's known for being powerful and efficient, giving programmers a lot of control over how a computer operates. While newer languages have emerged, C's influence is still everywhere.

Lesson image

A Brief History

C was created at Bell Labs between 1972 and 1973 by a computer scientist named Dennis Ritchie. It wasn't built from scratch. It evolved from an earlier language called B, which was created by Ken Thompson.

The main goal was to create a language to build the Unix operating system. Before C, operating systems were often written in assembly language, which is specific to one type of computer. C was portable, meaning the same code could be adapted to run on different machines. This was a revolutionary idea at the time and a key reason for C's rapid adoption.

Why C Still Matters

You might wonder why a language from the 70s is still relevant. C's importance comes from its proximity to the hardware. It allows for fine-tuned control over memory and system resources, making it incredibly fast and efficient.

This efficiency is why C is still the language of choice for performance-critical tasks:

  • Operating Systems: The kernels of Windows, Linux, and macOS are all largely written in C.
  • Embedded Systems: The tiny computers inside your car, microwave, and smartwatch often run on C code.
  • Other Languages: Many popular languages like Python, PHP, and Ruby have their core interpreters written in C to ensure they run quickly. C++ and Objective-C are direct descendants of C.
Lesson image

Anatomy of a C Program

Let's break down the basic structure of a C program. By tradition, the first program anyone writes in a new language is one that simply displays "Hello, World!" on the screen.

#include <stdio.h>

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

This might look cryptic, but each part has a specific job. Let's go line by line.

#include <stdio.h>

This is a preprocessor directive. The #include command tells the C compiler to include the contents of another file before compiling the code. In this case, we're including stdio.h, which stands for "standard input-output header."

This header file contains declarations for functions that handle input and output, like printing text to the screen. Without it, the compiler wouldn't know what printf means.

int main(void)

This line defines 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 the program. The parentheses () after main are for function arguments, but void here means this function takes no arguments.

The int at the beginning means the main function is expected to return an integer value when it finishes.

printf("Hello, World!\n");

This is a statement inside the main function. It calls the printf function (which we got from stdio.h) to print text to the console. The text to be printed is enclosed in double quotes.

The \n is a special sequence called an escape character. It represents a newline, telling the console to move the cursor to the next line after printing. Finally, the semicolon ; at the end marks the end of the statement. Most statements in C must end with a semicolon.

return 0;

This line ends the main function. Since we declared that main would return an integer (int), we must return one. By convention, returning 0 signals to the operating system that the program executed successfully. A non-zero value typically indicates that an error occurred.

Now, let's test your understanding of these core concepts.

Quiz Questions 1/5

Who is credited with creating the C programming language, and from which earlier language did it primarily evolve?

Quiz Questions 2/5

What was the main reason C was considered a revolutionary language when it was created in the 1970s?

And that's the basic anatomy of a C program. It's a simple but powerful structure that forms the basis of all C applications.