Standard for typedef'ing

前端 未结 7 1995
清酒与你
清酒与你 2020-12-30 02:41

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         


        
相关标签:
7条回答
  • 2020-12-30 03:02

    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
    
    0 讨论(0)
  • 2020-12-30 03:04

    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.
    0 讨论(0)
  • 2020-12-30 03:05

    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.

    0 讨论(0)
  • 2020-12-30 03:07

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

    0 讨论(0)
  • 2020-12-30 03:11

    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;
    
    0 讨论(0)
  • 2020-12-30 03:14

    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.

    0 讨论(0)
提交回复
热议问题