The size of the data in C depends on the OS?

For example, it inttakes 4 bytes in a 32-bit OS, but 8 bytes in a 64-bit OS, does it do something like C?

+3
source share
3 answers

Yes. This is what is meant by β€œplatform-specific definitions” for things like the size intand meaning of system calls.

They depend not only on the OS, but also on the configuration of the target hardware and the compiler.

+6
source

The size of the datatype depends on the memory model implemented by the compiler. Think in terms of ILP (Int, Long, Pointer) and the number of bits used for the specified.

, , ILP64, Int, Long Pointer, 64 . LP64 Int 32 , Long Pointer - 64 .

, . , .

, , :

#include <stdio.h>                                                                                                                                                                 
#define BYTE_SIZE 8

void main(void)
{

  int sizeof_int = sizeof(int) * BYTE_SIZE;
  int sizeof_long = sizeof(long) * BYTE_SIZE;
  int sizeof_ptr = sizeof(&sizeof_int) * BYTE_SIZE;

  printf("Size of int: %d, long: %d, pointer: %d\n",
         sizeof_int, sizeof_long, sizeof_ptr );
}

64- AMD, 64- Debian Squeeze, , GCC LP64. 64- , 1995 UNIX. , , .

+2

, . - 16- int: s, , .

While we are in the subject. This not only affects the type int, but also affects the evaluation of expressions. The principle is that each expression is evaluated as if it were evaluated exactly intor more. For example, given the two 16-bit variables short aand band the longvariable x, the expression x = a*bwill give a different result depending on the size int, although none of the variables involved are of type int.

+2
source

All Articles