Reputation: 151186
If I remember correctly, on some machine, int
was 16 bit, and when we move onto the 32 bit platform, then int
was 32 bit.
Now that Snow Leopard and Lion are 64 bit, can a C or Objective-C program be compiled on Xcode so that int
is 64 bit? (and "%d" or "%i" will take a 64 bit integer as well). Or is int
kept as 32-bit for compatibility reason?
(and if using 64 bit int
, will it be faster than 32 bit because 64 bit is native?)
Update: I just found out sizeof(NSInteger)
if printed in a console app by Xcode on Lion is 8 (it is typedef as long), and if it is on iOS 5.1.1, then it is 4 (typedef as int). sizeof(int)
is 4 on both platforms. So it looks like in a way, int
moved from 16 bit to 32 bit before, but now we do want to stop it at 32 bit.
Upvotes: 2
Views: 1342
Reputation: 52602
To get a compiler where int = 64 bit, you'd have to download the Clang source code and modify it by hand. So with existing gcc or Clang compilers, there's no way.
But there is a good reason for int being 32 bits: Lots of code needs for compatibility reasons the ability to have 8 bit, 16 bit, 32 bit, and 64 bit items. The types available in C are char, short, int, long, and long long. You wouldn't want long or long long being smaller than int. If int = 64 bit, you'd only have two types (char and short) for three sizes (8, 16 and 32 bit), so you'd have to give up one of them.
Upvotes: 0
Reputation: 3648
Under the LP64 data model, as adopted by Apple, an int will always be 32-bits. Some compilers might allow you to use the ILP64 data model instead, as explained in the comments here. However, you will likely break compatibility with precompiled libraries.
Upvotes: 3
Reputation: 86671
can a C or Objective-C program be compiled on Xcode so that int is 64 bit?
I have been unable to find a clang option that will make int
64 bits wide. In fact, the platform headers seem to assume that it can't be. Also, you would be unable to use any platform library functions / methods that take an int
as a parameter (that includes things like printf()
that specify int
types in the format string).
and if using 64 bit int, will it be faster than 32 bit because 64 bit is native?
I see no reason why using 32 bit ints
would be slower than using 64 bit ints
. In fact, possibly, they might be faster since you can fit twice as many of them in the cache.
Upvotes: 1