Friday 3 February 2017

Introduction to C Programming

This is the first in a series about the basics of programming in C. These lessons are taken from my book, The Little Book Of C, which is the course text for my online video-based course, C Programming For Beginners, which teaches C programming interactively in over 70 lessons including a source code archive, eBook and quizzes. For information on this courses see HERE.

What is C?


C is a general-purpose compiled programming language. It was first developed by Dennis Ritchie in the late ‘60s and early ‘70s. The C language is widely used for all kinds of programming: everything from general-purpose applications, programming language tools and compilers – even operating systems. The C language is also widely used for programming hardware devices.

A C compiler (and an associated tool called a ‘linker’) is the program that translates your source code (the text you write in an editor) into machine code that is capable of being run by your operating system. C compilers are available for all major operating systems including Windows, OS X and Linux.

Editors and IDEs


In order to write C code you will need a programming editor or IDE (Integrated Development Environment) and a C compiler. For beginners, I recommend the CodeLite editor which is freely available for several operating systems: http://codelite.org/ However, if you already use an editor or IDE that supports C programming, that’s fine. Suitable IDEs include NetBeans, Microsoft Visual Studio, Code Blocks and many others.

Once you have a C compiler and a C source code editor installed you are ready to start programming in C.

Hello World


This is the traditional “Hello World” program in C…

#include <stdio.h>

main() {
printf("hello world\n");
}

This program uses (that is, it ‘includes’) code from the C-language ‘standard input/output library, stdio, using this statement:

#include <stdio.h>

The code that starts with the name main is the ‘main function’ – in other words, it is the first bit of code that runs when the program runs. The function name is followed by a pair of parentheses. The code to be run is enclosed between a pair of curly brackets:

main() {

}

In this case, the code calls the C printf function to print the string (the piece of text) between double-quotes. The “\n” at the end of the string causes a newline to be displayed:

printf("hello world\n");


The anatomy of a C program


This shows the essential features of the simple ‘Hello world’ program…


The program above could be rewritten like this:

#include <stdio.h>

int main(int argc, char **argv) {
printf("hello world\n");
return 0;
}

In fact, if you create a new C project using the CodeLite environment, the code above will be generated automatically. When this program is run, you will see no difference from the last program – it too displays “Hello world” followed by a newline. The main differences are that this time the name of the main function is preceded by int. This shows that the function returns an integer (a full number) when it finishes running. The number 0 is returned in the last line of the function:

return 0;

This return value is unlikely to be of any significance in your programs and, for the time being at any rate, you can ignore it. By tradition, a value of 0 just means that the program ran without any errors. Any other value might indicate an ‘error code’.

The other difference is that this program contains two ‘arguments’, called argc and argv, between parentheses:

int main(int argc, char **argv)

 These arguments may optionally be initialized with values passed to the program when it is run. I’ll shown an example of this in the next lesson.