Fundamental data types

F

Data types are the type of information that can be stored in a variable. In a data type, we determine both the range of values ​​that a variable of a given type can take and the operations that can be performed on it. The following are the basic types of C language, together with a brief description of these:

1. char – represented by an 8-bit number (one byte). It can be either a signed char or an unsigned char.
2. int – Stores whole numbers. Its length (and implicit the value range) is dependent on the compiler and operating system considered. Generally, on Linux, int is represented by 32 bits (so 4 bytes). In this case, it can store numbers in the range [-2.147.483.648; 2,147,483,647].
3. float – represents a real number stored in floating-point in the value range 3.4E +/- 38. It generally complies with the IEEE 754 single-precision format, which means that its size will be 4 (bytes) and the number will have at least 7 accurate decimal places.
3. double – represents a real number stored in floating point in the 1.7E +/- 308 range. It generally complies with the IEEE 754 double-precision format, which means that its size will be 8 (bytes) and the number will have at least 15 accurate decimal places.

A number of specifiers can be added to these fundamental types as follows:
1. short – applicable only to int. The result type is at least 16 bits.
2. long – applicable for int and double. long int is guaranteed to have at least 32 bits. Linked to long double only guarantees that it is greater than or equal to size than double, which in turn is greater than or equal to float.
3. signed – applicable for int and char. A declared variable int is implicitly signed. The key word here is only for cases where we want to say this explicitly. Applied to char (⇒ signed char), we guarantee that that variable can have any value in the range [-128; 127].
4. unsigned – Specifies that the value of the variable is positive. Only applicable to whole types.
The four specifiers were divided into two groups of complementary specifiers. This means that the expression long signed int; It’s correct. Same as unsigned short int b ;. Definition of short long int c; but it is not correct. Also, not any speculator in the first category with one of the two can combine. For example, long signed char d; and long unsigned double e; are not correct. In C there are 3 “char”: char, signed char, unsigned char. This is different from int, for example, where it is guaranteed that the signed int is always the same type as int. For char this is not true: a declared variable char, may be, depending on the compiler / operating system either signed char or unsigned char. The difference is subtle but important when we want to write portable C code.

As a best practice, we use:
char – when we want to store an ASCII character.
signed char – when we want to store a whole of the [-128; 127].
unsigned char – when we want to store an integer from the [0; 255].
Sometimes we want to use types whose size is exactly specified as in structure work.
For this, we can use the types defined in the <stdint.h> header.
Determining correctly the types of data to use is essential to the security and smooth operation of the applications you write. If the value contained by a variable exceeds the limits imposed by the type of data used, the so-called over-flow can occur, which may cause unexplained errors.

Recent Posts

Archives

Categories