0

I read about int vs size_t vs ssize_t , and I understand that int and ssize_t is signed while size_t is unsigned.

Why memcmp return int and no return ssize_t like recv return ssize_t?

yfr24493AzzrggAcom
  • 159
  • 1
  • 2
  • 13

1 Answers1

2

An int is sufficient to hold the return value of memcmp.

The memcmp function returns 0 if the two given memory regions are equal, a value less than 0 if the first region compares less, and a value greater than 0 if the first region compares more.

A ssize_t may be larger than an int (and on most implementations it will be), and an int is typically the "natural" word size, so there is no benefit to using the larger size.

Jardel Lucca
  • 1,115
  • 3
  • 10
  • 19
dbush
  • 205,898
  • 23
  • 218
  • 273
  • So we will use int when to small signed number . ssize_t to bigger signed numbers. unsigned int to small unsigned number And size_t to bigger unsigned number? – yfr24493AzzrggAcom Jun 28 '21 at 20:41
  • 2
    @yfr24493AzzrggAcom `size_t` and `ssize_t` are typically used when the value represents the length of a memory buffer. There are other types that can be used for larger or smaller values. In general, `int` should be used unless there's a reason to do so otherwise. – dbush Jun 28 '21 at 20:44
  • 1
    Additionally, `ssize_t` is a POSIX data type, not a standard C data type. The standard version of `memcmp()` could not, therefore, return `ssize_t`. – Jonathan Leffler Jun 28 '21 at 23:21