简体   繁体   中英

Why is C struct tm not as typedef?

I am just wondering out of curiosity, why is in C time structure

struct tm

used as this and not with typedef . This way I always need to write struct tm , to have a correct code. A lot of structures are "typedefed", why not this one?

Because it would be rather rude to pollute the general namespace with an unnecessary typedef when the language has a separate namespace for struct tags. If you really want a typedef to hide the fact that it's a struct , you can easily provide one yourself without forcing it on the rest of us.

A lot of structures are "typedefed", why not this one?

I don't know why you say that; there are no typedefed structs in the C library. FILE is a typedef, but not necessarily a struct.

Maybe you mean that there are a lot in other bodies of code. That's because some people, like yourself, think that namespace pollution is a good idea for some reason. The C library authors don't.

I think the thing is that in C this structure was born this way!

You are free to create your include file that declare the tm typedef, or declare it in your code!

typedef struct tm tm;

or better (on my opinion)

typedef struct tm tm_t;

因为C ++与C不同。C++不是C的超集,而只是语言规范。

You can use a #define line written on the top of your code to help you to write faster.

#define TM struct tm

Then, instead of struct tm write TM .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM