Skip to main content

Overview

The 0x00-hello_world module introduces you to C programming fundamentals. You’ll learn about the compilation process, write your first C programs using puts and printf, and understand data type sizes.

Compilation Process

C programs go through multiple stages before becoming executable files. Understanding these stages is crucial for debugging and optimization.

Preprocessing

The preprocessor handles directives like #include and #define, expanding them before compilation.
0-preprocessor
#!/bin/bash
gcc -E $CFILE >> c
The -E flag tells gcc to stop after preprocessing. The output shows all header files expanded and macros replaced.

Compilation to Object Code

The compiler translates C code to object code without linking.
1-compiler
#!/bin/bash
gcc -c $CFILE
This creates a .o file containing machine code that isn’t yet executable.

Assembly Generation

Generate assembly language code to see the low-level instructions.
2-assembler
#!/bin/bash
gcc -S $CFILE

Creating Executables

Compile and link to create an executable with a custom name.
3-name
#!/bin/bash
gcc -o cisfun $CFILE
The -o flag specifies the output filename. Without it, gcc defaults to a.out.

Your First C Programs

Using puts()

The puts() function outputs a string followed by a newline.
4-puts.c
#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: 0 on success
 */


int main(void)
{
puts("\"Programming is like building a multilingual puzzle");
return (0);
}
The puts() function automatically adds a newline, unlike printf(). The \" escapes the quote character.
Compilation:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 4-puts.c -o puts
./puts

Using printf()

The printf() function provides formatted output with more control.
5-printf.c
#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);
}
Key differences between puts() and printf():
  • printf() requires explicit \n for newlines
  • printf() supports format specifiers (%d, %s, etc.)
  • puts() is simpler for plain strings

Data Type Sizes

Understanding data type sizes is essential for memory management and choosing appropriate types.
6-size.c
#include <stdio.h>

/**
 * main - Entry point
 * Return: 0 on success
 */
int main(void)
{
printf("Size of a char: %i byte(s)\n", sizeof(char));
printf("Size of an int: %i byte(s)\n", sizeof(int));
printf("Size of a long int: %i byte(s)\n", sizeof(long int));
printf("Size of a long long int: %i byte(s)\n", sizeof(long long int));
printf("Size of a float: %i byte(s)\n", sizeof(float));
return (0);
}
  • char: 1 byte (8 bits)
  • int: 4 bytes (32 bits)
  • long int: 4 or 8 bytes (platform-dependent)
  • long long int: 8 bytes (64 bits)
  • float: 4 bytes (32 bits)
The sizeof operator returns the size in bytes. Sizes may vary between 32-bit and 64-bit systems.

Compilation Standards

All programs in this module follow the Betty style and compile with strict flags:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 main.c -o program
Flag explanations:
  • -Wall: Enable all common warnings
  • -Werror: Treat warnings as errors
  • -Wextra: Enable extra warnings
  • -pedantic: Enforce strict ISO C standard
  • -std=gnu89: Use GNU89 C standard

Best Practices

Always include function documentationEvery function should have a comment block describing its purpose, parameters, and return value, following the Betty style.
Return 0 from main()Returning 0 indicates successful execution. This is important for shell scripts that check exit codes.
Use appropriate output functionsUse puts() for simple strings and printf() when you need formatting or don’t want automatic newlines.

Common Issues

The -Werror flag converts warnings to errors. Common issues:
  • Unused variables
  • Missing return statements
  • Implicit function declarations
Fix all warnings before they become errors!
Remember that printf() doesn’t add a newline automatically. Always include \n when needed.

Build docs developers (and LLMs) love