gcc 4.4.4 c89
I am just wondering is there any standard that should be followed when creating types.
for example:
typedef struct date
{
} dat
In general most languages allow the use of SentenceCase for non-standardized classes or types. I find this is the best practise, and in languages that allow it, additionally use namespaces or modules to prevent clashes. In languages that don't (such as C), a prefix where necessary never goes astray. To use a multi-language example for something I'm currently working on:
C: typedef uint32_t CpfsMode;
C++: namespace Cpfs { typedef uint32_t Mode; }
Python: cpfs.Mode = int
You may just simply use
typedef struct toto toto;
struct toto
(tag) and the
typedef
name toto
(identifier)
are in different C "namescopes" so
they are compatible, but they point to the same type in the end.typedef
.toto
which can
be quite confusing at times.Style is a very personal and highly subjective thing, I strongly urge you to just use whatever you like, or whatever conventions are used in your organization.
Follow what the rest of the people do for your project so everything stays consistent. Otherwise they're both acceptable technically.
I don't think there is any "standard" naming convention. In fact, they vary so wildly between projects (and also between other languages like C++ or Java) that I've personally adopted camelCase in all languages.
I always define my structures through typedef
, so I just use whatever name I would have given it otherwise (this is also what the Win32 API does). In case I need a self-referencing structure, I prefix an _
to the raw struct's name:
typedef struct _Node {
_Node *next;
} Node;
If you are working on a platform that follows POSIX standards you should be aware that any identifier ending in _t
is reserved for POSIX defined types so it is not advisable to follow the same convention for your own types.