C is considered an older programming language, but due to its speed, is still widely used today, especially in system programming and embedded environments.
This introductory guide will provide a head start in understanding and using C programming code.
For a guide to the most popular C programming language concepts, check out our Getting Started with C article. In this particular guide, we'll be focusing on the basic constructs of the language.
A minimal variable declaration must consist of the type and name. C is a strongly typed language, so the type can't be changed later on.
Also, C isn't object-oriented, which means that variables lack an access modifier. They can, however, be marked as static or as constants:
The static keyword doesn't mean that a variable belongs to a class rather than an object of the class (remember: C is not object-oriented!)
It means that the variable is valid in a global scope (rather than a function).
This code will print numbers from one to three.
Note: The variable is only initialized once and it retains its value throughout the program.
Besides other effects, static variables (and methods) are only visible to the class itself (more precisely, the same compilation unit) and cannot be accessed outside of it.
Methods in C
A minimal method declaration consists of its name.
The return type of a minimal method isn't explicitly defined and neither are the parameters and it will, therefore, accept any parameters and might return any value (even though the implicit default return type is an integer).
You should always define a return type (use void if your function doesn't return a value), and it will, therefore, accept any parameters and might return any value.
You should always define a return type (use void if your function doesn't return a value), and also the parameters of a method (use void to define an empty parameter list).
Additionally, methods can be marked as static.
Pointers and Arrays
You can declare and de-reference a pointer in C using an asterisk. To get the address, use an ampersand as demonstrated below:
Arrays can be defined by using square brackets.
In C, arrays have a fixed type and length:
Pointers are often used with arrays to quickly access certain elements. Elements can also be accessed using their index:
Because C isn't object-oriented, it can be tricky to represent coherent values that aren't separated. However, using structs is a great method to use for this purpose since they define variables that belong together:
You can then create a pointer for the struct, allocate memory for it, and define the values:
The values can then be used the same way:
After you're done using the struct, make sure to free the allocated memory!
If statements allow you to define alternate paths depending on whether a value evaluates to true or false:
The else if and else blocks are optional. The switch-statement is an alternative way of checking for a larger range of possible values.
The default block is optional and the code is executed when another case isn’t matched.
The breaks aren’t necessary, but they enable the program to step out of the switch statement after the first case matches.
For- and While- Loops
For- loops can be used to repeat code a fixed number of times. You typically have to define a starting value, ending condition, and an action that's repeated after each iteration.
However, any of these instructions can be omitted.
The code inside of a while-loop will always be executed, as long as the condition defined in the loop’s head, is true.
Writing to the Console
To write for the console, use the printf()-command, which stands for print formatted.
Since we’re unable to can’t connect strings with other values in C, you’ll need to define placeholders that are replaced once you print the output.
See an example below:
The %s and %f signs are placeholders and you have to supply the values as a list of parameters after a formatted string.
Note: it's important to supply the right type of placeholder in the right order. The most common placeholders are:
%d or %i for integer types
%f for floating-point numbers
%s for strings
Supplying the wrong placeholder type could cause an incorrectly formatted output. To write the output for a file, use fprintf() and supply a pointer to the receiving file: