Skip to main content

Welcome to C programming

This guide will help you set up everything you need to start working with the ALX low level programming curriculum. You’ll install the necessary tools, understand the compilation process, and run your first C programs.

Prerequisites

Before you begin, ensure you have:

Operating system

Linux (Ubuntu 20.04 LTS recommended) or macOS. Windows users should use WSL2.

Terminal access

Basic familiarity with command-line navigation and file operations.

Text editor

Any code editor (VS Code, Vim, Emacs, or Nano).

Internet connection

Required for installing packages and tools.

Installation

1

Install GCC compiler

The GNU Compiler Collection (GCC) is essential for compiling C programs.Ubuntu/Debian:
sudo apt update
sudo apt install build-essential
macOS:
xcode-select --install
Verify installation:
gcc --version
You should see output showing GCC version 9.4.0 or higher.
2

Install additional tools

Install helpful development tools for debugging and code quality.
# Betty style checker (for ALX coding standards)
git clone https://github.com/holbertonschool/Betty.git
cd Betty
sudo ./install.sh

# Valgrind (memory leak detector)
sudo apt install valgrind

# GDB (debugger)
sudo apt install gdb
3

Set up your workspace

Create a directory structure for your projects.
mkdir -p ~/alx-low_level_programming
cd ~/alx-low_level_programming

Understanding the compilation process

C compilation happens in four stages. Understanding these stages is crucial for debugging and optimization.
The preprocessor handles directives like #include and #define, expanding macros and including header files.
# Run only the preprocessor
gcc -E source.c -o output.i
Example from the curriculum:
0-preprocessor
#!/bin/bash
gcc -E $CFILE >> c
The -E flag stops after preprocessing. Output contains expanded code with all headers included.
The compiler translates preprocessed C code into assembly language.
# Generate assembly code
gcc -S source.c -o output.s
Example from the curriculum:
2-assembler
#!/bin/bash
gcc -S $CFILE
The assembler converts assembly code into machine code (object files).
# Create object file
gcc -c source.c -o output.o
Example from the curriculum:
1-compiler
#!/bin/bash
gcc -c $CFILE
The linker combines object files and libraries into an executable.
# Create executable with custom name
gcc -o program_name source.c
Example from the curriculum:
3-name
#!/bin/bash
gcc -o cisfun $CFILE

Your first C program

Let’s compile and run a simple “Hello, World!” program using examples from the curriculum.
1

Create your first C file

Create a file called hello.c with the following content:
hello.c
#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: 0 on success
 */
int main(void)
{
    puts("\"Programming is like building a multilingual puzzle");
    return (0);
}
This example uses puts() from the standard library to print a string.
2

Compile the program

Use GCC to compile your program:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 hello.c -o hello
Compiler flags explained:
  • -Wall: Enable all common warnings
  • -Werror: Treat warnings as errors
  • -Wextra: Enable extra warnings
  • -pedantic: Enforce strict ISO C compliance
  • -std=gnu89: Use GNU C89 standard (required for ALX projects)
3

Run your program

Execute the compiled program:
./hello
Expected output:
"Programming is like building a multilingual puzzle

More examples

Here are additional examples from the curriculum to practice with:
#include <stdio.h>

/**
 * main - Entry point
 * Return: 0 on success
 */
int main(void)
{
    printf("with proper grammar, but the outcome is a piece of art,\n");
    return (0);
}
Compile these examples using the same compiler flags. Understanding data type sizes is crucial for memory management later in the curriculum.

Common compilation errors

Error message:
undefined reference to 'function_name'
Solution: You’re missing a function implementation or need to link a library. Make sure all functions are defined or properly declared.
Error message:
implicit declaration of function 'function_name'
Solution: Include the appropriate header file or declare the function before using it.
Error message:
expected ';' before '}' token
Solution: Check for missing semicolons, brackets, or parentheses. GCC usually points to the line where it detected the problem.

Betty coding style

All ALX projects must follow the Betty coding style. Here are the key principles:

Documentation

All functions must have documentation comments describing purpose, parameters, and return values.

Formatting

Use proper indentation (tabs), place braces correctly, and limit line length to 80 characters.

Naming

Use descriptive names with snake_case for functions and variables. Avoid single-letter names except for counters.

Structure

One declaration per line, declare variables at the beginning of functions, and limit functions to 40 lines.
Check your code style:
betty filename.c

Next steps

Now that your environment is set up, you’re ready to dive into the curriculum:

Fundamentals

Start with C basics: variables, conditionals, loops, and functions.

Pointers & memory

Learn memory management and pointer manipulation.

Data structures

Implement linked lists, hash tables, and more.

System programming

Work with file I/O and create libraries.
Keep the GCC manual handy: man gcc or visit gcc.gnu.org/onlinedocs

Build docs developers (and LLMs) love