Thread: Word size of processor

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    Word size of processor

    How can I find out my system word size??Can anybody tell me "C" Program to find out word size??

    Ex: if it 32 bit then should print 32-bit, if it is 64 bit should print 64-bit

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
            printf("%d\n", sizeof(void *) * CHAR_BIT);
            return 0;
    }
    should do it

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > How can I find out my system word size?
    With great difficulty, if you're trying to do it in code.

    Just take sl4nted's answer and compile it using TurboC on your nice new 32 or 64 bit machine, and you'll get obviously wrong answers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Are you referring to an ancient version of 16 bit Turbo C running under a 32 bit windows, Salem, or something else? If you're referring to something else ...... more info please!

    Assuming you have a native compiler (a compiler that specifically targets the type of system your application will run on), sl4nted's answer is a good starting point.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    maybe cause I'm assuming an address is the size of a word. I'm also assuming CHAR_BIT is the amount of bits in a byte. which in my experiece are both fair assumptions. I'd like to know why these assumptions aren't correct.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Assuming you have a native compiler (a compiler that specifically targets the type of system your application will run on), sl4nted's answer is a good starting point.
    But wouldn't sizeof(void*) tell you the size of a void pointer for this particular compiler, which has nothing to do with the processor the code would run on?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by anon
    But wouldn't sizeof(void*) tell you the size of a void pointer for this particular compiler, which has nothing to do with the processor the code would run on?
    When you compile code - you do it for target processor/OS - so it is modified for the specified target.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    yes, with my compiler (gcc) the size of a pointer is the size of an address, which in turn is more than likley the size of a word. atleast I thought till now

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, but if you put your current gcc on a 64 bit processor, then you're in the same boat as Turbo C.
    There isn't a C function to tell you if your compiler is incompatible with your architecture.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    On a PURE 64 bit machine (ie, the OS is also 64bit) would not that code still work?

    If you are on a 32 bit platform, then the size of the register to you is 32 bit, not 64, regardless of what plugins you have for the processor driver (ie, the AMD 64-bit drivers) -- unless you're using the libs from the processor mfg that allows you access to the extended registers.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    On a PURE 64 bit machine (ie, the OS is also 64bit) would not that code still work?
    Depends on compiler you use, Not?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    OK; Salem's point is that, in C, the best you can hope for, without knowledge of your target machine, is information about the word size supported by your compiler. Since it is possible for a compiler to support one word size, and the programs it produces to run on an machine with different word size, it is not always possible to extract the word size for your machine. To extract the word size for your machine, you need to use machine or OS-specific methods (which implies you must have some idea about what type of machine your application will run on). If you have a compiler that supports the same word size as the target machine, then knowing word size supported by your compiler is sufficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. word processor
    By christian in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2002, 07:45 PM