To determine and test the limits of common data types in C.
|
Corning Community College UNIX/Linux Fundamentals |
To determine and test the limits of common data types in C.
There are several data types available for you to manipulate in C:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Each data type can be a signed or unsigned quantity. The sign of a quantity is simply whether or not it includes negative values. For example, let's look at an unsigned 4-bit quantity:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
So, in a 4-bit quantity, you've got 16 distinct values. It is considered 4-bit because we are dealing with the binary values-- what the computer deals with. If we were dealing with an 8-bit quantity, there would be 8 bits for use in counting.
Actually, to determine the number of unique states, you'd use the following: 2n
Where n is the number of bits you are dealing with. In this case: 4-bits = 24 = 16
The values in the above table are all unsigned quantities, in that they are only the positive representation. To play with signed quantities, we use the leftmost bit (the Most Significant Bit (MSB)) as the sign-- where 0 is positive and 1 is negative.
In this case, we now have the following values:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NOTE: Negative values are obtained by subtracting one and then inverting all the bits:
6 = 0110
6 - 1 = 5 ---> 0110 - 0001 = 0101Then invert (turn 0's to 1's, and 1's to 0's): 0101 ---> 1010
This process is known as the two's complement.
As you can see, we still have all 16 distinct states, but they are now represented in positive and negative values. The range of 4-bit signed integers here is -8 to +7, where the range of 4-bit unsigned integers is 0 to 15.
Now, you may be wondering why this is relevant-- basically, the ranges of things like integers aren't a fixed absolute value from system to system.
This could result in some calculation problems if a program is written expecting integers to be 32-bit values and the host system treats integers as 16-bit values.
It is important to know what these values are on any system you program on. Guess what you get to determine and experiment with in this Case Study :)
Checking the values: limits.h
| | Locate the limits.h header file. |
| | Where is it? |
NOTE: There will be multiple limits.h files on the system.. use the one in the same directory as stdlib.h
| | How many bits are in a char? |
| | How many unique states can this value have? |
| | What is the range of the signed short integers? |
| | How many bits is an short int? |
| | What is the range of the unsigned integers? |
| | How many bits is an int? |
| | And finally, how many bits in a long long int? |
#include <stdio.h>
int main()
{
unsigned int a, x;
signed short int b, y;
a = 0; // zero not letter "O"
b = 0;
x = a - 1;
y = b - 1;
printf("unsigned int before: %u \t after: %u\n", a, x);
printf("signed short int before: %hd \t after: %hd\n", b, y);
return(0);
}
Testing the limits:
| | Using vi, create a file called cs6.c in your src/ directory and enter the above program. |
| | Don't forget to save!! |
| | If you get errors, please submit the exact message generated as well as your code to the class mailing list. |
| | Using your skills from Lab #6, compile the program. How did you do it? |
Now let's run it.
| | What was the value of the unsigned int afterwards? |
| | Have you seen this value anywhere else? Where? |
| | And what is the value of the signed short int afterwards? |
| | Did you expect this? Why did you get this value? |
Time for some modifications.
| | Change the line b = 0; to allow b to be equal to the maximum positive signed short int (which you found back in question 2) |
| | Change the line y = b - 1; to add instead of subtract |
| | Delete the first printf line and leave the second intact. |
Save, exit, and recompile cs6.c
| | What is the final result? |
| | Does the result make sense for the range of the data type? Why? |
Be sure to attach cs6.c to your e-mail when you submit it.
| | Create a small program to play with a long long int |
| | Show me the maximum value this data type can hold. |
| | Show me the minimum value. |
| | Why might this be a useful data type? |
Be sure to include this program as well, along with any observations you made when doing it.
All questions in this assignment require an action or response. Please organize your answers into an easily readable format and be prepared to submit the final results to your instructor.
Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.
When complete, electronically submit your assignment using the "Assignment Submitter", located here:
As always, the class mailing list is available for assistance, but not answers.