diff --git a/docs/B-Computations/types.md b/docs/B-Computations/types.md index 43530722..f352ac64 100644 --- a/docs/B-Computations/types.md +++ b/docs/B-Computations/types.md @@ -1,5 +1,8 @@ --- +id: types sidebar_position: 1 +title: Types +description: This chapter describes the basic types in C, how they differ in memory size, and how they can be used to declare variables --- # Types @@ -15,7 +18,9 @@ After reading this section, you will be able to: A typed programming language uses a type system to interpret the bit streams in memory. C is a typed programming language. A type is the rule that defines how to store values in memory and which operations are admissible on those values. A type defines the number of bytes available for storing values and hence the range of possible values. We use different types to store different information. The relation between types and raw memory is illustrated in the figure below. -![types](/img/types.png) +![Representation of memory allocation for basic types](/img/types.png) + +This chapter describes the four most common types in the C language and the ranges of values that these types allow. This chapter concludes by describing how to allocate memory for variables by identifying their contents using a type. This chapter describes the four most common types in the C language and the ranges of values that these types allow. This chapter concludes by describing how to allocate memory for variables by identifying their contents using a type. @@ -23,493 +28,480 @@ This chapter describes the four most common types in the C language and the rang The four most common types of the C language for performing arithmetic calculations are: -- char -- int -- float -- double +- `char` +- `int` +- `float` +- `double` A **`char`** occupies one byte and can store a small integer value, a single character or a single symbol: + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + +
char
1 Byte
        
char
1 Byte
        
+ +
+
An **`int`** occupies one word and can store an integer value. In a 32-bit environment, an **`int`** occupies 4 bytes: + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
int (32-bit environment)
1 Byte1 Byte1 Byte1 Byte
                                
int (32-bit environment)
1 Byte1 Byte1 Byte1 Byte
                                
+ +
+
A **`float`** typically occupies 4 bytes and can store a single-precision, floating-point number: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
float
1 Byte1 Byte1 Byte1 Byte
                                
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
float
1 Byte1 Byte1 Byte1 Byte
                                
+ +
+
A **`double`** typically occupies 8 bytes and can store a double-precision, floating-point number: + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
double
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
                                                                
double
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
                                                                
+ +
+
## Size Specifiers Size specifiers adjust the size of the **`int`** and **`double`** types. -### _int_ Type Size Specifiers +### `int` Type Size Specifiers Specifying the size of an int ensures that the type contains a minimum number of bits. The three specifiers are: -- short -- long -- long long - -A **`short int`** \(or simply, a short\) contains at least 16 bits: - +- `short` +- `long` +- `long long` + +A **`short int`** \(or simply, a `short`\) contains at least 16 bits: + + + + + + + + + + + + + + + + + + + + + + + + +
short
1 Byte1 Byte
                
+ +
+
+ +A **`long int`** \(or simply, a `long`\) contains at least 32 bits: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
long
1 Byte1 Byte1 Byte1 Byte
                                
+ +
+
+ +A **`long long int`** \(or simply, a `long long`\) contains at least 64 bits: + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - -
short
1 Byte1 Byte
                
- -A **`long int`** \(or simply, a long\) contains at least 32 bits: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
long
1 Byte1 Byte1 Byte1 Byte
                                
- -A **`long long int`** \(or simply, a long long\) contains at least 64 bits: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
long long
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
                                                                
long long
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
                                                                
+ +
+
The size of a simple **`int`** is no less than the size of a **`short`**. -### _double_ Type Size Specifier +### `double` Type Size Specifier The size of a **`long double`** depends on the environment and is typically at least 64 bits: + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
long double
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
                                                                
long double
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
                                                                
+ +
+
Specifying the **`long double`** type only ensures that it contains at least as many bits as a **`double`**. The C language does not require a **`long double`** to contain a minimum number of bits. -## const Qualifier +## `const` Qualifier Any type can hold a constant value. A constant value cannot be changed. To qualify a type as holding a constant value we use the keyword const. A type qualified as **`const`** is unmodifiable. That is, if a program instruction attempts to modify a **`const`** qualified type, the compiler will report an error. @@ -517,16 +509,16 @@ Any type can hold a constant value. A constant value cannot be changed. To quali Hardware manufacturers distinguish integral types from floating-point types and represent integral data and floating-point data differently. -- integral types: **`char int`** -- floating-point types: **`float double`** +- integral types: **`char` `int`** +- floating-point types: **`float` `double`** ### Integral Types -C stores the char and int data in equivalent binary form. Binary form represents the value stored exactly. To learn how to convert between decimal and binary representation refer to the appendix entitled [Data Conversions](../Resources-Appendices/data-conversions.md). +C stores the `char` and `int` data in equivalent binary form. Binary form represents the value stored exactly. To learn how to convert between decimal and binary representation refer to the appendix entitled [Data Conversions](../Resources-Appendices/data-conversions.md). ### Characters and Symbols -C stores characters and symbols in char types. Since characters and symbols have no intrinsic binary representation, the host platform provides the collating sequence for associating each character and symbol with a unique integer value. C stores the integer value from this sequence as the representative of the character or symbol. +C stores characters and symbols in `char` types. Since characters and symbols have no intrinsic binary representation, the host platform provides the collating sequence for associating each character and symbol with a unique integer value. C stores the integer value from this sequence as the representative of the character or symbol. The two popular collating sequences are ASCII and EBCDIC. ASCII is more popular. [ASCII](../Resources-Appendices/ascii-collating-sequence.md) represents the letter A by the bit pattern 010000012, that is the hexadecimal value 0x41, that is the decimal value 65. [EBCDIC](../Resources-Appendices/ebcdic-collating-sequence.md) represents the letter A by the bit pattern 110000012, that is the hexadecimal value 0xC1, that is the decimal value 193. @@ -552,63 +544,51 @@ To obtain the 2's complement of an integer, we For example, we represent the integer -92 by 101001002 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Bit #76543210
92 =>01011100
Flip Bits10100011
Add 100000001
-92 =>10100100
Bit #76543210
92 =>01011100
Flip Bits10100011
Add 100000001
-92 =>10100100
### Floating-Point Data @@ -617,151 +597,145 @@ Floating-point types store tiny as well as huge values by decomposing the values The most popular model is the IEEE \(I-triple-E or Institute of Electrical and Electronics Engineers\) Standard 754 for Binary and Floating-Point Arithmetic. Under IEEE 754, a float has 32 bits, consisting of one sign bit, an 8-bit exponent and a 23-bit significand \(or mantissa\): + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
float
1 Byte1 Byte1 Byte1 Byte
sexponentsignificand
                                
float
1 Byte1 Byte1 Byte1 Byte
sexponentsignificand
                                
+ +
+
Under IEEE 754, a double occupies 64 bits, has one sign bit, an 11-bit exponent and a 52-bit significand: + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
double
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
sexponentsignificand
                                                                
double
1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte1 Byte
sexponentsignificand
                                                                
+ +
+
-Since the number of bits in the significand is limited, the float and double types cannot store all possible floating-point values exactly. That is, the floating-point types store values approximately. +Since the number of bits in the significand is limited, the `float` and `double` types cannot store all possible floating-point values exactly. That is, the floating-point types store values approximately. ## Value Ranges @@ -771,115 +745,33 @@ The number of bytes allocated for a type determines the range of values that tha The ranges of values for the integral types are shown below. Ranges for some types depend on the execution environment: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TypeSizeMinMax
char8 bits-128127
char8 bits0255
short>= 16 bits-32,76832,767
int2 bytes-32,76832,767
int4 bytes-2,147,483,6482,147,483,647
long>= 32 bits-2,147,483,6482,147,483,647
long long>= 64 bits-9,233,372,036,854,775,8089,233,372,036,854,775,807
+| Type | Size | Min | Max | +| ----------- | :--------: | :------------------------: | :-----------------------: | +| `char` | 8 bits | -128 | 127 | +| `char` | 8 bits | 0 | 255 | +| `short` | >= 16 bits | -32,768 | 32,767 | +| `int` | 2 bytes | -32,768 | 32,767 | +| `int` | 4 bytes | -2,147,483,648 | 2,147,483,647 | +| `long` | >= 32 bits | -2,147,483,648 | 2,147,483,647 | +| `long long` | >= 64 bits | -9,233,372,036,854,775,808 | 9,233,372,036,854,775,807 | ### Floating-Point Types The limits on a float and double depend on the execution environment: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TypeSizeSignificantMin ExponentMax Exponent
floatminimum6-3737
floattypical6-3737
doubleminimum10-3737
doubletypical15-307307
long doubletypical15-307307
+| Type | Size | Significant | Min Exponent | Max Exponent | +| ------------- | ------- | :---------: | :----------: | :----------: | +| `float` | minimum | 6 | -37 | 37 | +| `float` | typical | 6 | -37 | 37 | +| `double` | minimum | 10 | -37 | 37 | +| `double` | typical | 15 | -307 | 307 | +| `long double` | typical | 15 | -307 | 307 | -Note that both the number of significant digits and the range of the exponent are limited. The limits on the exponent are in base 10. +:::note + +Both the number of significant digits and the range of the exponent are limited. The limits on the exponent are in base 10. + +::: ## Variable Declarations @@ -887,7 +779,7 @@ We store program data in variables A declaration associates a program variable w In C, a declaration takes the form: -```text +```c [const] type identifier [= initial value]; ``` @@ -921,10 +813,10 @@ For example, We may select any identifier for a variable that satisfies the following naming conventions: -- starts with a letter or an underscore \(\_\) -- contains any combination of letters, digits and underscores \(\_\) +- starts with a letter or an underscore \(`_`\) +- contains any combination of letters, digits and underscores \(`_`\) - contains less than 32 characters \(some compilers allow more, others do not\) -- is not be a C reserved word +- is not be a [C reserved word](#reserved-words) ### Reserved Words