Difference between size_t and unsigned int?

后端 未结 7 1058
清酒与你
清酒与你 2020-11-28 01:29

I am so confused about size_t. I have searched on the internet and everywhere mentioned that size_t is an unsigned type so, it can represent only n

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 02:08

    if it is use to represent non negative value so why we not using unsigned int instead of size_t

    Because unsigned int is not the only unsigned integer type. size_t could be any of unsigned char, unsigned short, unsigned int, unsigned long or unsigned long long, depending on the implementation.

    Second question is that size_t and unsigned int are interchangeable or not and if not then why?

    They aren't interchangeable, for the reason explained above ^^.

    And can anyone give me a good example of size_t and its brief working ?

    I don't quite get what you mean by "its brief working". It works like any other unsigned type (in particular, like the type it's typedeffed to). You are encouraged to use size_t when you are describing the size of an object. In particular, the sizeof operator and various standard library functions, such as strlen(), return size_t.

    Bonus: here's a good article about size_t (and the closely related ptrdiff_t type). It reasons very well why you should use it.

提交回复
热议问题