No history yet

Introduction to C

The Bedrock of Modern Code

Many modern programming languages are like shiny skyscrapers. They’re impressive, feature-rich, and let you build amazing things quickly. But beneath almost all of them, deep in the foundation, you’ll find C.

Developed in the early 1970s at Bell Labs, C is one of the oldest and most influential programming languages. It was created to build operating systems, and in fact, the core of operating systems like Windows, macOS, and Linux are all written in C. It gives programmers a level of control over the computer's hardware that few other languages can match.

Learning C is like learning the fundamentals of how computers actually work. Because so many other languages (like C++, Java, Python, and JavaScript) borrow its ideas and syntax, knowing C makes it easier to learn almost any other language.

Lesson image

Setting Up Your Workshop

To start writing C, you need two basic tools: a text editor and a compiler.

A text editor is a program for writing your code. It's like a word processor, but for programming. You can use a simple one like Notepad on Windows or TextEdit on Mac, but most programmers use a code editor like Visual Studio Code, which adds helpful features like syntax highlighting.

A compiler is a special program that translates your human-readable C code into machine code—the ones and zeros that a computer’s processor can actually understand and execute. A popular and free C compiler is GCC (the GNU Compiler Collection).

Many developers use an Integrated Development Environment (IDE), which bundles a text editor, a compiler, and other useful tools into a single application. Code::Blocks and Visual Studio are popular IDEs for C programming.

Lesson image

Anatomy of a C Program

Let’s write a program that does one simple thing: print the phrase "Hello, World!" to the screen. This is a long-standing tradition for a programmer's first program in any new language. It's a simple way to confirm that your tools are set up correctly and to see the basic structure of the language.

// This is your first C program!

#include <stdio.h>

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

This might look a bit cryptic, but each line has a specific job.

  • #include <stdio.h>: This is a preprocessor directive. It tells the compiler to include the "standard input/output" library. This library contains pre-written code for common tasks, like printing text to the screen. We need it for the printf function.
  • int main(): This is the main function. Every C program must have a main function—it's the starting point where the computer begins executing your code. The parentheses () are where you can pass information into the function, and int means the function will return an integer (a whole number) when it's done.
  • { and }: These curly braces define the beginning and end of the main function. All the code inside them belongs to this function.
  • printf("Hello, World!\n");: This is the line that does the work. printf is a function from the stdio.h library that prints text to the console. The text we want to print goes inside the parentheses and quotes. The \n is a special sequence that means "new line," so the cursor will move to the next line after the text is printed. The semicolon ; marks the end of the statement, like a period in a sentence.
  • return 0;: This line ends the main function. It sends a value of 0 back to the operating system, which is the standard way of saying "the program ran successfully."

Compile and Run

Once you've saved your code in a file (let's call it hello.c), you need to compile it. If you're using an IDE, you can usually just click a "Build" or "Run" button. If you're using a command line, you'd navigate to the file's directory and type a command like:

gcc hello.c -o hello

This command tells the GCC compiler to take the hello.c source file and create an executable program named hello. Once it's done, you can run your new program from the command line by typing:

./hello

If all goes well, you'll see Hello, World! printed on your screen. You’ve just written and run your first C program.

Quiz Questions 1/5

What is the primary role of a compiler in C programming?

Quiz Questions 2/5

Every C program must have a function that serves as the starting point for execution. What is this function called?

That's the fundamental cycle of C programming: write, compile, and run. It's a simple process that forms the basis for building much more complex and powerful applications.