This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:asowers:data_type_exploration [2012/09/03 23:57] – [Code] asowers | user:asowers:data_type_exploration [2012/09/04 01:29] (current) – [Scope] asowers | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Andrew Sowers | ||
+ | It'll be done when it's done. | ||
+ | |||
+ | =====Objectives===== | ||
+ | To explorificate datatypes and such. | ||
+ | |||
+ | =====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===== | ||
+ | |||
+ | =====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 signed and unsigned verifications of: | ||
+ | |||
+ | * char | ||
+ | * short int | ||
+ | * int | ||
+ | * long int | ||
+ | * long long int | ||
+ | |||
+ | The sizeof() and printf() functions, as well as arithmetic operators, will be utilized in performing much of the work. | ||
+ | |||
+ | =====Code===== | ||
+ | |||
+ | < | ||
+ | /* | ||
+ | * range.c - A program to display information for signed and unsigned data types | ||
+ | * | ||
+ | * | ||
+ | * Compile with: gcc -o range range.c -lm | ||
+ | * Execute with: ./range | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // Variables | ||
+ | unsigned long long int quantity = 0; | ||
+ | unsigned char uc = 0; | ||
+ | signed char sc = 0; | ||
+ | |||
+ | // Display information for unsigned char data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned char)(uc-1) + 1; // What does this line do? | ||
+ | printf(" | ||
+ | |||
+ | // Display information for signed char data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | Output: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | 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 0 to -128 | ||
+ | A signed char can store 256 unique values | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | For short int: | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | int main() | ||
+ | { | ||
+ | // Variables | ||
+ | unsigned long long int quantity = 0; | ||
+ | unsigned short int usi = 0; | ||
+ | signed short int ssi = 0; | ||
+ | // Display information for unsigned data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned short int)(usi-1) + 1; // What does this line do? | ||
+ | printf(" | ||
+ | // Display information for signed data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(ssi)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | Output: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | An unsigned shot 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 a signed short int is -32768 to 32767 | ||
+ | A signed short int can store 65536 unique values | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | For int: | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | int main() | ||
+ | { | ||
+ | // Variables | ||
+ | unsigned long long int quantity = 0; | ||
+ | unsigned int ui = 0; | ||
+ | signed int si = 0; | ||
+ | // Display information for unsigned data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)(ui-1) + 1; // What does this line do? | ||
+ | printf(" | ||
+ | // Display information for signed data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(si)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | Output: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | 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 a signed int is -2147483648 to 2147483647 | ||
+ | A signed int can store 4294967296 unique values | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | For long int: | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | int main() | ||
+ | { | ||
+ | // Variables | ||
+ | unsigned long long int quantity = 0; | ||
+ | unsigned long int uli = 0; | ||
+ | signed long int sli = 0; | ||
+ | // Display information for unsigned data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)(uli-1) + 1; // What does this line do? | ||
+ | printf(" | ||
+ | // Display information for signed data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(sli)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | Output: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | An unsigned long int is 8 bytes | ||
+ | The range of an unsigned long int is 0 to 18446744073709551615 | ||
+ | An unsigned long int can store 0 unique values | ||
+ | |||
+ | A signed long int is 8 bytes | ||
+ | The range of a signed long int is -9223372036854775807 to 9223372036854775806 | ||
+ | A signed long int can store 18446744073709551615 unique values | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | for long long int: | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | int main() | ||
+ | { | ||
+ | // Variables | ||
+ | unsigned long long int quantity = 0; | ||
+ | unsigned long long int ulli = 0; | ||
+ | signed long long int slli = 0; | ||
+ | // Display information for unsigned data type | ||
+ | printf(" | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)(ulli-1) + 1; // What does this line do? | ||
+ | printf(" | ||
+ | // Display information for signed data type | ||
+ | printf(" | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(slli)*8)); | ||
+ | printf(" | ||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | Output: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | 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 0 unique values | ||
+ | |||
+ | A signed long long int is 8 bytes | ||
+ | The range of a signed long long int is -9223372036854775807 to 9223372036854775806 | ||
+ | A signed long long int can store 18446744073709551615 unique values | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | =====Reflection===== | ||
+ | This line: | ||
+ | < | ||
+ | | ||
+ | </ | ||
+ | this line is mathematical operation that establishes the value of the unsigned char and sets it equal to " | ||
+ | an unsigned char has 8 bits allocated and its range is 0 to 255. | ||
+ | This line: | ||
+ | < | ||
+ | quantity = (unsigned long long int)pow(2, (sizeof(slli)*8)); | ||
+ | </ | ||
+ | Pow is a mathematical " | ||
+ | |||
+ | 2, | ||
+ | < | ||
+ | printf(" | ||
+ | </ | ||
+ | this line displays the variables datatype range. | ||
+ | |||
+ | =====After thoughts===== | ||
+ | This exercise sufficiently explained how different datatypes are manipulated. Hopefully a practical application will soon follow. | ||
+ | |||
+ | =====References===== | ||
+ | http:// | ||
+ | http:// | ||
+ | http:// | ||
+ | http:// |