This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:brobbin4:portfolio:cprogproject0 [2012/02/11 03:21] – [Objectives] brobbin4 | user:brobbin4:portfolio:cprogproject0 [2012/02/11 05:12] (current) – [Code] brobbin4 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for CSCS1320 C/C++ Programming by Brian Robbins during the spring 2012 semester. | ||
+ | |||
+ | This project was begun on February 7, 2012 and is anticipated to take 3 days to complete. Project was completed on February 10, 2012. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The purpose of this project is to create a program that will calculate and output the byte size, range, and total ammount of unique values for each data type present within the C programming language. The point of this project is to learn about the datatype. Through the progression of this project I will learn about different datatypes including how datatypes are used, the capacities of different datatypes, and the upper and lower ranges of different datatypes. By undertaking this project I hope to achieve the knowledge of different datatypes, how they are supposed to be used within the C programming language and when each different datatype should be utilized. | ||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * ability to log into Lab46 | ||
+ | * ability to edit text files | ||
+ | * ability to compile C source code | ||
+ | * ability to read and appropriately react to compiler messages during compilation | ||
+ | * ability to execute compiled code | ||
+ | * knowledge of the size of a byte, how many combinations are possible therein | ||
+ | |||
+ | =====Background===== | ||
+ | The idea of this project is to create a program that outputs information about each data type within the C programming lnaguage. Through the creation of this program we will learn about the different datatypes that are built into the C programming language. The datatypes that we will be learning about are the unsigned char, signed char, unsigned short int, signed short int, unsigned int, signed int, unsigned long int, signed long int, unsigned long long int, and signed long long int. Each of these datatypes has a different use and each can store a different amount of values. We will also learn about using math within a program to calculate the values for each datatype along with the proper arguments that are required to make the everything work. | ||
+ | =====Scope===== | ||
+ | This project will be exploring the nature of some of the data types available to us in the C Programming Language. How much space is allocated to each type, how many numbers can exist within each type, and what are the ranges available for each type? | ||
+ | |||
+ | A program will be written that will display (to STDOUT) the size (in bytes), the lower and upper bounds of each studied type, and display the total quantity of values possible with each type. | ||
+ | |||
+ | The data types covered for this project will include: | ||
+ | |||
+ | * unsigned char | ||
+ | * signed char | ||
+ | * unsigned short int | ||
+ | * signed short int | ||
+ | * unsigned int | ||
+ | * signed int | ||
+ | * unsigned long int | ||
+ | * signed long int | ||
+ | * unsigned long long int | ||
+ | * signed long long int | ||
+ | |||
+ | The **sizeof()** and **printf()** functions, as well as arithmetic operators, will be utilized in performing much of the work. | ||
+ | |||
+ | |||
+ | =====Code===== | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * range.c - A program to display information for signed and unsigned data char types | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o range range.c -lm | ||
+ | * Execute with: ./range | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // Variables | ||
+ | unsigned char uc = 0; | ||
+ | signed char sc = 0; | ||
+ | unsigned short int usi = 0; | ||
+ | signed short int ssi = 0; | ||
+ | unsigned int ui = 0; | ||
+ | signed int si = 0; | ||
+ | unsigned long int uli = 0; | ||
+ | signed long int sli = 0; | ||
+ | unsigned long long int ulli = 0; | ||
+ | signed long long int slli = 0; | ||
+ | |||
+ | unsigned long long int quantity = 0; | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned char data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned char)(uc-1) +1; // This line take the value of an unsigned char and multiplies it by the value of an unsigned char minus one, and then add one to it. | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for signed char data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned short int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for signed short int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for signed int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display informaion for unsigned long int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for signed long int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned long long int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | // Display information for signed long long int | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, | ||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Execution===== | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | |||
+ | An unsigned char is 1 bytes | ||
+ | The range of an unsigned char is 0 to 255 | ||
+ | An unsigned char can store 256 unique values | ||
+ | |||
+ | A signed char is 1 bytes | ||
+ | The range of a signed char is -128 to 127 | ||
+ | A signed char can store 256 unique values | ||
+ | |||
+ | An unsigned short int is 2 bytes | ||
+ | The range of an unsigned short int is 0 to 65535 | ||
+ | An unsigned short int can store 65536 unique values | ||
+ | |||
+ | A signed short int is 2 bytes | ||
+ | The range of an signed short int is -32768 to 32767 | ||
+ | A signed short int can store 65536 unique values | ||
+ | |||
+ | An unsigned int is 4 bytes | ||
+ | The range of an unsigned int is 0 to 4294967295 | ||
+ | An unsigned int can store 4294967296 unique values | ||
+ | |||
+ | A signed int is 4 bytes | ||
+ | The range of an signed int is -2147483648 to 2147483647 | ||
+ | A signed short int can store 4294967296 unique values | ||
+ | |||
+ | An unsigned long int is 8 bytes | ||
+ | The range of an unsigned long int is 0 to 18446744073709551615 | ||
+ | An unsigned int can store 18446744073709551615 unique values | ||
+ | |||
+ | A signed long int is 8 bytes | ||
+ | The range of an signed long int is -9223372036854775807 to 9223372036854775806 | ||
+ | A signed long int can store 18446744073709551615 unique values | ||
+ | |||
+ | An unsigned long long int is 8 bytes | ||
+ | The range of an unsigned long long int is 0 to 18446744073709551615 | ||
+ | An unsigned long long int can store 18446744073709551615 unique values | ||
+ | |||
+ | A signed long long int is 8 bytes | ||
+ | The range of an signed long long int is -9223372036854775807 to 9223372036854775806 | ||
+ | A signed long int can store 18446744073709551615 unique values | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | This project turned out to be more complicated then I thought when all was said and done. I observed that due to constraints in the C programming language that when the quantity was processed for both the signed and unsigned long and long long ints the ammounts were off by one value. I also observed when you start getting into larger datatypes there storage values appear to increase exponentially. After analysing this project I now see why there are several different types of datatypes and why one may be used over another in different applications. From completing this project I learned about the different types of datatypes that are used in the C language and when a certain datatype should be used. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | * C Pocket Reference from O' | ||
+ | * The C Programming Language SECOND EDITION from Brian W. Kernighan and Dennis M. Ritchie | ||
+ | * The all mighty wedge | ||
+ | * The lab46 IRC Computer Sciences Channel |