What is the size of int in DOS?

This is a common question that arises in so many minds who are learning to program in any high level language such as C. Thanks for asking. While programming, we need a way to make our program “remember” values. So, we use “variables”. For example, the statement, “int i = 10;” defines an int variable and assigns the value 10 to it. Further, this allows to manipulate the value in statements like “i = i + 20;” or “i = i * 2;”. Thus we are able to build on this idea and write more complicated programs. When we go for placements, or even for GATE kind of exams, the concept of space complexity is of utmost importance. Space complexity denotes the amount of memory our program will occupy. Based on this, programs are sometimes classified as good or bad. Sorting is an example. You can sort in-place i.e., without significant additional memory! The variables we declare and use are the primary reasons for memory usage. More variables you declare, your programs need more memory (RAM). Let us assume you have one program which uses 100 bit type variables and 100 strings. It is a no-brainer to claim that the latter program needs more memory than the former. Each data type, uses a defined amount of memory. Now that we understand the background of variables, data types and the space occupied by them, let us get to this question.

How much memory does int occupy on a DOS system? Let me assume for now that we are talking about C programming. DOS is a family of disk operating systems. There are multiple flavors of DOS namely MS DOS, FreeDOS, AppleDOS and so on. Refer to the wikipedia page of DOS for more detailed discussion on DOS. For this discussion, let us consider MS-DOS, the microsoft DOS that we know better. Interestingly, there are different flavors of DOS based on the underlying system architecture. Here comes the notion of “32-bit” machine. This means that the registers and addresses are 32-bits wide. Even FAT uses 32-bits. So, you cannot store one single file which is more than 4GB on your USB drive even if the drive is say 16 GB in size. Anyways, that was just a side-detail.

An Integer is a primitive data type. To learn about the memory occupied by integer, we refer to the C99 or any such standard. The standard only requires size relations between the data types and minimum sizes for each data type. C wants compiler designers to be only ensure that int is capable of containing at least the [−32,767, +32,767] range. Therefore, it is at least 16 bits in size. But, if we use 32 bit registers, we cannot allocate half register. So, nowadays, it is 32 bits. Earlier, when most C books were written, we had 16 bit processors. So, they stuck to the standard and provided 16 bits to int. Nowadays, it depends on the processor architecture. If you are on a 64-bit machine, you are even worse. You need 8 bytes or 64 bits! You can check this by yourself by using sizeof() in C.

So, the final answer is, assuming the version of MS DOS uses a 32-bit architecture, the sizeof() is very likely to give 4 as result (i.e., 4 bytes = 32 bits) for the size of int. Note the word, “likely”. It means that the compiler designer can come up with a different size. As long as it is more than 16 bits and adheres to the size ratios, they are allowed to do so. If no information such as DOS, or system architecture is given, we go with the C99 standard which requires an int to be at least 16 bits i.e., 2 bytes. Hope this answers your question.

One final bit to add here is that do not memorize these sizes. It is not required. Remember the ideas but not the exact number of bits occupied by each data type. That is not necessary and is often incorrect as I have explained above.


Author: Venkatesh Vinayakarao

Researcher, Computer Science.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s