开发者

confusion with size of variable in C [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Any guaranteed minimum sizes for types in C?

C/C++: Size of builtin types for various compilers/platforms

confusion with size of variable in C [duplicate]

i have a question开发者_开发技巧 regarding c language

in books it is written that size of int, float in c is one word ad two words respectively. these words are machine specific. for a 16 bit machine size of word is 16 bit and so size of int in c is 16 bit i.e. 2 byte.

some say that size of int in c is operating system specific. because in windows it gives size of int is 2 byte and in linux size of int is 4 byte

some say it is compiler specific because for tc size of int is 2 byte and for gcc it is 4 byte long

mine is a intel pentium dual processor(hope it is 32 bit) and 32 bit os (shown in system properties) and i am using tc when i make show size of int in c program it shows 2 bytes. but if it is machine or os dependent it should show 4 byte long

i am completely confused. please help me solving my prob

(attached: my system's properties shown by computer)


The size of int is definitely platform specific - usually it is 16 bits on 16-bit platforms, 32 bits on 32-bit platforms etc. However, it is also compiler specific: a 16-bit compiler will create 16-bit ints even on a 32-bit platform. Such code may then be run in a special backward-compatibility mode by a 32-bit OS.


I think I understand what you are asking. In short the size of an int is defined by the compiler you are using.

Traditionally, for a given Unix platform, all compilers conform to the same "ABI". That means an int is an int, and there is no distinction between "operating system specific" and "compiler specific". If you call open(), for example, your compiler literally calls the kernel open function, and the return value is an int returned from the kernel.

Theoretically though, the C library is divorced from the OS. Your compiler may have an int size that is different to the native word size of the machine. For example, if you run on WIN32 a native file handle is 32-bits but your compiler may have 16-bit or 64-bit ints. In this scenario you can think of your compiler's c library as a layer that isolates you from the operating system word size.


The most correct term would be "implementation specific" which means that compiler X for operating system Y will have its own idea of what sizes primitive types will have.

So you can have 2-bytes int on a 32-bit system. All those long passages about "words" in books are to ghelp you get familiar with how computers work, they are not requirements to how a C compiler must be implemented.


There's stdint.h header file which have fixed size variables, e.g. int64_t, uint_16t, enc


There is a minimum range of values for each type that's defined by the language standard (draft n1256, § 5.2.4.2), which determine the minimum size in bits. char must be at least 8 bits wide, int must be at least 16 bits wide, float must be at least 32 bits wide, etc. A compiler may use wider types than required, but not narrower.

To see the type sizes for your platform, check the standard headers limits.h and float.h.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜