Lots of C11

In my experience a lot of game devopers get to use nothing but language standards straght out of 1989 (here’s looking at you C++89 and C90). But the language has had some very welcome changes. And today I only want to talk abbout those which affect plain C programming.

C99: The 1999 Standard

First, and most importantly, C99 is backwards compatible. You can compile an old code base and (mostly) see no glaring probles. This is a great way to promote adoption. I say ‘mostly’ because there are always corner cases. But as important is the backwards-compatibilty is, C99 brings a lot more to the table.

’#ifndef true’ Can Die in a Fire

How often have you seen this in C code?

#ifndef true
#define true 1
#endif

Say goodbye to that. C99 offers the new <stdbool.h> header which defines two types:

  1. _BOOL
  2. bool an alias for the first.

It also gives you macros for true and false so you never have to write that by hand again.

One Line Comments

// You can now write this. Some older compilers allowed it, but not its support is mandatory.

Inline Functions

Let’s take the fundamental example:

int max(int x, int y) {     return (x > y) : x ? y; }

Now we can inline that for performance.

inline int max(int x, int y)
{
    return (x > y) : x ? y;
}

Variable Length Arrays

Which you should write this?

function(counter)
{
    users = people[counter];
    // …
}

Works now :)

More Data Types

C99 defines long long int and complex for you to use.

Variadic Macros

These, like printf() and friends use.

Restricted Pointers

Consider this function:

char * copy(char *foo, char *bar)
{
    return strcpy(foo, bar);
}

The problem? Both pointers may reference the same data. The restrict keyword tells them compiler than the pointers never point the same menu. So our function should be written like this:

char * copy(char * restrict for, char * bar)
{
    // Now `foo` and `bar` cannot point the same place, allowing for
    // compiler optimizations.
}

Conclusion

Oh, I know you didn’t have to do this, but the standard finally acknowledges that its ok to declare variables before you use. them.

Enjoy hacking up some C11 people! As C14 gets more support I’ll come back and write about that as well.

Advertisements

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s