How to Determine the Value Range of the Data Types in C

10 October 2020, 2:27pm – 14 October 2020 2:30pm 

We, the mankind, use the base-10 numeral system which is called decimal number system. Decimal number system requires 10 different numerals – 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. So a decimal number is made up of these 10 digits like – 3422, 10987, 233292 etc.

It is noticeable that when we write digits using commas like this – 3, 5, 6, 2, .. .. etc., they are called digits. 3 is a single digit, 5 is a single digit. But when we write them without commas like 356, this is called number, a decimal number. So 356 is a decimal number which is made up of three digits – 3, 5 and 6.

But what about computers?

Computers are nothing but machines. They can’t even imagine 10 different digits/states :P . They’re not intelligent like human beings. These poor machines can detect only two operating states or possible conditions – whether the switch is OFF or ON. ‘OFF’ is denoted by 0 and ‘ON’ by 1. That means in the computer it’s all 1s and 0s.

The number system computers use is called Binary Number System. ‘Bi’ means two. As the name suggests, it requires only two digits – 0 and 1. These two digits have a special name ‘bit’ which is short for binary digit.

A ‘bit’ is a single binary digit – 0 or 1. 01000001 is a binary number which is 8 bit long. So a binary number is made up of only 1s and 0s.

When we want the computer to store a decimal number, for example 3, it stores it as a binary number 11. Likewise, for a character like ‘A’, it is stored as 01000001. Whatever we store, the computer stores it as a binary number.

The decimal number system is a base-10 numeral system as it uses 10 different symbols or digits.

 And the binary number system is a base-2 numeral system as it uses 2 different symbols or digits or bits.

Well, that’s no big deal for a programming learner. Just for the sake of continuity….

Look, guys, I don’t want any of you to memorize things having no clear concept. So for the sake of your better and simpler understanding I’m going to present some concepts in ways which may not meet the standard definitions. But don’t worry. Standard definitions are no big deal. The big deal is a better understanding.

Okay, so, let’s talk about signed and unsigned numbers.

I would like to keep it as simple as you can remember it easily.

Signed numbers are those which are represented using plus (+) or minus (-) sign, for example, -3, +7 etc. That means these numbers represent negative and positive numbers including zero. Zero is a non-negative number.

 As shown in the number line below.

And unsigned numbers are represented without sign such as 3, 7 etc. But remember no sign means there is a ‘+’ sign there. So it can represent non-negative numbers i.e., zero and positive numbers.


That’s all you need to remember.

Now, again binary numbers. Let’s make some three-bit-long binary numbers. How many unique numbers can be made with three bits together? Let’s see.

Binary ---- Decimal

000 ------------ 0

001 ------------ 1

010 ------------ 2

011 ------------ 3

100 ------------ 4

101 ------------- 5

110 ------------- 6

111 -------------- 7

So, we can make 8 unique numbers. But how can we figure out a common expression for this?

Binary numbers are based on 2, right? And we have 3 bits, no?

Don’t we write 23?

23 = 8

Remember this. It’s going to come in handy a bit later.

Let’s just imagine that we have a variable called ‘num’. We want it to store some numbers for us. But it can only store 3-bit-long binary numbers. Then how many unique numbers can we store into this variable? Well, you definitely know the answer J . The answer is 8. That’s great!

Now the question is- which numbers can we store? Most provably this also is known to you. We can store 0, 1, 2, 3, 4, 5, 6 or 7. Of course we store decimal numbers, not binary. Computers will convert decimal numbers into binary.

num = 0

num =1

num = 2

num = 3

..

..

num = 6

num = 7

That shows we can store numbers in the range 0 to 7. This is called value range. The value range for variable ‘num’ is - 0 to 7.


Let’s try to figure out a common expression for this value range.

It starts from 0. 0 is 0.

What about 7?

23  = 8

23 – 1 = 7

So the range can be written in this form –

0 to 23 – 1

Or, 0 to 7

Can you notice that the number of unique numbers is 8. But the last range is 7, that is, 1 less than 8. That’s because the numbers start from 0. See the explanation above mentioned.

The numbers include 0 and positive numbers, that is, non-negative numbers. So we can marked the variable ‘num’ as an unsigned variable. And can write like this –

unsigned num = 0

unsigned num = 1

unsigned num = 2 etc.

What if we want to store negative numbers, zero and positive numbers into that variable ‘num’?

Well, we can do that too. In that case we have to share 8 numbers equally between negative and non-negative numbers. Like this -

4 negative (half of total numbers) + 4 non-negative (half of total numbers)

= 4 negative + 1 zero + 3 positive

= 8 numbers

See the number line:


So….

The negative numbers:

num = - 4

num = - 3

num = - 2

num = - 1

and the non-negative numbers

num = 0

num =1

num = 2

num = 3

Now the range is different:

- 4 to 3

= - 22  to 22 - 1

Our variable is 3 bit long.

3 -1 = 2.

And base is 2.

 So, 22 = 4.

And of course, this is a signed variable as it contains negative, zero and positive numbers. Now we can write-

signed num = - 4

signed num = - 3

..

signed num = 3

I hope now your concept of decimal and binary numbers, signed and unsigned numbers, value range etc. is very much clear. Let’s get into our actual and much-awaited topic, shall we?

In C language, there are four primary data types.

1.       Integer (int)

2.       Floating-Point (float)

3.       Character (char)

4.       Void

 

1.       Integer (int)

An integer type variable can contain a 32-bit-long binary number.

232  = 4294967296

That means 32 bits together can make 4294967296 unique numbers. (Read again the above explanation)

That’s why an integer type variable can store 4294967296 unique numbers.

For unsigned integers, the value range is-

0 to 232 – 1

= 0 to 4294967295 (1 less than 4294967296)

In C, unsigned integer type variables are written like this-

unsigned int num = 0

unsigned int num = 1

unsigned int num =2

..

..

unsigned int num = 4294967295

Notice that 4294967295 is a 10-digit-long decimal number. That means in an integer type variable we can store maximum 10-digit-long decimal number. If any programming problem asks you to store maximum 100-digit-long decimal number, can you use integer type variables? Of course, not. This is important. So don’t forget.

Now what value range will be for a signed integer type variable?

We’ve already known integer type variables can store 4294967296 (232) unique numbers and singed numbers include negative and non-negative numbers.  

(Read again the respective explanation)

Negative (half of 4294967296) + non-negative(half of 4294967296)

= Negative (half of 4294967296) + Zero (1) +  positive(half of 4294967296 - 1)

= 4294967296 total

So the value range -

-231 to 231 – 1 (32bits – 1bit = 31bits)

= -2147483648 to 2147483647 (use calculator)

Singed integer type variable can be written like this-

int num = -2147483648

int num = -2147483647

int num = -2147483646

int num = -2147483645

--

--

int num = -2

int num = -1

int num = 0

int num = 1

int num = 2

--

--

int num = 2147483647

We need not use ‘singned’ keyword, as this is default.

Now, I believe,  you don’t have to memorize the value range and you definitely know why they are so.

Homework:

1.       Character (char) type data – data size 8 bits

2.       Long Long Integer type data – data size 64 bits

3.       Floating Point type data – data size 32 bits

4.       Double Float type data – data size 64 bits

5.       Long Double Float type data – data size 96 bits

Home Work-1. Determine the value range for each of the data types.

Home Work-2.  Determine the maximum number of decimal digits we can store into each of the data types.

Comments

Popular Posts