Find the answer to your Linux question:
Results 1 to 8 of 8
Hello everyone!! When I started learning C from a book, it was mentioned that sizeof(int) is 2 bytes. I believed it blindly and never bothered to try it!! But when ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Location
    India
    Posts
    24

    sizeof(int)


    Hello everyone!!

    When I started learning C from a book, it was mentioned that sizeof(int) is 2 bytes. I believed it blindly and never bothered to try it!!
    But when I did, I was surprised to see that the sizeof(int) is actually 4 bytes (the same as that of long int). I was told that it was because I tried on a 32-bit machine! I know that on a 16-bit machine it is 2 bytes, as it should be!!
    Can neone explain how the sizeof(int) depends on the how_many-bit machine I'm using, while the sizeof(other_datatypes) don't change??

    Also how much will the sizeof(int) be on a 64-bit machine??

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2005
    Location
    Hell
    Posts
    514
    The C standard doesn't specify the sizes of the integer types, only some vague relations between them; and the actual sizes differ between implementation choices; see http://en.wikipedia.org/wiki/C_varia...larations#Size

  3. #3
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,906
    An int is usually the size of a single register on the processor on which your software is built to run. So for 16 bit operating systems (e.g. Windows 3.1, MSDOS), that'll be 16 bits, for 64 bit operating systems, its 64 bits.

    You really shouldn't be relying on integers being a specific size, if you want to use fixed-size integers, then you'll find there are other declarations that you can use, but take care here, some of these can be compiler-based.
    Linux user #126863 - see http://linuxcounter.net/

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401

    inttypes.h

    Uses declarations from inttypes.h

    This header defines symbols wich are architecture independent...

    uint32_t -> unsigned 32 bit integer
    int8_t -> signed 8 bit integer
    ...
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  6. #5
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Quote Originally Posted by Roxoff
    An int is usually the size of a single register on the processor on which your software is built to run. So for 16 bit operating systems (e.g. Windows 3.1, MSDOS), that'll be 16 bits, for 64 bit operating systems, its 64 bits.
    A counter-example.....
    sparc64 is a 64-bit CPU - the linux kernel needs to be compiled explicitly for 64-bit on it whereas all user-space apps are 32-bit natively (they use the sparc32 ABI). Check this link for details:
    http://www.ultralinux.org/faq.html#q_1_5
    The Unforgiven
    Registered Linux User #358564

  7. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    I was always led to believe that a C int was the "natural" word length of the machine, but all the 64-bit implementations I've seen have 32 bit ints. As noted elsewhere, all that the C spec guarantees is that a short is no longer than a long.

  8. #7
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86

    sizeof(int)

    In C, by definition in an integer must at a minimum be able to represent -32768 to 32767, and hence unsigned 0-65535, larger than this is allowed but not required. For example an eight bit microcontroller must deal with the minimum representation limitations (char would be its "natural" type) and 64-bit compiler support is still in its infancy so a 32-bit int is legal.

  9. #8
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86

    Side note

    BTW: Most 32-bit processors support intermediate 64-bit multiplication results. Some chip manufacturers incorrectly use this fact to advertise 32-bit procs as 64-bit when infact they are not. So long as the internal data bus is 32-bit, 64-bit register-to-register transfers and mathematical operations require multiple move instructions and carry bit checking, thus are multi-precision operations and are not the natural processor's 'size.' This isn't so much a problem today as true 64-bit procs with 128-bit product registers exist, however compilier support and testing is still growing, so it is quite likely that your 64-bit proc is running a lot of 32-bit code.

Posting Permissions

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