The C programming language is a general purpose programming language, which relates closely
to the way machines work. Understanding how computer memory works is an important aspect of
the C programming language. Although C can be considered as "hard to learn", C is in fact a very
simple language, with very powerful capabilities.
C is a very common language, and it is the language of many applications such as Windows, the
Python interpreter, Git, and many many more.
C is a compiled language - which means that in order to run it, the compiler (for example, GCC or
Visual Studio) must take the code that we wrote, process it, and then create an executable file. This
file can then be executed, and will do what we intended for the program to do.
Our first program
Every C program uses libraries, which give the ability to execute necessary functions. For example,
the most basic function called printf, which prints to the screen, is defined in the stdio.h header
file.
To add the ability to run the printf command to our program, we must add the following include
directive to our first line of the code:
#include <stdio.h>
The second part of the code is the actual code which we are going to write. The first code which
will run will always reside in the main function.
int main() {
... our code goes here}
The int keyword indicates that the function main will return an integer - a simple number. The
number which will be returned by the function indicates whether the program that we wrote
worked correctly. If we want to say that our code was run successfully, we will return the number 0.
A number greater than 0 will mean that the program that we wrote failed.
For this tutorial, we will return 0 to indicate that our program was successful:
return 0;
Notice that every statement in C must end with a semicolon, so that the compiler knows that a
new statement has started.
Last but not least, we will need to call the function printf to print our sentence.
Exercise
Change the program at the bottom so that it prints to the output "Hello, World!".
include <stdio.h>
int main() {
printf("Hello, World!");
, return 0;
}
Output:
Hello, World
Variables and Types
Data types
C has several types of variables, but there are a few basic types:
Integers - whole numbers which can be either positive or negative. Defined
using char, int, short, long or long long.
Unsigned integers - whole numbers which can only be positive. Defined
using unsigned char, unsigned int, unsigned short, unsigned long or unsigned lo
ng long.
Floating point numbers - real numbers (numbers with fractions). Defined
using float and double.
Structures - will be explained later, in the Structures section.
The different types of variables define their bounds. A char can range only from -128 to 127,
whereas a long can range from -2,147,483,648 to 2,147,483,647 (long and other numeric data
types may have another range on different computers, for example - from –
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit computer).
Note that C does not have a boolean type. Usually, it is defined using the following notation:
#define BOOL char#define FALSE 0#define TRUE 1
C uses arrays of characters to define strings, and will be explained in the Strings section.
Defining variables
For numbers, we will usually use the type int. On most computers today, it is a 32-bit number,
which means the number can range from -2,147,483,648 to 2,147,483,647.
To define the variables foo and bar, we need to use the following syntax:
int foo;int bar = 1;
The variable foo can be used, but since we did not initialize it, we don't know what's in it. The
variable bar contains the number 1.
Now, we can do some math. Assuming a, b, c, d, and e are variables, we can simply use plus, minus
and multiplication operators in the following notation, and assign a new value to a:
int a = 0, b = 1, c = 2, d = 3, e = 4;
a = b - c + d * e;printf("%d", a); /* will print 1-2+3*4 = 11 */
Exercise
,In the next exercise, you will need to create a program which prints out the sum of the
numbers a, b, and c.
#include <stdio.h>
int main() {
int a = 3;
float b = 4.5;
double c = 5.25;
float sum;
/* Your code goes here */
printf("The sum of a, b, and c is %f.", sum);
return 0;
}
Output:
The sum of a, b, and c is 0.000000.
Arrays
Arrays are special variables which can hold more than one value under the same variable name,
organised with an index. Arrays are defined using a very straightforward syntax:
/* defines an array of 10 integers */int numbers[10];
Accessing a number from the array is done using the same syntax. Notice that arrays in C are zero-
based, which means that if we defined an array of size 10, then the array cells 0 through 9
(inclusive) are defined. numbers[10] is not an actual value.
int numbers[10];
/* populate the array */
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
numbers[5] = 60;
numbers[6] = 70;
/* print the 7th number from the array, which has an index of 6 */printf("The
7th number in the array is %d", numbers[6]);
, Arrays can only have one type of variable, because they are implemented as a sequence of values
in the computer's memory. Because of that, accessing a specific array cell is very efficient.
Exercise
The code below does not compile, because the grades variable is missing.
One of the grades is missing. Can you define it so the grade average will be 85?
#include <stdio.h>
int main() {
/* TODO: define the grades variable here */
int average;
grades[0] = 80;
/* TODO: define the missing grade
so that the average will sum to 85. */
grades[2] = 90;
average = (grades[0] + grades[1] + grades[2]) / 3;
printf("The average of the 3 grades is: %d", average);
return 0;
}
Output
prog.cpp: In function ‘int main()’:
prog.cpp:7:3: error: ‘grades’ was not declared in this scope
grades[0] = 80;
Multidimensional Arrays
In the previous tutorials on Arrays, we covered, well, arrays and how they work. The arrays we
looked at were all one-dimensional, but C can create and use multi-dimensional arrays. Here is the
general form of a multidimensional array declaration:
type name[size1][size2]...[sizeN];
For example, here's a basic one for you to look at -
int foo[1][2][3];
or maybe this one -
char vowels[1][5] = {
{'a', 'e', 'i', 'o', 'u'}};