C Tutorial – The functions malloc and free

The function malloc is used to allocate a certain amount of memory during the execution of a program. The malloc function will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory.

When the amount of memory is not needed anymore, you must return it to the operating system by calling the function free.

Take a look at the following example: More »


Posted in C Tutorials | 39 Comments


C Tutorial – File I/O (using text files)

The file I/O functions and types in the C language are straightforward and easy to understand. To make use of these functions and types you have to include the stdio library. (Like we already did in most of the tutorials).

The file I/O functions in the stdio library are: More »


Posted in C Tutorials | 31 Comments


C Tutorial – structures, unions, typedef

In the C language structures are used to group together different types of variables under the same name. For example you could create a structure “telephone”: which is made up of a string (that is used to hold the name of the person) and an integer (that is used to hold the telephone number).
Take a look at the example: More »


Posted in C Tutorials | 73 Comments


C Tutorial – printf, Format Specifiers, Format Conversions and Formatted Output

In this C programming language tutorial we take another look at the printf function. We will look at how to use format specifiers to print formatted output onto the screen. The topics covered are; a little printf background, format specifiers and conversions, formatting of different types and format conversions of strings. More »


Posted in C Tutorials | 145 Comments


C Tutorial – strings and string Library Functions

Important: Before you start this tutorial, did you follow the pointers and more on pointers tutorials? Strings and pointers are intertwined to a large extent.

A string in the C language is simply an array of characters. Strings must have a NULL or \0 character after the last character to show where the string ends. A string can be declared as a character array or with a string pointer. First we take a look at a character array example: More »


Posted in C Tutorials | 12 Comments


C Tutorial – More on Pointers

In the previous C programming language tutorial we looked at the fundamentals of pointers. In this C tutorial we will look at some specifics of pointers.

Initialize a pointer

Before you can use a pointer in for instance a printf statement, you have to initialize the pointer.
The following example will not initialize the pointer: More »


Posted in C Tutorials | 32 Comments


C Tutorial – How to use Pointers

To make full use of the C Programming language, you have to have a very good understanding of pointers. For most people it will take some time to fully understand pointers. So be patient. You have to learn pointers because they are used everywhere in the C language. Once you master the use of pointers, you will use them everywhere. OK, enough pep talk, let’s start. More »


Posted in C Tutorials | 60 Comments


C Tutorial – More on Functions

In this C programming language tutorial we will talk some more about functions. We will take a look at command-line parameters and function prototypes.

Command-line parameters

In some cases you want to give a parameter at the start of a program.
For example: More »


Posted in C Tutorials | 3 Comments


C Tutorial – Functions and Global/Local variables

Most languages allow you to create functions of some sort. Functions are used to break up large programs into named sections. You have already been using a function which is the main function. Functions are often used when the same piece of code has to run multiple times.

In this case you can put this piece of code in a function and give that function a name. When the piece of code is required you just have to call the function by its name. (So you only have to type the piece of code once). More »


Posted in C Tutorials | 23 Comments


C Tutorial – Arrays and Multi-Dimensional Arrays

In this C programming language tutorial, we are going to talk about arrays.

An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this: More »


Posted in C Tutorials | 26 Comments