Results 1 to 8 of 8

Thread: ANSI C question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242

    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... Because noone specified it has to be for 32-bit platform...

    Zvonko
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  2. #2
    jim mcnamara
    Guest
    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.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The SGI systems have a 128-bit long long, I think (or was that the long double ).

    I can find out when I get back to work on wednesday...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    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?
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Thank you for you explanation.

    Zvonko
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    VC++ has __int64, but that's not exactly portable
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width