Fundamental Data Types − Integer

Fundamental Data Types − Integer in C

Have you ever wondered why a simple number like 5 can sometimes behave differently in different computers? Or why your friend’s program works perfectly on their laptop but crashes on yours? The answer often hides in a small but powerful concept: how integers are stored in memory. In this blog, I will walk you through the integer data type from the ground up. By the end, you will know exactly how much memory integers use, what range of values they can hold, and how to find these limits yourself.


Why This Topic Matters


If you are taking an introductory programming course, understanding integer size and range is essential for exams. Questions about sizeof, overflow, and signed vs unsigned ranges appear frequently. In the real world, this knowledge prevents bugs when working with embedded systems, game development, or network protocols. Imagine storing a sensor reading in a small microcontroller—if you choose the wrong integer size, your data silently wraps around and gives false results. I have seen this happen more times than I can count.


The Foundation: Bytes and Bits


Before we talk about integers, let us build the base. A computer stores everything in bits. Each bit is either 0 or 1. Eight bits together make one byte. This is not just trivia—it directly determines how much information an integer can hold.


Here is the key idea: the more bytes an integer takes, the larger the range of numbers it can represent. If an integer uses 2 bytes, that means 16 bits. If it uses 4 bytes, that means 32 bits. More bits mean a bigger power of two combinations available.


Comparison of short int and int in C Programming


Finding Integer Size Programmatically


You do not need to guess how many bytes an integer uses on your machine. C and C++ provide the sizeof operator. Let me clarify an important point here: sizeof is a unary operator, not a function. Many beginners write sizeof(int) and think it is a function because of the parentheses. But the parentheses are only needed when you use it with a type name. You could also write sizeof x if x is a variable.


Try this small program on your computer:


#include <stdio.h>
int main() {
    printf("%zu\n", sizeof(int));
    return 0;
}

On my machine, this prints 4, meaning 4 bytes or 32 bits. On some older or embedded systems, it might print 2. This variation is exactly why you need to know how to handle ranges.


Understanding Range Through Binary Numbers


To grasp integer range, we must first understand the binary number system. Humans use decimal (base 10), where digits go from 0 to 9. Computers use binary (base 2), where digits are only 0 and 1. Let me show you why this matters.


Take the decimal number 568. We write it as: \( 5 \times 10^2 + 6 \times 10^1 + 8 \times 10^0 = 500 + 60 + 8 = 568 \)


Now take a binary number like 1100 (4 bits). The place values are powers of 2: \( 1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 0 \times 2^0 = 8 + 4 + 0 + 0 = 12 \)


So 1100 in binary equals 12 in decimal.


Understanding Range Through Binary Numbers in C Programming


Calculating the Range for Unsigned Integers


For an unsigned integer, all bits represent the magnitude. There is no sign bit. The minimum value is always 0 (all bits zero). The maximum value occurs when all bits are 1.


For n bits, the maximum value is: \[ 2^n - 1 \]


Let us test this with 4 bits: \( 2^4 - 1 = 16 - 1 = 15 \) That matches our manual calculation: 8+4+2+1 = 15.


Now apply this to real integer sizes:


  • If sizeof(int) = 2 bytes (16 bits), the unsigned range is from 0 to \( 2^{16} - 1 = 65535 \).
  • If sizeof(int) = 4 bytes (32 bits), the unsigned range is from 0 to \( 2^{32} - 1 = 4294967295 \).

Signed Integers and Two's Complement


What about negative numbers? Most computers use two's complement representation. I will not dive into the full digital electronics derivation here, but you need the final formula.


For n bits in two's complement:


  • Minimum (most negative) value: \( -2^{n-1} \)
  • Maximum (most positive) value: \( 2^{n-1} - 1 \)

Notice the asymmetry: there is one more negative number than positive. For 16 bits:


  • Minimum: \( -2^{15} = -32768 \)
  • Maximum: \( 2^{15} - 1 = 32767 \)

For 32 bits:


  • Minimum: \( -2^{31} = -2147483648 \)
  • Maximum: \( 2^{31} - 1 = 2147483647 \)

Worked Example: Putting It All Together


Let us say you are writing code for a weather station. The temperature sensor outputs an unsigned integer from 0 to 5000. Your microcontroller uses 2-byte integers. Is this safe?


Step 1: Determine the maximum unsigned value for 2 bytes. \( 2^{16} - 1 = 65535 \)


Step 2: Compare with your sensor range. Sensor maximum = 5000, which is less than 65535. So it fits easily.


Step 3: What if you accidentally used a signed integer? Signed 2-byte range goes up to 32767. Still safe for 5000.


But what if the sensor output reached 40000? That would exceed the signed positive limit and wrap around to a negative number, corrupting your data. Always check your range before choosing a data type.


Common Mistakes Students Make


I have graded many exams and seen the same errors repeatedly. Here are the top three:


  1. Confusing sizeof as a function: It is an operator. The parentheses work with types but are not required for variables. No, you cannot say sizeof int without parentheses—that is a syntax error.


  2. Forgetting the minus one in maximum value: The formula is \( 2^n - 1 \), not \( 2^n \). For 4 bits, \( 2^4 = 16 \), but the maximum value is 15 because we start counting from 0. This small mistake costs many points.


  3. Mixing up signed and unsigned ranges: A signed 16-bit integer cannot hold 60000, but an unsigned one can. Know which one you are using.



Quick Exam Tips and Shortcuts


Here are some tricks I share with my students:


  • Memorize the key powers of two: \( 2^{10} = 1024 \), \( 2^{16} = 65536 \), \( 2^{32} = 4294967296 \). Then subtract one for the unsigned maximum.
  • For signed 16-bit: remember -32768 to 32767. The negative limit is exactly half of 65536.
  • For signed 32-bit: the positive limit is roughly 2.147 billion. The negative limit is -2.147 billion minus one more.
  • When an exam question gives you a size in bytes, first convert to bits (multiply by 8), then apply the formulas.

Summary


Let me bring everything together. The integer data type takes either 2 bytes or 4 bytes depending on your machine. You can find the exact size using the sizeof operator. The range of an unsigned integer is from 0 to \( 2^n - 1 \), where n is the number of bits. For signed integers using two's complement, the range is from \( -2^{n-1} \) to \( 2^{n-1} - 1 \). Understanding these ranges helps you choose the correct data type and avoid overflow bugs. Next time you write a program, take a moment to check your integer sizes—your future self will thank you.


#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!