Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modern operating system / compiler where int (in C) is not 32 bits?

The question is actually somewhat broader.

Based on the last 15 years experience I currently assume that size of types is as follows:

byte = 8 bit

short = 2 bytes

int = 4 bytes

long long = 8 bytes

Is there a modern OS where this assumption can be challenged?

like image 446
Vladislav Vaintroub Avatar asked Dec 03 '11 00:12

Vladislav Vaintroub


People also ask

Is int always 32 bits in C?

int is always 32 bits wide. sizeof(T) represents the number of 8-bit bytes (octets) needed to store a variable of type T .

Can a'int be of 32 bits?

In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes. Thus, the int type is equivalent to either the short int or the long int type, and the unsigned int type is equivalent to either the unsigned short or the unsigned long type, depending on the target environment.

Is an int 32 or 64 bit?

int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size.

What C code can check whether the OS is 32 bit or 64 bit?

You can try sizeof(char*) , in general the pointer-to-char has the machine-size. If it's 8, then your machine is 64 bit, if 4, then 32.


1 Answers

Whether or not such "modern" systems exist, you should static_assert assert those assumptions in your code so that if your code is ever ported to a platform where the assumption is incorrect, the person doing the porting will be alerted to the problem immediately.

like image 98
Raymond Chen Avatar answered Sep 28 '22 18:09

Raymond Chen