The integer is a fundamental (āĻŽā§āϞāĻŋāĻ) data type in C programming language.
An integer variable can occupy either 2 bytes or 4 bytes of memory space, depending entirely on the underlying architecture of the machine.
Since 1 byte is equal to 8 bits, a 2-byte integer corresponds to 16 bits, whereas a 4-byte integer corresponds to 32 bits. A larger allocation size inherently implies that the variable can hold more substantial data content.
To programmatically (āĻĒā§āϰā§āĻā§āϰāĻžāĻŽāĻāϤāĻāĻžāĻŦā§) determine the exact memory size of a data type during execution, C provides the sizeof operator.
It is crucial to note that sizeof is a unary operator (āĻāĻāĻ āĻ āĻĒāĻžāϰā§āĻāϰ) and not a function, despite its functional looking syntax.
If the system architecture allocates 4 bytes for an integer, evaluating sizeof(int) within a printf function will output the value 4.
The term range refers to the defined upper and lower limits (āϏā§āĻŽāĻž) of a specific set of data.
For instance, in a data set containing {0, 1, 2, 3, 4}, the minimum value is 0 and the maximum value is 4, which establishes a definitive range from 0 to 4. No elements smaller than 0 or larger than 4 can exist within this boundaries.
Evaluating integer ranges requires a prerequisite (āĻĒā§āϰā§āĻŦāĻļāϰā§āϤ) comprehension of the Decimal number system.
This system is human-understandable and is formally called a base 10 number system, meaning it possesses a digit range strictly from 0 to 9.
For example, the number 568 is parsed by multiplying each constituent digit by its respective place values (āϏā§āĻĨāĻžāύā§āϝāĻŧ āĻŽāĻžāύ) starting from the rightmost end (\(8 \times 10^0 = 8\), \(6 \times 10^1 = 60\), \(5 \times 10^2 = 500\)), and adding them up to compute the final result of 568.
Computers cannot interpret the decimal configuration; instead, they operate on the Binary number system, which is a base 2 number system containing only two digits: 0 and 1.
To convert a 4-bit binary sequence like 1100 into its decimal equivalent, each digit is multiplied by its base-2 place values (\(2^0, 2^1, 2^2, 2^3\)) from right to left. The summation process is: \((0 \times 1) + (0 \times 2) + (1 \times 4) + (1 \times 8) = 12\).
For any standard 4-bit allocation, the minimum value is 0 (when all positions are 0) and the maximum value is 15 (when all positions are 1).
To efficiently calculate the maximum unsigned value for larger bit widths without manual expansion, the mathematical formula \(2^n - 1\) is highly handy (āĻāĻĒāϝā§āĻā§). Substituting \(n = 4\) bit positions yields \(2^4 - 1 = 15\), defining the range from 0 to 15.
2-Byte Integers (16 bits): On systems supporting 2-byte allocations, the total Unsigned range spans from 0 to 65,535 based on the formula \(2^{16} - 1\).
Signed Representations: To represent negative values alongside positive values, systems use signed magnitude, 1's complement, or 2's complement representations.
2's Complement Range: Most modern computer systems exclusively utilize the 2's complement representation, which defines its boundaries using the formula: \(-2^{n-1}\) to \(+2^{n-1} - 1\). For a 16-bit space (\(n=16\)), the mathematical evaluation establishes a signed range from \(-32,768\) to \(+32,767\).
4-Byte Integers (32 bits): On modern machines supporting 4-byte allocations, the Unsigned range expands significantly from 0 to 4,294,967,295, and the corresponding signed limit is obtained symmetrically by applying the same 2's complement limits.