20

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,

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • 2
    I think the more customary terminology is 'convention' rather than 'standard'. Good question. I think the openssl project for one uses the convention of typedef struct objname_st {...} objname. – President James K. Polk Aug 21 '10 at 16:26
  • I can't believe the global C community hasn't developed a shared standard for something this elementary after 50-odd years. Almost every other major language has obvious naming & style conventions everyone sticks to because we all understand the value in doing so, whereas you still regularly see people writing C functions without word separators of any kind. – iono Jul 13 '22 at 04:56

7 Answers7

41

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
14

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 point 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
  • 76,821
  • 6
  • 102
  • 177
9

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?

Community
  • 1
  • 1
Edward Leno
  • 6,257
  • 3
  • 33
  • 49
  • Wouldn't it be better to have the Node as the tag name, and node_t as the type? Thanks. – ant2009 Aug 21 '10 at 19:08
  • 7
    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 Aug 21 '10 at 19:32
5

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
  • 69,683
  • 7
  • 133
  • 150
  • Of course for some reason the C# and .NET convention seems to be capitalize the first letter of method and field names. – President James K. Polk Aug 21 '10 at 16:54
  • 10
    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 Aug 21 '10 at 17:08
4

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
  • 20,094
  • 5
  • 45
  • 69
3

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

1

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
  • 112,946
  • 110
  • 377
  • 526