A small C program tutorial

This is a small tutorial that introduces how to create a small C program.


Contents


Our first example with C

int main()
{
  return 1;
}

Using gcc, we compile and run our small example with:

gcc exp1.c -o exp1.exe
./exp1.exe

Testing

We get the returned code of our program with:

./exp1.exe
echo $?
1

Integers

An integer example (uninitialised):

  int d1;

Declared and initialised at the same time:

  int d1 = 5;

Declared and initialised after:

  int d1;
  d1 = 5;

Declared with an initialisation, and with its value changed later:

  int d1 = 3;
  d1 = 12;

Additions

Additions with integers:

int main()
{
  int d1 = 2;
  int d2 = 3;
  int d3;

  d3 = d1 + d2;

  return d3;
}

Getting the result:

gcc exp2.c -o exp2.exe

./exp2.exe
echo $?
5

Or in a more simple way:

int main()
{
  return ((6 * 4) + 2);
}
gcc exp5.c -o exp5.exe
./exp5.exe
echo $?
26

Arrays

An array example of 6 integers:

  int d[6];

We can initialise each cells of our array like this:

  d[0] = 1;
  d[1] = 2;
  d[2] = 3;
  d[3] = 4;
  d[4] = 5;
  d[5] = 6;

The equivalent array initialised from the beginning:

  int d[] = { 1, 2, 3, 4, 5, 6 };

For loops

The equivalent initialisation with a for loop:

  int d[6];
  int i;

  for (i = 0; i < 6; i++) {
    d[i] = i + 1;
  }

i = 0 initialises the variable i before we enter the loop.

The loop proceeds as long as i is lower than 6 (i < 6).

We add 1 to the variable i at each loop steps with i++.

Testing:

int main()
{
  int d[6];
  int i;

  for (i = 0; i < 6; i++) {
    d[i] = i + 1;
  }

  return d[3];
}
gcc exp3.c -o exp3.exe

./exp3.exe
echo $?
4

Chars Array

Here is an example of array that can contain 6 char's:

  char s1[6];

We can put one char in each cell:

  s1[0] = 'a';
  s1[1] = 'b';
  s1[2] = 'c';
  s1[3] = 'd';
  s1[4] = 'e';
  s1[5] = 'f';

Chars Pointers

A pointer to an array of char's:

  char *s;

Pointing the third element of the previous array:

  s = &(s1[2]);

Addresses

Retrieving the address of a cell containing an integer:

int main()
{
  int d[6];
  int *p;

  d[0] = 1;
  d[1] = 2;
  d[2] = 3;
  d[3] = 4;
  d[4] = 5;
  d[5] = 6;

  p = &(d[2]);

  return *p;
}
gcc exp3.c -o exp3.exe
./exp3.exe
3

Float's

A float example:

  float n1;

Setting a value for our floating point number:

  n1 = 1.2;

sizeof

int main()
{
  float a;
  double b;
  return sizeof(a);
}
gcc exp6.c -o exp6.exe
./exp6.exe
4

With sizeof(b):

8

And with arrays:

int main()
{
  float a[10];
  double b[10];
  return sizeof(a);
}

We get:

40

With several characters:

int main()
{
  char cs[] = "abcd";
  return sizeof(cs);
}

an additional ending character is automatically added at the end:

gcc exp8.c -o exp8.exe
./exp8.exe
5

Structures

A structure example:

struct {
  float n1;
  int i;
  char *s;
} _str;

Typedef's

Here is the previous structure with a typedef:

typedef struct str {
  float n1;
  int i;
  char *s;
} _str;

Conclusion, going further

TODO


Version of this document

2023-07-09.d


License

© 2019 2021 2022 2023 Florent Monnier

You can use this document according to the terms of either one of these licenses:


You can reuse the pieces of code according to the terms of either one of these licenses:


back