Why do data sizes change as the operating system changes?

This question was asked to me in an interview that the size charis 2 bytes in some operating system, but in some operating system it is 4 bytes or different.

Why is this so?

Why is it different from other fundamental types, such as int?

+5
source share
2 answers

It was probably a trick. sizeof(char) always .

If the size is different, probably due to an inappropriate compiler, in which case the question should be about the compiler, and not about the C or C ++ language.

5.3.3 Size [expr.sizeof]

1 sizeof . , , . , , l, -. sizeof(char), sizeof(signed char) sizeof(unsigned char) 1. sizeof, (3.9.1), ( )

, , , . int , 64 32, 32 32- .

+9

( ) .

, 16 . char 16- . 8- CPU, 8- 16- . / .

. 16- 8- , /. 16- CPU, , 17- ? :

  • (32-, 15 ), .
  • , .

. , , , - , . 8- 16- .

C , CPU. char, short, int, long, long long, void*, void(*)(), float, double, long double, wchar_t, .. .

, , CPU, . , , , . , Windows int= long= 32 . , Windows API, int= long= 32 . int long, - - . , .

, , C ( ++) chars bytes. - . C 8- , , . , octet, 8. .

+5

All Articles