Last active
December 20, 2015 21:59
-
-
Save gerdr/6201767 to your computer and use it in GitHub Desktop.
how we structure C type definitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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