No history yet

Introduction to C Programming

The Building Blocks of C

C is one of the oldest and most influential programming languages. It's known for its power and efficiency, which is why it's used to build operating systems, game engines, and other high-performance software. Let's start with the traditional first program, "Hello, World!" It's a simple way to see the basic structure of a C program.

#include <stdio.h>

int main() {
    // This line prints the text to the console
    printf("Hello, World!\n");
    return 0;
}

Let's break that down.

  • #include <stdio.h>: This is a preprocessor directive. It tells the compiler to include the contents of the "Standard Input/Output" library, which gives us access to functions like printf().
  • int main(): This is the main function. Every C program has one. It's the entry point where the program's execution begins.
  • printf("Hello, World!\n");: This is a function call that prints text to the screen. The text inside the quotes is what gets printed. The \n is a special character that means "new line."
  • return 0;: This line ends the main function and returns a value of 0. A return value of 0 typically means the program ran successfully.
  • Curly braces {} define the beginning and end of a function. Semicolons ; mark the end of a statement.

Variables and Data Types

To do anything useful, a program needs to store information. Variables are used for this. Think of them as labeled boxes where you can keep data while your program is running.

variable

noun

A named storage location in memory that holds a value. The value can be changed during program execution.

Before you use a variable, you must declare it by specifying its name and the type of data it will hold. C has several fundamental data types.

Data TypeDescription
intIntegers (whole numbers, like -5, 0, 100)
floatSingle-precision floating-point numbers (decimals, like 3.14)
doubleDouble-precision floating-point numbers (more precise decimals)
charA single character (like 'a', 'Z', or '!')

Here’s how you declare and initialize variables:

#include <stdio.h>

int main() {
    int age = 30;
    float price = 19.99;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Price: %f\n", price);
    printf("Grade: %c\n", grade);

    return 0;
}

Notice the %d, %f, and %c inside the printf statements. These are format specifiers. They act as placeholders that tell printf where to put the variable's value and what type of data to expect: %d for an integer, %f for a float, and %c for a character.

Controlling the Flow

Programs often need to make decisions or repeat actions. Control structures let you dictate the flow of execution instead of just running code from top to bottom.

The if statement is the most basic decision-making tool. It runs a block of code only if a certain condition is true. You can extend it with else if and else to handle multiple conditions.

int number = -10;

if (number > 0) {
    printf("The number is positive.\n");
} else if (number < 0) {
    printf("The number is negative.\n");
} else {
    printf("The number is zero.\n");
}

Loops are used to repeat a block of code. A for loop is great when you know exactly how many times you want to repeat an action.

// This loop will run 5 times
for (int i = 0; i < 5; i++) {
    printf("Iteration number %d\n", i);
}

A while loop is different. It keeps running as long as its condition remains true. You use this when you don't know the exact number of iterations ahead of time.

int countdown = 3;

while (countdown > 0) {
    printf("%d...\n", countdown);
    countdown = countdown - 1; // Or countdown--
}

printf("Liftoff!\n");

Functions and Reusability

As programs grow, you'll find yourself writing the same piece of logic over and over. Functions solve this by letting you package a block of code into a reusable unit. We've already been using functions like main and printf.

A function can take inputs (called parameters) and produce an output (called a return value). This makes your code modular, easier to read, and simpler to debug.

Good programming is about breaking down large problems into smaller, manageable pieces. Functions are the primary tool for doing this.

Here's how to define and use a simple function that adds two numbers.

#include <stdio.h>

// Function definition
int add(int a, int b) {
    int sum = a + b;
    return sum; // Returns the result
}

int main() {
    int result = add(5, 3); // Function call
    printf("The result is: %d\n", result);
    return 0;
}

In this example, int add(int a, int b) is the function's signature. It tells us the function is named add, it returns an int, and it accepts two int parameters, a and b.

From Code to Program

Writing code is just the first step. To run it, you must convert your human-readable source code into machine-readable instructions. This process is called compilation. A program called a compiler reads your code, checks for errors, and translates it into an object file. Then, a linker combines this object file with any necessary library code to create a final, executable program.

If you have a C compiler like GCC (GNU Compiler Collection) installed, you can compile the "Hello, World!" program from your terminal. If you saved the code in a file named hello.c, you would run this command:

gcc hello.c -o hello

This tells GCC to compile hello.c and create an executable output file (-o) named hello. You can then run your program by typing ./hello in the terminal.

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

Quiz Questions 1/6

What is the primary purpose of the int main() function in a C program?

Quiz Questions 2/6

What does the line #include <stdio.h> accomplish?

You've just covered the absolute fundamentals of C. These building blocks—syntax, variables, control flow, and functions—are the foundation for writing any program, simple or complex.