Know the C / C++ Data Types



Data types are classifications of data that define what kind of values a variable can hold and the operations that can be performed on it. They help programmers organize and manage data efficiently within a program.

The C & C++ programming languages, one of the oldest & yet being the most used is because they are one of the powerful, simple, robust & also light enough to be run on minimal hardware makes it more desirable programming skills with developers. A strong knowledge in these languages is important for being able to derive solutions using them.

However, as there were many vendors early on working with limited hardware resources in the 80's, & 90's, it gave rise to various flavors of these languages with varied datatype sizes & definitions. To standardize these discrepancies, a few newer sub-data-types were introduced that took away that anxiety & ambiguity. These will be discussed further on as we proceed.

Here's a breakdown of common data types:

Fundamental / Primary / Primitive Data Types:

  • Integer (int): Whole numbers without a fractional part (e.g., 10, -5, 0). 
  • Floating-point (float, double): Numbers with decimal points (e.g., 3.14, -2.5). 
  • Character (char): Single letters, numbers, or symbols (e.g., 'A', '5', '+'). 
  • Boolean (bool): Represents true or false values. 

Derived / Secondary / Non-Primitive Data Types:

  • String (str): Sequences of characters (e.g., "Hello, world!"). 
  • Array: A collection of similar data types stored under a single variable name. 
  • Object: A complex data type that can contain multiple data types and methods. 
  • Date and Time: Used to store date and time information. 

Other Important Considerations:

Primitive vs. Non-primitive:

Primitive data types are built-in, while non-primitive data types are created by combining primitive types. 

Data Type Categories:

Data types can be further categorized based on their characteristics, such as numeric, character, or boolean. 

Memory Allocation:

Each data type has a specific size in memory, which affects how much space is used to store data. 

Memory Allocation

Memory allocation is a vital topic with C & C++ languages, as it would decide the final outcome with the final software, which makes it compact, fast, precise & economical with respect to time, hardware & power resources.

Further there are specific & cross-platform data types which solve a specific or a generic purpose for ease of usage in such scenarios. Lets further discuss them one by one in a group.

uint8, int8, uint8_t and int8_t  || uint16 and int16 , uint16_t and int16_t || ...

With regards to embedded electronics & other resource limited platforms, the memory is one of the key areas the program is optimized in terms of performance & efficiency. So there were introduction of newer & specific data-types such as uintX, intX, where X is the size of bits for these data types. However, even here there were differences & ambiguity, so they used the uintX_t, intX_t, where the _t stresses the size to be exactly to X bits length across all platforms & devices universally.

uint8_t and int8_t are optional integer types of exactly 8 bits. Both types have no padding, and int8_t uses 2's complement. uint8_t is unsigned, and has the range zero to UINT8_MAX , which is [0, +255]. int8_t is signed, and has the range INT8_MIN to INT8_MAX , which is [−128, +127].

Likewise,

  • int16, uint16, int16_t, uint16_t uses 16 bit length data types
  • int32, uint32, int32_t, uint32_t uses 32 bit length data types,
  • int64, and so on..

Similarly, the other primary data-types, such as float & char are also derived & categorized as following.

User-Defined / Tertiary / Complex Data Types:

No comments:

Post a Comment