C unsigned long
Data Type
In C, the unsigned long
data type is used to store large positive whole numbers. Unlike long
, which can store both positive and negative values, unsigned long
can only store non-negative values, but it has a greater positive range.
1 Storage Size of unsigned long
Data Type
The storage size of unsigned long
depends on the system architecture and compiler. Typically, the sizes are:
System / Model | unsigned long Size |
---|---|
32-bit system (ILP32) | 4 bytes (32 bits) |
64-bit Windows (LLP64) | 4 bytes (32 bits) |
64-bit Linux/macOS (LP64) | 8 bytes (64 bits) |
2 Values Stored by unsigned long
Data Type
The unsigned long
data type can store only positive whole numbers, including zero. It does not support negative values.
Example values that can be stored in unsigned long
:
0, 1000, 4294967295 (on a 32-bit system), 18446744073709551615 (on a 64-bit system)
3 Example: Declaring and Using unsigned long
Variables
Let’s see a simple program demonstrating how to declare and use unsigned long
variables in C.
main.c
#include <stdio.h>
int main() {
unsigned long num1 = 100000;
unsigned long num2 = 500000;
unsigned long sum = num1 + num2;
printf("Number 1: %lu\n", num1);
printf("Number 2: %lu\n", num2);
printf("Sum: %lu\n", sum);
return 0;
}
Explanation:
- We declare three
unsigned long
variables:num1
,num2
, andsum
. - We assign values 100,000 and 500,000 to
num1
andnum2
respectively. - The sum of these numbers is stored in
sum
. - We use
printf()
with the%lu
format specifier to print the values.
Output:
Number 1: 100000
Number 2: 500000
Sum: 600000
4 Checking Storage Size of unsigned long
Programmatically
We can determine the storage size of unsigned long
using the sizeof
operator.
main.c
#include <stdio.h>
int main() {
printf("Size of unsigned long: %lu bytes\n", sizeof(unsigned long));
return 0;
}
Output (varies based on system architecture):
Size of unsigned long: 8 bytes
5 Minimum and Maximum Values of unsigned long
The range of values an unsigned long
can store depends on its size:
Storage Size | Minimum Value | Maximum Value |
---|---|---|
4 bytes | 0 | 4,294,967,295 |
8 bytes | 0 | 18,446,744,073,709,551,615 |
6 Getting Maximum Value of unsigned long
Programmatically
The maximum value of an unsigned long
can be retrieved using limits.h
.
main.c
#include <stdio.h>
#include <limits.h>
int main() {
printf("Maximum unsigned long value: %lu\n", ULONG_MAX);
return 0;
}
Output:
Maximum unsigned long value: 18446744073709551615
Conclusion
In this tutorial, we explored the unsigned long
data type in C, including:
- Its ability to store large positive whole numbers.
- Its typical storage size of 4 or 8 bytes, depending on the system.
- How to get the storage size programmatically using
sizeof()
. - The maximum value that
unsigned long
can store. - How to retrieve the maximum value using
limits.h
.
The unsigned long
data type is useful when dealing with large numbers that do not require negative values.