defined typedef in "sys/types.h"

Hi
I am newbie in C programming. I am reading source codes and also writing very simple program for test.
Almost in every linux program I have seen some types that I do know not reason of definition those. for example file "sys/types.h" has a lot new types that most of them are same as another.
Why does programmers define new types that only differ in name.why does they use basic types,such as, int,or unsigned int or short int.instead they defined new type as named u_initX or etc.

Thanks for any help or guidance
You often don't need to know the actual types as they're defined for your platform and may change on a different kind of plaform.
That is done because while today size_t might be a synonym for unsigned int or unsigned long, tomorrow it could turn into something else, and library writers want to retain the right to change it. If, for example, std::string::size() returned int or unsigned, then in the future that could never change without breaking people's correct code that did

1
2
std::string str( "Hello World" );
int len = str.size();


Instead, std::string has a size_type typedef that typedefs size_t, which is itself a typedef of some fundamental type. Correct code is thus:

1
2
std::string str( "Hello World" );
std::string::size_type len = str.size();


And as such, such code would continue to compile and work even if std::string::size_type were changed to something else.
Topic archived. No new replies allowed.