The First Program

C was never supposed to be a programming language for learning, so the "Hello World" in C takes some effort to write, and will require some handwaving for now. I promise I will explain everything later! Let's print something:

#include <stdio.h>

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

First of all, do you see this "Compile and Run" button above? Click it. Compile means to convert your program to a machine readable format (the binary), and Run is – obviously – executing a program. If everything works, it should print Hello World! in the output box above. You can try editing the code, running it, and seeing that the result changes.

Let's figure out line by line what's going on here.

int main() { ... }

You know this, it's a function! This time it has no parameters, and it has a special name main, which means that it's the function which will be evaluated when the program runs.

Before the function, we see a weird line

#include <stdio.h>

Funny enough, the C language does not have a print statement (like the one in Python), a console.log (like the one in JavaScript), or anything like that. There are reasons for that, and we'll talk about them; but for now, you need to tell the C compiler to read a file which defines functions for standard input and output – hence stdio. The .h means "header", and, as I said, more on that later.

printf("Hello World!\n");

printf if the way for your program to actually print something. If we print a text – that is, letters, spaces, and such – we must put it in "double quotes", and the special sequence \n at the end means "insert a new line", like what happens when you hit Enter or Return on your keyboard.

Finally, return 0; at the end, as we know, sets the result of the main function. If a program finishes with no errors, its result needs to be zero. For the last time on this page, I'll say "more on that later".

So, here we go, a working program! Once more:

#include <stdio.h>

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

Now, how would we print our variables? Remember the amount we pay for bananas? Let's do it!