Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the size of data types in C depend on the OS?

Tags:

c

types

For example, int takes 4 bytes on a 32-bit OS, but 8 bytes on a 64-bit OS, does this kind of thing exist in C?

like image 247
compiler Avatar asked Nov 16 '25 05:11

compiler


2 Answers

Yes. This is precisely what is meant by "platform-specific definitions" for things like the size of int and the meaning of system calls.

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

like image 179
Crashworks Avatar answered Nov 17 '25 18:11

Crashworks


It can depend on the OS, but more typically it depends on the hardware. Many micro controllers still use 16-bit int:s, since this is what the device handles naturally.

While we're in the subject. This does not only affect the int type, it also affects how expressions are evaluated. The principle is that every expression is evaluated as though it would have been evaluated in int precision, or larger. For example, given two 16-bit short variables a and b and a long variable x, the expression x = a*b will yield different result depending on the size of int, even though none of the involved variables have the type int.

like image 37
Lindydancer Avatar answered Nov 17 '25 19:11

Lindydancer