Skip to content

Instantly share code, notes, and snippets.

@gerdr
Last active December 20, 2015 21:59
Show Gist options
  • Save gerdr/6201767 to your computer and use it in GitHub Desktop.
Save gerdr/6201767 to your computer and use it in GitHub Desktop.
how we structure C type definitions
// enumerations cannot be forward-declared
// the compiler needs to see its definition
// if it wants to choose an underlying type other than int
// therefore, we start with enum definitions
enum foo
{
FOO,
BAR
};
// the enum typedef could be joined with the definition
// we list it seperately for consistency
typedef enum foo foo;
// forward-declarations of structs and unions
// same as with enums, using the same name is perfectly fine
// if you want to use different names, don't use preceding underscores
// trailing ones are fine, but why bother?
typedef struct bar bar;
typedef union baz baz;
// actual definitions of struct and union types
struct bar
{
int a;
int b;
};
union baz
{
int i;
float f;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment