tags:

views:

119

answers:

7

Hello,

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,

+5  A: 

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.

jer
But, conventions evolve based on experience. Why not try to leverage that experience?
GregS
+2  A: 

Follow what the rest of the people do for your project so everything stays consistent. Otherwise they're both acceptable technically.

thyrgle
+11  A: 

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.

Charles Bailey
+2  A: 

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:

http://stackoverflow.com/questions/1722112/what-are-the-most-common-naming-conventions-in-c

Edward Leno
Wouldn't it be better to have the Node as the tag name, and node_t as the type? Thanks.
robUK
Given that '_t' is reserved for POSIX, I would suggest not using '_t' at all and coming up with a naming convention that makes sense to you (for example, see @casablanca's answer). I only quoted from the article, which is only an opinion.
Edward Leno
+1  A: 

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;
casablanca
Of course for some reason the C# and .NET convention seems to be capitalize the first letter of method and field names.
GregS
Names with a leading underscore followed by a capital letter or another underscore are reserved. (Names with a leading underscore following by a lowercase letter might be reserved depending on the scope; easier to just avoid leading underscores.) http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797
jamesdlin
A: 

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
Matt Joiner
+1  A: 

You may just simply use

typedef struct toto toto;
  1. The struct toto (tag) and the typedef name toto (identifier) are in different C "namescopes" so they are compatible, but they pooint to the same type in the end.
  2. As an extra bonus this is also compatible with C++, which usually implicitly has such a typedef.
  3. As another bonus this inhibits to declare a variable toto which can be quite confusing at times.
Jens Gustedt