Why are there so many custom data types like socklen_t, ssize_t, size_t, uint16_t? I don't understand the real need for them. To me, they’re just a bunch of new variable names to be learned.
Data types are required so that you know what "kind" of data your variable holds, or can hold. If you (and compiler, and runtime) know this information in advance, you can save a hell lot of runtime issues at compile time only, and also for utilizing the minimum memory space. thank you!
A custom data type (CDT) is a designer-defined data structure that represents a logical grouping of related data, such as Employee and Contract. CDTs can be used to read from and write to a database table, to store information within a process, or to define inputs or outputs of a web service or Appian plug-in.
C++ mixes up things a bit because it adds a data-type for handling strings. To reduce ambiguity, string is only used for the abstract concept, you have to rely on the context to disambiguate or be more explicit.
We can perform many operations (sum, average, concatenation, matching, etc.) if the data is stored in the correct format and with correct types. That is why we have so many data types in C so that we can differentiate and segregate data in the best possible way. There are 4 Data types in C:
Structure is the most commonly used custom data type. When you need to work with different variables of different data types (i.e. char, int, float ) for a specific task, you have to use structure.
Array, pointers, struct, and union are the derived data types in C. Same as any other language, Array in C stores multiple values of the same data type. That means we can have an array of integers, chars, floats, doubles, etc
Structure members are grouped in a curly braces after the tag part. All the members which are needed to build the new data type are listed here. In previous example, new custom data type employee consists of name, designation, department, age and salary. These members are grouped along with their data type after the tag part.
Intent and portability.
For example, let's say I have a variable unsinged n
. An unsigned integer can represent many things, so it's intent is not clear. But when I write size_t n
, it is clear that n
represents size of something. When I write socklen_t n
it is clear that n
represents the length of something related to socket.
Second reason is portability. For example, socklen_t
is guaranteed to be at least 32 bits. Now if we just write unsigned n
then size of n
may be less than 32 bits. size_t
can hold the size of any object, but the actual value is implementation defined. When we use plain integer it may happen that sizeof(int)
can't hold the size of largest object that is theoretically possible. But using size_t
doesn't have such portability issue.
uint16_t
clearly says that it is unsigned integer of 16 bits which is both clear and portable than using unsigned int
or unsigned short
.
Firstly, It provides more readability.
Secondly, while programming for embedded systems or cross platform or when portability is important, it is necessary of be explicit about the size of the data type that you are using - using these specific data types avoids confusion and guaranteed to be having the defined data width.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With