What are the benefits of distinguishing type / tag names for typedef in C?

Some base codes use a different identifier for tagand type, for example:

typedef struct _foo { int bar; } foo;

Instead:

typedef struct foo { int bar; } foo;

The differences are explained in detail here: Difference between 'struct' and 'typedef struct' in C ++? (note: this question is about C, and the link is related to the C ++ question, so I make the assumption that the basics of this answer apply to C, although this may not be the case, or there may be some subtle differences).


My question is:

What are the practical benefits (if any) of using different identifiers for a type namespace and a global namespace?

Are there any good reasons to do one over the other? (under certain conditions) .. or is it just an agreement?

+4
3

, . Struct typedef " " ( namespace ++), .

typedef struct foo {
    int bar;
} foo;

, struct foo foo. struct struct.

IDE . , , , - ; typedef, . , .

struct foo, typedef foo, struct , . :

typedef struct foo_s {
    int bar;
} foo;

, foo_s.

, , typedefs . :

struct foo {
    int bar;
};

struct foo. foo, , struct foo. - , , . FILE <stdio.h> .

, C , , .

+3

. , , .

, , . , , - - ( ).

+2

/ typedef

. struct , C. , .

typedef struct foo_S {
  int bar;
} foo_T;

// legal
struct foo_S s1;
foo_T t1;

// illegal
foo_S s2;

// struct foo_T is not yet defined.
struct foo_T t2;

[]

DRY: _t . _t . / C, , - , . :

typedef struct point {
  int x,y;
} point_T;

point_T center;

struct, :

struct point center;

IMO, using the same / different name for structor tag depends on the standards and encoding goals of workgroups. Since C allows any approach, I simply set my group standard (pragmatic and dogmatic).

+1
source

Source: https://habr.com/ru/post/1616882/


All Articles