-
ANSI C question
Hi!
I'm just wondering. Length of data types is dependable on system software (OS). Now, tell me, if this piece of code works on 64-bit machines? Because on 32 bit machines it doesn't - integer is only 32k long.
Code:
int oldVal = 0, newVal;
for (n = 0; n <= 100; n++) {
newVal = n^2 + oldVal; //this line!
oldVal=newVal;
printf("%d", newVal);
}
Sorry for bothering you, but we had exam and this exercise was in it. I used integer instead of long and I'm just wandering, if it works on 64-bit platform, then I've done it right... :D Because noone specified it has to be for 32-bit platform... :rolleyes:
Zvonko
-
The long long datatype (part of C99 standards) will support this if any datatype will, as it is the largest integer type available on any machine. By definition. On some Cray systems it is 128 bit.
There is nothing explicitly stated in the ansi C standard about sizes of datatypes for integers and longs. The reason is that as you go from machine to machine things are different.
Implementations of C vary. For example under HPUX 11.0 and higher, int is 32 bit, long is 32 bit, long long is 64. Under other implementations int can be 32 bit, long 64 bit. On PC's with 486 and older CPU's 32 bit is long, and 16 bit is integer. These limitations are imposed by system architecture.
-
The SGI systems have a 128-bit long long, I think (or was that the long double :confused:).
I can find out when I get back to work on wednesday...
-
What about new 64-bit P4s and Athlons?
So:
- 486 and older: int = 16bit, long=32bit
- Pentiums, PII, PIII, P4, Athlon...: int = 32bit, long=64bit
- New 64bit arhitecture: int=64bit???
Are my conclusions right?
-
386 and upwards can use a 32-bit int, it's a 16-bit compiler that will limit you to a 16-bit int.
286s had a 16-bit limit I think.
Most of the newer 64-bit systems seem to be staying with a 32-bit int for portability (but if you want to store a pointer in anything you need to make sure you've allocated sufficient space - MSVC 7 warns about this).
-
Thank you for you explanation.
Zvonko
-
even school pcs are modern enough by now that ints are 32 bit. I think like you have it now no overflow should occur.
On 64-bit compilers:
int: 32
long: 32!
long long (= LONGLONG = _int64 in VC): 64
I think VC++ does not support the name long long.
BTW the int size is partly defined by the OS: normal Dos: 16, extended DOS 32, Win16 16, Win32 32.
-
VC++ has __int64, but that's not exactly portable :rolleyes: