search

Home  >  Q&A  >  body text

c++ - 使用C编译器,如何设置使得int字长与系统字长相同

比如在处理问题:“如何不使用sizeof()用C程序获得系统字长?”

即便在64位机上,使用过的C编译器也会将int编译为32位。那么如何在C中设置,使得int编译字长和系统字长相同呢?

迷茫迷茫2815 days ago1013

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-17 11:54:17

    When compiled with GCC on Linux, the long of 32-bit machines is 4 bytes, and the long of 64-bit machines is 8 bytes.

    I searched for the definition of macro INT_MAX on VS. It seems that there is only one place, which is 2147483647, so there should be no way to set it. I can only define a type myself.

    And I learned that usually some projects do not use default types directly, but redefine a set of types for use. For example, I can define a set of types like this:

    1

    2

    3

    4

    5

    6

    7

    8

    <code>#ifdef m32

    typedef int SpacelanInt;

    typedef float SpacelanFloat;

    #else

    typedef long int SpacelanInt;

    typedef double SpacelanFloat;

    #endif

    </code>

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 11:54:17

    You cannot "set" the word length of int. This thing is not something that can be set in the first place.
    But in most C implementations, the word length of long is equal to the machine word length, provided that you generate a "native binary", such as generating a 64-bit program in a 64-bit system.
    In addition, the word length of pointer/intptr_t/uintptr_t is generally equal to the machine word length.

    Of course, this is not the case on microcontrollers such as 51 or in ancient 16-bit systems

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:54:17

    The data models used on different platforms (x86, x64, etc.) and different compilers are different. For details, please refer to:
    http://en.wikipedia.org/wiki/64-bit_computing# 64-bit_data_models

    In addition, if you want to get a data structure with the same machine word length, you can use size_t

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:54:17

    I can’t configure to generate 64-bit programs using VC 2010, but others have this option. But now that I think about it, this option is meaningless. Most people have 32-bit systems, even for the little bit of 64-bit. I guess the performance improvement is almost the same as 32-bit

    1

    2

    <code>__int64 j;

    </code>

    http://baike.baidu.com/item/int64

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 11:54:17

    Generally, the header files that come with the compiler include typedefs such as __int32, int32, int32_t, etc., which can ensure that it is 32-bit. However, if the code crosses the compiler, there will be problems, and you need to package it yourself.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:54:17

    1

    2

    <code>#include <stdint.h>

    </code>

    There is intptr_t in it

    reply
    0
  • Cancelreply