Welcome to the world of C! C came out of Bell Labs in the early 1970's (credit is given to Dennis Ritchie) as a language to implement the Unix platform with. Over the years, the language became more standardized and rich, with the current standard (at time of writing) being C18 (published in June of 2018), which will later be superseded by C23

C is what's known as a low-level programming language, meaning that there is relatively little abstraction between the code you write and the instructions that the machine runs. This means that there are fewer abstractions given to you; a C programmer has to worry about things like out-of-bounds checking and illegal memory accesses that other languages might handle for you. However, this also means that you have much more control over everything that happens, and don't have a lot of unnecessary overhead, allowing your programs to be much faster in comparison.

When learning a language, the first thing one does is usually figure out how to print "Hello, World!" to the console. This not only provides a nice introduction (and is tradition), but makes sure that your setup is working. Let's take a look at our "Hello, World!" program below:

hello.c
// Include printf function
#include <stdio.h>

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

We can start to see the structure of how a C program is written:

  • The first two lines comprise the header. This is where we can include header files that give us access to other code. We won't worry about that too much right now; all this does is gives us the function to print "Hello, World"
  • We can then see we have our main function. We'll cover functions more later, but it should hopefully be clear that there are some other lines of code associated with main, including one that looks like it prints "Hello, World!". Whenever we run a C program, main is what gets run
  • Our code also contains comments. These bits of text begin with // so that our compiler knows they aren't real code. They don't have any functionality, but can be useful to remind yourself what code is doing or whenever you want non-code text in your files.

Alright, let's actually run this! Using your preferred code editor, copy these lines into a file called hello.c (for now, all of our C files will appropriately end with the extension .c). Then, open up a terminal in the same directory and compile the code with the command

gcc -o hello hello.c

This command uses our GCC compiler to compile hello.c, with us specifying that the output should be named hello with the -o flag. You should now see (either in terminal or your regular file viewer) a file named hello in the same directory. This is a raw executable; it only contains the machine instructions to run your C code (and therefore has no extension). We can run it from the terminal in the same directory by simply specifying its location, using . as the current directory

./hello # MacOS/Linux
.\hello # Windows

You should hopefully get your desired output printed to the terminal; "Hello, World!" Congratulations; you've written your first C program! In the next chapter, we'll cover how we can start to build our knowledge of C with some basic types and operators.

  • No labels