Get introduced to the programming language C by learning the most important concepts for beginners.
C was developed in the 1970s and the language is still widely in use todayâespecially for system programming and embedded applicationsâbecause of its high speed and memory management capabilities and because it gets compiled rather than interpreted.
This article introduces the most important concepts of the language. The goal is to give you a good overview of these concepts rather than being a step-by-step guide or general programming introduction.
Before You Begin
Pointers in C
C supports the use of pointers, which are references to the position of a data-object (variables, functions, etc.) stored in the memory.
Pointers can be dereferenced (using an asterisk) to access the value or to call a function thatâs stored at the position. Other than that, you can also get a variableâs address (by using an ampersand) and directly assign a value to that address. This has the same effect as assigning the value to the variable itself:
#include <stdio.h>
int main()
{
// The current year and month is: 2018/12
int year = 2018;
int month = 12;
int *pointer = NULL;
// Make the pointer point at the address of
// the year-variable and assign a new value
// directly to the address
pointer = &year;
*pointer = 2019;
// Repeat the same with the month
pointer = &month;
*pointer = 1;
// We changed the date without assigning anything
// to the variables!
printf("The new date is: %d/%d\n", year, month);
return 0;
}
Pointers and Arrays
Arrays are fixed length and fixed type data-structures in C.
The values are usually stored one after another in the memory and because pointers reference a specific address in the memory, they can also be used to quickly go through an array of values. Simply assign the arrayâs first elementâs address to the pointer and then add the index to that address.
You can perform the same operations as above:
#include <stdio.h>
int main()
{
// The array contains each month and stores
// the values one after another in the memory
char* months[12] = {"JAN", "FEB", "MAR", "APR",
"MAY", "JUN", "JUL", "AUG",
"SEP", "OCT", "NOV", "DEC"
};
printf("The fifth month is: %s\n", months[4]);
// Make the pointer point to the beginning of the
// array and add 9 to the address to select the
// tenth month (indices start at 0)
char** datePointer = &months;
datePointer += 9;
// Print the value the pointer is referencing
printf("The tenth month is: %s\n", *datePointer);
return 0;
}
Memory Management
C is neither object-oriented nor do you have the possibility to change a variableâs type (and therefore its size) during runtime. Arrays are also fixed. Therefore you need to take care of the memory management yourself if you need dynamic data structures like lists or even structs.
Memory allocation can be done by using the standard libraryâs malloc()-function. The function takes the size of the block you want to allocate as a parameter and returns a pointer that references the beginning of that block. If the allocation failed, you get a NULL pointer. This allows you to load anything into the memory, as long as it fits:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Allocate two blocks in the memory with
// the size of an integer
int *year = malloc(sizeof(int));
int *month = malloc(sizeof(int));
// Check if the allocation failed
if(year == NULL || month == NULL)
{
fprintf(stderr, "Memory allocation failed!\n");
return 1;
}
// Store values in the allocated memory
*year = 2018;
*month = 12;
// Read the values and print them
printf("The date is: %d/%d\n", *year, *month);
// Don't forget to free the allocated space!
free(year);
free(month);
return 0;
}
The above example does the same thing as the first one in this article. As you can see, I used free() at the end of the code. Donât forget to free the allocated memory! Otherwise, you might end up using space thatâs not needed anymore and other processes might have less memory available as a consequence.
C is a Powerful Tool for Your Arsenal
C is a very powerful and incredibly fast language but itâs also quite hard to master. You definitely need to understand pointers and memory-allocation to write anything that goes beyond very simple programs and I hope this article gave you a good starting point!