1

While I was being taught C, it was said that the size of int datatype is 2 bytes. But in CS 50, I am being taught that it is of 4 bytes. Why is that so?

4

The size of an int is dependent on the hardware and operating system environment that you are working with. It has historically had a lot to do with the type of processor in the system. Early computers had 8 bit, or 1 byte processors, which had integers as small as 1 byte - a real nuisance since that had a range of roughly +/- 127. Later, the 16 bit or 2-byte processor followed and ints became 2 byte or +/- 32k. As processors grew, so did the integer's size. Same logic applies to long ints, etc. So today, you have to check the particular environment you are in to see just how big an int really is to be absolutely sure.

In CS50, it's 4 bytes or 32 bits, which happens to be the environment - a 32 bit o/s. I suspect that if you programmed in a 64-bit environment, an int would be 8 bytes, but I have yet to look. ;-)

If this answers your question, please click on the check mark to accept the answer. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .