Reputation: 22696
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
{
} date_t;
I have also seen people put a capital like this:
typedef struct date
{
} Date;
Or for variables
typedef unsigned int Age;
or this
typedef unsigned int age_t;
Is there any standard that should be followed. Personally I prefer post fixing with a _t.
Many thanks for any suggestions,
Upvotes: 23
Views: 25566
Reputation: 78993
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.Upvotes: 15
Reputation: 118710
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
Upvotes: 1
Reputation: 70721
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;
Upvotes: 6
Reputation: 6327
Much of this comes down to personal preference, with the key being to be consistent (or if you have a company convention, use that). The following article has some naming guides:
http://www.montefiore.ulg.ac.be/~piater/Cours/Coding-Style/
Note that it switches the '_t' portion:
typedef struct node_t {
void *content;
struct node_t *next;
} Node;
typedef enum season_t { SPRING, SUMMER, FALL, WINTER } Season;
There was an earlier discussion on C naming conventions here:
What are the most common naming conventions in C?
Upvotes: 9
Reputation: 793239
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.
Upvotes: 47
Reputation:
Follow what the rest of the people do for your project so everything stays consistent. Otherwise they're both acceptable technically.
Upvotes: 3
Reputation: 20236
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.
Upvotes: 4