Domanda

What are the naming standards for data structures in C language?

For example, the following code snippet was picked from http://www.sparknotes.com/cs/searching/hashtables/section3.rhtml:

typedef struct _hash_table_t_ {
    int size;       /* the size of the table */
    list_t **table; /* the table elements */
} hash_table_t;

Why is _*_ used into naming the struct, but not the typedef? What does the _t stand for? And so on...

A link pointing to the correct guide would be perfect.

I've been searching google and looking into coding style guides, however I couldn't find anything to relate to that.

È stato utile?

Soluzione

You should never copy naming conventions of your C implementation or third party libraries. They use their naming convention to not interfere (have name clashes) with your code.

The use of tag names starting with an underscore is expressly forbidden in ISO C99, 7.1.3:

— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

Personally, I believe using typedefs for structs is completely silly, since all it does is saving you from writing the struct keyword in a few places--information that should rather not be hidden behind a typedef. I always cringe when I see typedef names for structs that end in *_s just to signify that the typename is an alias for a struct.

Altri suggerimenti

There is no unique/definite/Ultimate/Final coding standard in C. Each project/company has its own coding standard. For eg linux kernel has its own coding standard guidelines. Read Chapter 5: Typedefs in this standard which discusses when you should/shouldn't use typedefs in structs.

  • Use underscores ('_') to separate name components.
  • _t is usually used for typedef, distinguish typedef names with a final "_t",for ex- Thing_list_t;

You can refer C Coding Standards for EECS 381 for more details.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top