I have the following simple program that reads in a number given as a string and prints it. It works for small numbers, but when I try to use numbers of size unsigned long, like "18446744073709551615", it does not yield the expected results. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readNumbers(char* numbers, unsigned long numbers_length, unsigned long* number)
{
char string[numbers_length + 1];
strncpy(string, numbers + 0, numbers_length);
*number = strtoul(string, NULL, 10);
}
int main () {
unsigned long number;
char* numbers = "18446744073709551615";
unsigned long numbers_length = strlen(numbers);
readNumbers(numbers, numbers_length, &number);
printf("X = %lu \n", number); // prints 4294967295
return 0;
}
Edit_1: According to this site the maximum value of unsigned long is 18446744073709551615.
Edit_2: The following code works on my system:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
void readNumbers(char* numbers, int numbers_length, unsigned long long* number)
{
char string[numbers_length + 1];
strncpy(string, numbers + 0, numbers_length);
string[numbers_length] = '\0';
*number = strtoull(string, NULL, 10);
}
int main () {
unsigned long long number;
char* numbers = "18446744073709551615";
int numbers_length = strlen(numbers);
readNumbers(numbers, numbers_length, &number);
printf("X = %llu \n", number);
return 0;
}
The ISO C standard only requires that the data type
unsigned longis able to represent numbers in the range from0to4,294,967,295.On some platforms, it is common for
unsigned longto be able to represent larger numbers than required by the ISO C standard. For example, on 64-bit Linux, it is common forunsigned longto be able to represent numbers up to18,446,744,073,709,551,615. But on other platforms, such as 64-bit Microsoft Windows, it is common forunsigned longto only be able to represent numbers up to4,294,967,295, i.e. the minimum amount required by the ISO C standard.In contrast to
unsigned long, the data typeunsigned long longis guaranteed by the ISO C standard to be able to represent values up to at least18,446,744,073,709,551,615on all platforms.In order to determine the maximum number that is representable in an
unsigned longon your platform, you can check the value of the macro constantULONG_MAX.Similarly, in order to determine the maximum number that is representable in an
unsigned long longon your platform, you can check the value of the macro constantULLONG_MAX.Here is an example program:
On 64-bit Microsoft Windows using MS Visual Studio, I get the following output:
On 64-bit Linux, I get the following output:
UINT_MAXis the maximum value representable in anunsigned int.