This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:smalik2:portfolio:cprogproject0 [2012/02/11 01:15] – [Project: DATA TYPE EXPLORATION] smalik2 | user:smalik2:portfolio:cprogproject0 [2012/02/11 01:17] (current) – [Project: DATA TYPE EXPLORATION] smalik2 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for C/C++ Programming by Saad Malik during Spring 2012. | ||
+ | |||
+ | This project was begun on 2/9/12 and is anticipated to take a day to complete. Project was completed on February 10, 2012. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The point is to write a code that will display the bytes and range associated with signed and unsigned variable types. | ||
+ | =====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===== | ||
+ | |||
+ | |||
+ | Write a code that will display the amount of bytes and the range and amount of values that different variable types can hold. | ||
+ | |||
+ | Basically, take the example code provided and apply it to eight other variable types inside one program. | ||
+ | =====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> | ||
+ | |||
+ | /* | ||
+ | * Project0.c - A program to display information for signed and unsigned data char types | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o Project0 Project0.c -lm | ||
+ | * Execute with: ./Project0 | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // UNSIGNED VARIABLES | ||
+ | unsigned long long int quantity = 0; | ||
+ | unsigned long long int ullong = 0; | ||
+ | unsigned long int ulong = 0; | ||
+ | unsigned int uint = 0; | ||
+ | unsigned short int ushort = 0; | ||
+ | unsigned char uc = 0; | ||
+ | |||
+ | // SIGNED VARIABLES | ||
+ | signed long long int sllong = 0; | ||
+ | signed long int slong = 0; | ||
+ | signed int sint = 0; | ||
+ | signed short int sshort = 0; | ||
+ | signed char sc = 0; | ||
+ | |||
+ | |||
+ | // Display information for unsigned char data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned char)(uc-1); | ||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned short int data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned short)(ushort-1); | ||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned int data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned int)(uint-1); | ||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned long int data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long int)(ulong-1); | ||
+ | printf(" | ||
+ | |||
+ | // Display information for unsigned long long int data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)(ullong-1); | ||
+ | printf(" | ||
+ | |||
+ | |||
+ | // | ||
+ | |||
+ | // Display information for signed char data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | // Display information for signed short int data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(sshort)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | // Display information for signed int data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(sint)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | // Display information for signed long int data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(slong)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | // Display information for signed long long int data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(sllong)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | |||
+ | ^G Get Help ^O WriteOut | ||
+ | ^X Exit ^J Justify | ||
+ | |||
+ | </ | ||
+ | |||
+ | =====Execution===== | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | An unsigned char is 1 bytes | ||
+ | The range of an unsigned char is 0 to 255 | ||
+ | An unsigned char can store 255 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 65535 unique values | ||
+ | |||
+ | An unsigned int is 4 bytes | ||
+ | The range of an unsigned int is 0 to 4294967295 | ||
+ | An unsigned int can store 4294967295 unique values | ||
+ | |||
+ | An unsigned long int is 8 bytes | ||
+ | The range of an unsigned long int is 0 to 18446744073709551615 | ||
+ | An unsigned 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 char is 1 bytes | ||
+ | The range of a signed char is -128 to 127 | ||
+ | A signed char can store 256 unique values | ||
+ | |||
+ | A signed short int is 2 bytes | ||
+ | The range of a signed short int is -32768 to 32767 | ||
+ | A signed short int can store 65536 unique values | ||
+ | |||
+ | A signed int is 4 bytes | ||
+ | The range of a signed int is -2147483648 to 2147483647 | ||
+ | A signed int can store 4294967296 unique values | ||
+ | |||
+ | A signed long int is 8 bytes | ||
+ | The range of a signed long int is -9223372036854775807 to 9223372036854775806 | ||
+ | A signed char can store 18446744073709551615 unique values | ||
+ | |||
+ | A signed long long int is 8 bytes | ||
+ | The range of a signed long long int is -9223372036854775807 to 9223372036854775806 | ||
+ | A signed char can store 18446744073709551615 unique values | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | I had a lightbulb moment when I figured out how the code was producing the range of values for the given variable type, which I appreciated. | ||
+ | |||
+ | Also, now that I have written code to show it, I think I will more easily be able to remember the values that can be held by different types. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | None. |