Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -

# C Coding Standard

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This coding standard is (very) lite. Our aim was to make it easy to read and use in the exercises in this book. There are several other standards for C you can use once you're familiar with C. This standard is based on the GNU Coding Standards.

## Short version

• use English (comments, function names, variable names ...)
• use descriptive names for everything (variables, functions, macros, files ...)
• always use `{` and `}`. Yes even when only one statement after a for/if/while (etc).

## English

When you, eventually and hopefully, will start using C for real in projects all the code and comments you will write will be english so we better start with this behaviour directly.

## Descriptive names

If you see a variable named `nr_of_students`. What do you think it represents? You'll not get the Nobel Prize for saying that the variable represents the number of students. So this is a good name for a variable. In programming we use name for our variables that are descriptive, sometimes with a few words rather one. If we use several words we write the words with a an underscore (`_`) in between and all letters in lower case. This is why a variable representing "Nr of students" becomes `nr_of_students` when we write code.

## C code conventions

### indent

When writing code it is important to see what parts belong to what block. To acchieve this we indent our code a few characters (2 is recommended) for every block of code. Here's an example:

```if (length > 0 )
{
for (i=0; i<10; i++)
{
sum = sum + points[i];
}
}
```

We believe the above is way easier to read than the following (which is NOT the way to format your code):

```  if (length > 0 )
{
for (i=0; i<10; i++)
{
sum = sum + points[i];
}
}
```

### { and }

In C (and many other languages) you can skip the characters `{` and `}` after for example an `if` and `for` statement, like this (NOT recommended):

```if (length > 0 )
for (i=0; i<10; i++)
sum = sum + points[i];
```

The `for` statement becomes the block of code for the `if` statement and the `sum = sum + points[i];` becomes the block of code executed in `for` loop. So, for the compiler, this is the same thing as writing:

```if (length > 0 )
{
for (i=0; i<10; i++)
{
sum = sum + points[i];
}
}
```

Look at the following code:

```if (length > 0 )
for (i=0; i<10; i++)
sum = sum + points[i];
total = total + i;
```

The two statements:

```    sum = sum + points[i];
total = total + i;
```

looks as if they belong to the `for` loop but only one of them does. This is how the compiler will treat the code:

```if (length > 0 )
{
for (i=0; i<10; i++)
{
sum = sum + points[i];
}
}
total = total + i;
```

We can see that it is easy to make mistakes not using `{` and `}`.... so let's use them , which is not a very cumbersome work for you to add, we make the code:

• look the same regardless of how many statements are in the block and
• the code is less vulnerable for mistakes