The C Programming Language: Preface
C is by any standard a relatively old programming language; despite this it is still widely used. In fact C just missed out on a top ten spot in this years Stack Overflow Developer Survey.
For a long time now I've been meaning to go through The C Programming Language by Brian Kernighan and Dennis Ritchie. Although the most recent edition of the book is older than I am, it still has a reputation for being one of the best ways to learn C.
Over the next few posts I'm going to go over each of the following chapters from the book:
- A Tutorial Introduction
- Types, Operators, and Expressions
- Control Flow
- Functions and Program Structure
- Pointers and Arrays
- Structures
- Input and Output
- The UNIX System Interface
I'm planning to keep sample code on GitHub. If you're interested in following along, the rest of this post is going to quickly go over compiling a simple hello world program on CentOS or Debian.
Installing GCC
The GNU Compiler Collection (GCC) is a free and open source
compiler system which is available on most Linux distributions. On Debian it
can be installed using apt-get
:
sudo apt-get install gcc
Alternatively on Red Hat based distributions like CentOS yum
can be used:
sudo yum install gcc
If everything goes well, you should now be able to run gcc --version
:
$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Hello World
Once GCC is installed you can test it with a simple hello world example. First
create a text file called hello.c
with the following contents:
#include <stdio.h>
main()
{
printf("hello, world\n");
}
This can then be compiled with gcc
:
gcc hello.c
Compiling the source code will produce an executable binary, by default the
binary will be called a.out
. Running this should print hello, world
to
stdout:
$ ./a.out
hello, world
Note: a.out is short for assembler output, this filename default actually pre-dates the first edition of UNIX!