A project for Data Structures by Nicholas Brimmer during the Spring 2013 semester.
This project was began on Monday, September 2 and is anticipated to take a few days of time. The program took a weekend to write but could have been written in a few hours if I had the time.
The purpose of this project is to learn about the different kinds of data types and there sizes (in bytes). Accomplishing this project should reinforce knowledge of C/C++ data types and there ranges on different types of systems. Depending on the system being used; these values do change. This knowledge should help prevent “Overflows/Underflows” and “Off-by-one/Fence post” errors.
Note: There is no project page for this project.
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
The purpose of this project is to represent and display the sizes (in bytes) of the different data types of C/C++. Matt provided some example code:
/* Nick Brimmer Data Structures Mathew Haas Spring 2013 Explore C Data types, there sizes in bytes, and ranges. */ #include <stdio.h> #include <stdlib.h> int main() { // Program Variables. /* Quantity variable: Stores the maximum unique values the data type can hold. The maximum number of unique values is 1 less than 0; 0 - 1 causes quantity to "roll-over" to its maximum value. */ unsigned long long int quantity = 0; unsigned char uc = 0; signed char sc = 0; // Aesthetics, heading for the output table with a divider. printf("\nData Type Sizes (in bytes) and Ranges:\n"); printf("----------------------------------------\n"); // unsigned char: printf("An unsigned character is: %d bytes.\n", sizeof(uc)); quantity = (unsigned char) (uc - 1); printf("The range of an unsigned character is %hhu to %hhu.\n", uc, (uc - 1)); printf("An unsigned character can store %llu unique values.\n\n", quantity); // Signed char: printf("A signed character is: %d bytes.\n", sizeof(sc)); quantity = (unsigned long long int)pow(2, (sizeof(sc) * 8)); printf("The range of a signed character is %hhd to %hhd.\n", (sc - (quantity / 2)), (sc + (quantity / 2) - 1)); printf("A signed character can store %llu unique values.\n\n", quantity); printf("\n"); return(0); }
Upon approval, you'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.
Providing any links to original source material, such as from a project page, is a good idea.
You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)
This project will display all C/C++ data types as well as there ranges. This can be useful in preventing some errors in future C/C++ programs. The program will display the size (in bytes) and the lower and upper bounds of each data type. The data types that will be displayed include:
The C functions sizeof() and printf() will be used, as well as arithmetic operators. A potential improvement would be to insert “,”'s into the data type sizes to better represent the numbers.
State and justify the attributes you'd like to receive upon successful approval and completion of this project.
The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.
The program was pretty easy to implement using Matt's code as a reference. Looking at Matt's example code; to determine the size of a data type the function “sizeof()” can be used. This function also supports arithmetic operators.
// C Code.
lab46:~/$
Since some of the numbers were so large, I had them “converted” to their equivalent number name:
The first thing I learned was about signed and unsigned numbers. The max value for an unsigned number is 0 - 1. This causes the lowest value (0) to “overflow” like an odometer to its maximum value. This is called “Integer overflow”. If it was a signed number, the number would go from positive to a negative number. Another important thing I learned was the size of the data types is dependent on the system that the code is being ran on. This can cause the data type ranges to be different from system to system. For example; I found that on lab46, the “long int” and “long long int” data types have the same size (8 bytes). This could be a system property of lab46. I also found that you cannot use the “signed” or “unsigned” modifiers on the “float” or “double” data types.
The first problem I encountered was a variable we declared in class. I had declared the “unsigned long long int ulli = 0;” variable incorrectly and mistyped my quantity variable. The “long float” variable is also broken. Another problem was my use of parenthesis and getting the order of operations correctly implemented.
One Possible improvement is to enter “,”'s into the output to make the numbers look all pretty:
In performing this project, the following resources were referenced: