Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 2: Building a Simple Program

2.1 Compilers vs. interpreters

One major difference between C and Python is how programs run. Python is an interpreted language: the code is executed line by line. C is a compiled language. A compiler first translates the code into machine code, and then the machine code is executed.

Example

Python:

print("Hello, World!")

To run it:

python my_program.py

C:

#include <stdio.h>

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

To compile and run it:

gcc my_program.c -o my_program
./my_program

2.2 Writing and compiling your first C program

Here is a simple C program that prints “Hello, World!”:

#include <stdio.h>

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

To compile and run it:

  1. Save the code in a file named hello.c.
  2. Open a terminal and go to the directory that contains hello.c.
  3. Compile the program with GCC (the GNU Compiler Collection):
    gcc hello.c -o hello
    
  4. Run the compiled program:
    ./hello
    

Explanation:

  • #include <stdio.h> includes the standard input/output library, which you need in order to use the printf function.
  • int main() { ... } is the main function. Program execution starts here.
  • printf("Hello, World!\n"); prints “Hello, World!” followed by a newline character.
  • return 0; indicates that the program ended successfully.

2.3 Variable declarations

In C, you must declare the type of each variable before you use it. Python doesn’t ask for this, because its variables are dynamically typed.

Python:

x = 10

C:

int x = 10;

Older C (the C89 standard) required variables to be declared at the beginning of a block. Since C99 you can declare a variable anywhere before its first use, but many C programmers still put declarations at the top of a function.

Example:

#include <stdio.h>

int main() {
    int x = 10;
    printf("x is %d\n", x);
    return 0;
}

Explanation:

  • int x = 10; declares an integer variable x and initializes it to 10.
  • %d is the format specifier for printing an integer value.

2.4 Whitespace and formatting

In Python, whitespace is significant. In C it isn’t: statements end with semicolons, and blocks of code are enclosed in braces {}.

Python:

if x > 0:
    print("Positive")
else:
    print("Non-positive")

C:

#include <stdio.h>

int main() {
    int x = 10;

    if (x > 0) {
        printf("Positive\n");
    } else {
        printf("Non-positive\n");
    }
    return 0;
}

Explanation:

  • In C, the if statement needs parentheses around the condition. Braces {} mark the block of code that runs when the condition is true, and the block that runs when it is false.
  • If a branch is a single statement, the braces are optional: if (x > 0) printf("Positive\n"); is valid C. Writing them anyway is a common habit, because it avoids bugs when you later add a second line to the branch.

2.5 Basic input and output with printf

C uses the printf function for output. It produces formatted output, much like Python’s print.

Python:

name = "Alice"
print(f"Hello, {name}!")

C:

#include <stdio.h>

int main() {
    char name[] = "Alice";
    printf("Hello, %s!\n", name);
    return 0;
}

Explanation:

  • char name[] = "Alice"; declares a character array (a string) called name and initializes it with “Alice”.
  • %s is the format specifier for a string.
  • \n is a newline character.

Two more examples with printf follow.

Example 1: Printing integers and floats

#include <stdio.h>

int main() {
    int a = 5;
    float b = 4.5;

    printf("Integer: %d\n", a);
    printf("Float: %.2f\n", b);

    return 0;
}

Explanation:

  • %d: Prints an integer.
  • %.2f: Prints a floating-point number with two decimal places.

Example 2: Printing multiple variables

#include <stdio.h>

int main() {
    int num_sol = 2;
    float sol0 = 4.0;
    float sol1 = 1.0;

    printf("# solns: %d, solns: %.1f, %.1f\n", num_sol, sol0, sol1);

    return 0;
}

Explanation:

  • printf can print several variables at once: list them after the format string.