welcome to

Programming Tutorials


001 - Hello, World!


  1. Introduction
  2. Symbols Used
  3. Lines
  4. Compilation Instructions
  5. Assembly-language Output
  6. Running the Program


On the index page of my website, I have a code snippet displayed:

//there should be code below here
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    printf("hello, world\n");
    return 0;

This is a very short program written in a language called "C". I'm not going to get into the history of C or its importance here, just know that this tutorial series is on the C programming language.

We're going to go line-by-line through this program and examine what each line does, as well as each linguistic symbol used here.

Symbols Used


  1. //there should be code below here
    • This is a comment and doesn't do anything.
  2. #include <stdio.h>
    • Include system library stdio.h.
    • stdio.h stands for "Standard I/O" or "Standard Input Output".
    • The printf method is defined in this library so we need to #include it.
  3. #include <stdlib.h>
    • Include system library stdlib.h.
    • We don't actually need this library to build this program, but it is a common library, so I frequently include it by default.
  4. int main(int argc, char *argv[]) {
    • Declare function int main with parameters int argc and char *argv[].
    • Every program generally needs a main function defined.
    • argc refers to "Argument Count", or the parameters passed into the program when ran.
    • argv refers to "Argument Vector", or the array where the program parameters are stored.
  5. printf("hello, world\n");
    • printf statement with parameter "hello, world\n".
    • This will print the string "hello, world".
  6. return 0;
    • return statement.
    • Returns are used to terminate a function and to "return" values to another part of our program.
  7. }
    • End of code block.
    • This is used to let the compiler know that this is the end of this code block.

Compilation Instructions


Satisfying Prerequisites


  1. To compile this program into an executable binary, we need to save this code to a file.
  2. Copy and paste the program into a text editor, such as "Notepad", "vim", "emacs", "Sublime", etc. and save it as main.c.
  3. Open a terminal program in the directory you saved the file, and execute gcc main.c.
    • optional: If you want to name the output file, execute gcc main.c -o nameOfBinary instead.
  4. The program gcc is a compiler, and will "compile" your code into an executable binary called a.out if you did not specify a name.
  5. To run your program, execute ./a.out.
    • optional: If you named your program something else, execute that program by typing the name with a ./ in front of it.

Assembly-language Output

We will dig into this some other time, but I want you to know one possibility of what that little program looks like before it gets linked into a fully-executable and working program. This is what it looks like:

gcc version

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

assembly output

    .section    __TEXT,__text,regular,pure_instructions
    .macosx_version_min 10, 12
    .globl  _main
    .align  4, 0x90
_main:                                  ## @main
## BB#0:
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register %rbp
    subq    $32, %rsp
    leaq    L_.str(%rip), %rax
    movl    $0, -4(%rbp)
    movl    %edi, -8(%rbp)
    movq    %rsi, -16(%rbp)
    movq    %rax, %rdi
    movb    $0, %al
    callq   _printf
    xorl    %ecx, %ecx
    movl    %eax, -20(%rbp)         ## 4-byte Spill
    movl    %ecx, %eax
    addq    $32, %rsp
    popq    %rbp

    .section    __TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
    .asciz  "hello, world\n"


Running the Program

Coming soon...