No history yet

Introduction to C

The Origins of C

The C programming language has a rich history. It was created in the early 1970s by a computer scientist named Dennis Ritchie at Bell Labs. Its primary goal was to help build the Unix operating system. This close relationship with Unix is a key reason for C's enduring legacy.

Lesson image

Why learn C today? It's often called the “mother of all languages” because so many popular languages, including C++, Java, Python, and JavaScript, borrow concepts from it. Understanding C gives you a deep insight into how computers actually work at a lower level.

It’s used everywhere, from the operating systems on your phone and computer to the embedded systems in cars, microwaves, and watches. C is valued for its performance and for giving programmers a high degree of control over the hardware.

To get started, you need two basic tools: a text editor to write your code and a compiler to translate it into instructions the computer can understand.

Your First Program

A long-standing tradition in programming is to make your first program print the text "Hello, World!" to the screen. It's a simple way to confirm that your setup is working correctly.

Lesson image

Here's what the code looks like in C.

// This line includes the Standard Input/Output library.
#include <stdio.h>

// This is the main function, where the program starts.
int main() {
    // This line prints the text to the console.
    printf("Hello, World!\n");

    // This tells the operating system the program ran successfully.
    return 0;
}

Once you save this code in a file (for example, hello.c), you use a compiler like GCC to turn it into an executable program. Then, you run that program from your terminal, and it will display the famous greeting.

The Structure of a C Program

Let's break down the "Hello, World!" program to understand its basic structure. Every C program is built from a few key components.

#include <stdio.h> This is a preprocessor directive. It tells the compiler to include the contents of the stdio.h file, which stands for Standard Input/Output. This file contains declarations for functions like printf() that let our program interact with the user.

int main() { ... } The main function is the heart of every C program. Execution always begins here. The int before main indicates that the function will return an integer value when it's done. The curly braces {} define the beginning and end of the function's code block.

printf("Hello, World!\n"); This is a function call. We are calling the printf function to print text to the screen. The text we want to print, called a string, is placed inside double quotes. The \n is a special sequence that represents a newline character, moving the cursor to the next line after the text is printed. Every statement in C ends with a semicolon ;.

return 0; This line ends the main function. It returns a value of 0 back to the operating system. By convention, returning 0 means the program executed successfully without any errors.

Ready to check your understanding?

Quiz Questions 1/5

Who is credited with creating the C programming language?

Quiz Questions 2/5

What was the primary initial purpose for the creation of the C language?

And that's the basic anatomy of a C program. With these fundamental building blocks, you're ready to start exploring more of what C can do.