-2

I wrote a program to find the last Fibonacci number using type unsigned int. It is 1836311903 but I thought the max values for an unsigned int is 65535. So what's going on?

while(true)
    {
        sequence[j] = sequence[j-1] + sequence[j-2];
        if(sequence[j-1]>sequence[j]) break;
        j++;
    }

    printf("%d", sequence[j-2]);
2
  • 1
    Unless you have 2 byte unsigned ints, the max number in unsigned int is definitely bigger than 65535.
    – wolfPack88
    Oct 8, 2014 at 19:56
  • How do you “know” that the largest unsigned int is 65535? Oct 8, 2014 at 20:00

3 Answers 3

10

You are mistaken in your belief that max number for unsigned int is 65535. That hasn't been the case for most compilers since perhaps early days of windows 95 when we had 16-bit processors.

The standards do NOT define the size of any integral type; they only define the relationship of the sizes between one another. (long long >= long >= int >= short >= char... etc) The actual sizes though incredibly common and consistent are defined by the implementation of your compiler and thus are generally platform defined.

That not withstanding most int's use the size of a word on the processor; which today is often 32bits or 64bits.

You could verify 'why' yourself by taking sizeof(int); then raise 2 to that power subtract 1 and you've got your answer for max int...

A better way would be to #include <limits.h> or #include <climits> and use the values it defines. In C++ you can use std::numeric_limits<unsigned int>::max() as well.

4
  • 1
    In c++, we would actually use std::numeric_limits<unsigned int>::max().
    – Bill Lynch
    Oct 8, 2014 at 19:59
  • Yes I'm aware, I went off C standard since I knew it better. I'll add that in
    – UpAndAdam
    Oct 8, 2014 at 20:01
  • @sharth is that a c++11 thing?
    – UpAndAdam
    Oct 8, 2014 at 20:02
  • No. It's been around since at least the first c++ standard in 1998.
    – Bill Lynch
    Oct 8, 2014 at 20:05
4

As shown in http://www.tutorialspoint.com/cprogramming/c_data_types.htm (and many other places), unsigned int can be 2 bytes or 4 bytes. In your case, you are using 4 bytes so the maximum is 4,294,967,295.

The maximum you mention, 65535 corresponds to 2 bytes.

0

The following code will give you the max value for an unsigned int on your system:

#include <stdio.h>

typedef unsigned int        ui;

int main()
{
    ui uimax = ~0;

    printf("uimax  %u\n",uimax);

    return 0;
}

Int types only define the relationship between their sizes not their actual size e.g.

unsigned long long int >= unsigned long int >= unsigned int >= unsigned short int >= unsigned char

3
  • 2
    It would be clearer to use one of the built-in facilities to get the maximum size.
    – Neil Kirk
    Nov 30, 2015 at 0:09
  • By clearer do you mean easier?? Its just I don't know how much clearer I can get... Sorry I'm a noob at this forum stuff..... Nov 30, 2015 at 0:22
  • 2
    std::numeric_limits<unsigned int>::max(), although wordy, makes the intention of the value clear.
    – Neil Kirk
    Nov 30, 2015 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.