Absurd: An Introduction to C

Part I, 20120613

C is a computer programming language that was designed for the purpose of implementing system software. Today, it is still used for that task but is also used for implementing application software. C is insanely widely used. C is the bedrock of UNIX, Linux, and Windows. C powers the applications people use most. C influenced many other languages: ObjC, C++, Java, etc... Most importantly, a C compiler is available for almost any architecture you care about. C is great in the power it gives to a good developer, but it does lack object orientation and garbage collection. If those two things matter quite a bit to you, you may wish to look into languages which are themselves C applications such as Python or PHP. Before you start writing a bunch of C, you will want to ensure that you have a compiler.

Debian-based

sudo apt-get install build-essential

RHEL-based

su -c "yum groupinstall 'Development Tools'"

On Windows, you will need to get mingw-get-inst from the MinGW Sourceforge site. Run the installer, and then add C:\MinGW\bin to the PATH environment variable.

#include <stdio.h>
  int main(void) {
  printf("hello, world\n");
  return 0;
}

Most of the BSDs ship with a compiler, on OSX just install Xcode. Slackware, Gentoo, Exherbo, etc... you already have a compiler. This is a traditional first program in any language since Dennis Ritchie wrote The C Programming Language. The first line is a C preprocessing directive. It informs the compiler that the contents of stdio.h should replace the line. The next line is a function (method, subroutine, module, whatever) declaration. The "int" informs the compiler that the function is going to return an integer. "main" is the name of the function. This particular function name must be present in every C program. As a matter of fact, the smallest C program you can write is:

main() { }

In C, parentheses enclose arguments that the function can accept. The braces you see are used to show the start and stop points of commands the function performs. The "printf" statement does what you would expect. It "prints" something on the "standard output" which is most typically a computer monitor. "printf" is a function from stdio.h and accepts arguments. In this case, the text to be printed to screen is the argument. As this text is being passed without first being assigned to a variable, it must be enclosed in quotation marks. The "\n" at the end of the text is a symbol that tells the compiler to put a carriage return at this point. The semicolon terminates the sequence of instruction. The next line is the return statement. This terminates the functions execution and returns a 0 to the runtime system. A zero indicates successful program completion. Save this little program as helloworld.c and close your editor. To test this program use the following command on Linux/UNIX/OSX:

cc helloworld.c -o helloworld

Then run the program

./helloworld

You should see "hello, world" on the screen.

⇠ back

© MMIX - MMXX, absurd.wtf
Licentiam Absurdum