Topic : An introduction to C
Author : Tom Torfs
Page : 1 Next >>
Go to page :

An introduction to C
An introduction to C - version 1999-11-16
Copyright (c) 1998-1999 by Tom Torfs
( (preferred),, 2:292/516 (Fidonet))

Current document size: HTML: approx. 230 KB
                       text: approx. 202 KB

This document, in both HTML and text format, may be distributed freely. Modifications made by anyone but the author must be clearly marked as such and a reference must be provided as to where the reader can obtain the original, unmodified version. This copyright information may not be removed or modified. It will apply only to the original text, not to any text added by others.
The author cannot be held responsible for any damage caused directly or indirectly by the use of information in this document.
Any suggestions for additions, improvements and/or error corrections are welcome at any of the addresses mentioned above.
All the source code in this document should be distributed together with it as plain text files. If you don't have them, you can obtain them, and the latest version of this document, at:
0. Contents

1. Overview and rationale

2. Hello, world!
   2.1. The Hello, world! program
   2.3. #include
   2.4. Functions and types
   2.5. return

3. Using variables
   3.1. The 42 program
   3.2. Defining variables
   3.3. printf()

4. Doing calculations
   4.1. The calc program
   4.2. Operators and typecasts
   4.3. Using the functions from math.h

5. Enums and typedefs
   5.1. The colours program
   5.2. Defining and using enums and typedefs

6. Structs, unions and bit-fields
   6.1. The cars program
   6.2. Using structs, unions and bit-fields

7. Conditionals (if/else)
   7.1. The yesno program
   7.2. Obtaining random numbers
   7.3. Using if/else
   7.4. Using the ?: operator

8. switch/case/default
   8.1. The animals program
   8.2. Using the switch() statement

9. Loops (do,while,for)
   9.1. The loops program
   9.2. Using loops
   9.3. Using break,continue,goto

10. Arrays, strings and pointers
   10.1. The strings program
   10.2. Arrays and strings
   10.3. The pointers program
   10.4. Addresses and pointers
   10.5. Example: using command line arguments

11. Using files
   11.1. The fileio program
   11.2. Using disk files
   11.3. The interact program
   11.4. Using the standard I/O streams

12. Dynamic memory allocation
   12.1. The persons program
   12.2. Dynamically allocating memory

13. Preprocessor macros/conditionals
   13.1. The preproc program
   13.2. Using preprocessor macros/conditionals
   13.3. Using the assert() macro

14. Variable argument functions
   14.1. The vararg program
   14.2. Using variable argument functions
   14.3. Example: a printf() encapsulation

15. Modular approach
   15.1. The module program
   15.2. Using different modules

16. Overview of the standard library
   16.1. Introduction to the standard library
   16.2. assert.h (diagnostics)
   16.3. ctype.h (character handling)
   16.4. errno.h (error handling)
   16.5. float.h (floating-point limits)
   16.6. limits.h (implementation limits)
   16.7. locale.h (localization)
   16.8. math.h (mathematics)
   16.9. setjmp.h (non-local jumps)
   16.10. signal.h (signal handling)
   16.11. stdarg.h (variable arguments)
   16.12. stddef.h (standard type/macro definitions)
   16.13. stdio.h (input/output)
   16.14. stdlib.h (general utilities)
   16.15. string.h (string handling)
   16.16. time.h (date and time)

17. Epilogue
   17.1. Credits
   17.2. Other interesting C-related online material

1. Overview and rationale
This document is intended to give people who are interested in learning C, whether they already know another programming language or not, a quick introduction to the language. It does not pretend to be complete, but it should get you familiar with most concepts of the C language. It is not intended to replace a good introductory book on the subject; on the contrary, it is probably best used together with such a book; if you haven't programmed before some of the explanations in this introduction may be a bit short and getting more detailed information from a book would often be useful.
This introduction discusses the standard C language as defined by the International Standards Organization [*], also commonly referred to as "ANSI C" (the American National Standards Institute had standardized the language before ISO). It does not discuss platform/compiler specific extensions such as accessing a screen in either text or graphics mode, interacting with external devices, direct access to memory, using operating system features etc.
[*] ANSI/ISO 9899-1990 aka C89/C90; normative addenda are not discussed here; C9X proposed features may be referred to occasionally; the implementation is assumed to be hosted
It has been - rightfully - reported that the material in this tutorial may not be very easy reading for a complete beginner to the C language, mainly because I try to be as complete as possible even in early stages of this tutorial.
This has two reasons: first, this tutorial can also be useful as reference material, e.g. to look up what printf() specifier can be used for what purpose, etc.
The second, more fundamental reason, is that the principle objective of this tutorial was to prefer accuracy over simplicity. In other words, I feel there are already enough tutorials and introductory books that don't adhere to the standard in several cases to keep things simple. This has the consequence that some people who learned C from such a tutorial/book will have a hard time adjusting their programming habits to write conforming code whenever possible. Since this tutorial is partly intended as a "cure" for those people, it requires some degree of accuracy which also means that it may not read very fluently if you're completely new to C. This only confirms the above statement that this tutorial is not meant to be used on its own, but rather as a complement to a good introductory book.

When reading this tutorial, it is OK to skip certain descriptions and listings which you feel are not required yet. Examples may include the complete list of printf() specifiers, standard types, etc.

2. Hello, world!
2.1. The Hello, world! program

/* prog2-1.c: Hello, world! */

#include <stdio.h>

int main(void)
   puts("Hello, world!");
   return 0;

If you compile and run this small program [*], you'll see that it writes the message Hello, world! onto your screen (or printer, or whatever output device you're using) and then ends the program successfully.
[*] see your compiler documentation / online help for instructions
Also, keep in mind that you are free to choose your own formatting style. E.g. the above could have been written as: (the reasons it hasn't should be obvious)

/* prog2-1.c: Hello, world! */
#include <stdio.h>
int main(void) {puts("Hello, world!"); return 0;}

Now we'll analyze this program:

/* prog1.c: Hello, world! */

Everything that is inside /* and */ is considered a comment and will be ignored by the compiler. You must not include comments within other comments, so something like this is not allowed: [*].

/* this is a /* comment */ inside a comment, which is wrong! */

[*] your compiler may allow this, but it's not standard C
You may also encounter comments after //. These are C++ comments and while they will be allowed in the next revision of the C standard, you shouldn't use them yet. They are more limited than the flexible /* */ comments anyway.2.3. #include

#include <stdio.h>

In C, all lines that begin with # are directives for the preprocessor, which means that all these directives will be processed before the code is actually compiled. One of the most commonly used preprocessor directives is #include. It has two forms. The first form is:

#include <header>

This form should only be used to include a standard header, such as stdio.h. This gives you access to all the goodies in the header. In this program we are going to use puts() (see below), and to be able to do that we need to [*] include the standard header stdio.h (which stands for standard input/output header).
[*] in some cases you may get away with not including the right standard header, but in other cases it may cause nasty things to happen (ranging from a simply malfunctioning program to a core dump or even a system crash: this is called undefined behaviour, in other words anything might happen); basically you should always include the right headers.
The following standard headers are available: (see 16. Overview of the standard library for a detailed discussion of them)
Commonly used headers:

stdio.h    input/output
stdlib.h   general utilities
string.h   string handling
time.h     date and time
math.h     mathematics
ctype.h    character handling

Standard definitions:

stddef.h   standard type/macro definitions
limits.h   implementation limits
float.h    floating-point limits
errno.h    error handling
assert.h   diagnostics

More advanced headers:

stdarg.h   variable arguments
setjmp.h   non-local jumps
signal.h   signal handling
locale.h   localization

The second form

Page : 1 Next >>