======Project: DATA TYPE EXPLORATION======
A project for CPROG by Josh Davis during the Spring 2012.
This project was begun on 2,4,2012 and is anticipated to take 2/10/2012 to complete. Project was completed on MONTH DAY, YEAR.
=====Objectives=====
Working program that prints variable types, ranges, and the size of their containers.
=====Prerequisites=====
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
* 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=====
State the idea or purpose of the project. What are you attempting to pursue?
General purpose is to get everyone playing with data types and learning more about them. This will help build a more effective foundation for using them later on. Could possibly JUST maybe help explain as to why certain answers may not return right when it appears it should be from the code syntax.
=====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=====
/*
* 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
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;
signed long long int slli = 0;
unsigned long long int quanity = 0;
// Display Information for unsigned char data type
printf("An unsigned char is %d bytes\n", sizeof(uc));
printf("The range of an unsigned char is %hhu to %hhu\n", uc, (uc-1));
quanity = (unsigned char)(uc-1) +1;
printf("An Unsigned char can store %llu unique values\n\n", quanity);
//Display information for a signed char data type
printf("A signed char is %d bytes\n", sizeof(sc));
quanity = (unsigned long long int)pow(2, (sizeof(sc)*8));
printf("The range of a signed char is %hhd to %hhd\n", (sc-(quanity/2)), (sc+(quanity/2)-1));
printf("A signed char can store %llu unique values\n\n", quanity);
//Display information for a unsigned short int data type
printf("an unsiged short int is %d bytes\n", sizeof(usi));
printf("the range of an unsigned int is %hu to %hu\n", usi, (usi-1));
quanity = (unsigned long long int)pow(2, (sizeof(usi)*8));
printf("A unsigned int can store %llu unique values\n\n", quanity);
//Display information for a signed short int data type
printf("an signed short int is %d bytes\n", sizeof(ssi));
quanity = (unsigned long long int)pow(2, (sizeof(ssi)*8));
printf("the range of an signed short int is %d to %d\n", (ssi-(quanity/2)), (ssi+(quanity/2)-1));
printf("A signed short int can store %llu unique values\n\n", quanity);
//Display information for a unsigned short int data type
printf("an unsiged short int is %d bytes\n", sizeof(usi));
printf("the range of an unsigned int is %hu to %hu\n", usi, (usi-1));
quanity = (unsigned long long int)pow(2, (sizeof(usi)*8));
printf("A unsigned int can store %llu unique values\n\n", quanity);
//Display information for a signed short int data type
printf("an signed short int is %d bytes\n", sizeof(ssi));
quanity = (unsigned long long int)pow(2, (sizeof(ssi)*8));
printf("the range of an signed short int is %d to %d\n", (ssi-(quanity/2)), (ssi+(quanity/2)-1));
printf("A signed short int can store %llu unique values\n\n", quanity);
//Display information for a unsigned int data type
printf("an unsigned int is %d bytes\n", sizeof(ui));
quanity = (unsigned long long int)pow(2, (sizeof(ui)*8));
printf("the range of an unsigned int is %u to %u\n", ui, (ui-1));
printf("A unsigned int can store %llu unique values\n\n", quanity);
//Display information for a signed int data type
printf("an signed int is %d bytes\n", sizeof(ui));
quanity = (unsigned long long int)pow(2, (sizeof(ui)*8));
printf("the range of an signed short int is %d to %d\n", (ui-(quanity/2)), (ui+(quanity/2)-1));
printf("A signed int can store %llu unique values\n\n", quanity);
//Display information for a unsigned long int data type
printf("an unsigned long int is %d bytes\n", sizeof(uli));
quanity = (unsigned long long int)pow(2, (sizeof(uli)*8));
printf("the range of an unsigned long int is %llu to %llu\n", uli, (uli-1));
printf("A unsigned long int can store %llu unique values\n\n", quanity);
//Display information for a signed long int data type
printf("an signed long int is %d bytes\n", sizeof(uli));
quanity = (unsigned long long int)pow(2, (sizeof(uli)*8));
printf("the range of an signed long int is %llu to %llu\n", (uli-(quanity/2)), (uli+(quanity/2)-1));
printf("A signed long int can store %llu unique values\n\n", quanity);
//Display information for a signed long int data type
printf("an signed long int is %d bytes\n", sizeof(uli));
quanity = (unsigned long long int)pow(2, (sizeof(uli)*8));
printf("the range of an signed long int is %llu to %llu\n", (uli-(quanity/2)), (uli+(quanity/2)-1));
printf("A signed long int can store %llu unique values\n\n", quanity);
//Display information for a signed long long int data type
printf("an signed long long int is %d bytes\n", sizeof(sli));
quanity = (unsigned long long int)pow(2, (sizeof(sli)*8));
printf("the range of an signed long long int is %lld to %llu\n", (sli-(quanity/2)), (sli+(quanity/2)-1));
printf("A signed long long int can store %llu unique values\n\n", quanity);
//Display information for a unsigned long long int data type
printf("an unsigned long long int is %d bytes\n", sizeof(sli));
quanity = (unsigned long long int)pow(2, (sizeof(sli)*8));
printf("the range of an unsigned long long int is %lld to %llu\n", sli, (sli-1));
printf("A unsigned long long int can store %llu unique values\n\n", quanity);
return(0);
}
=====Execution=====
lab46:~/src/cprog$ $ ./range
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 unsiged short int is 2 bytes
the range of an unsigned int is 0 to 65535
A unsigned int can store 65536 unique values
an 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
A unsigned int can store 4294967296 unique values
an signed int is 4 bytes
the range of an signed short int is -2147483648 to 2147483647
A signed int can store 4294967296 unique values
an unsigned long int is 8 bytes
the range of an unsigned long int is 0 to 18446744073709551615
A unsigned long int can store 18446744073709551615 unique values
an signed long int is 8 bytes
the range of an signed long int is 9223372036854775809 to 9223372036854775806
A signed long int can store 18446744073709551615 unique values
an signed long long int is 8 bytes
the range of an signed long long int is -9223372036854775807 to 9223372036854775806
A signed long 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
A unsigned long long int can store 18446744073709551615 unique values
lab46:~/src/cprog$
=====Reflection=====
Kinda long and tedious... none the less helped understand the variables a bit better than before.
=====References=====
In performing this project, the following resources were referenced:
* http://www.cplusplus.com/doc/tutorial/variables/
* Has a chart with a list of variables and ranges, helped a little with a check to make sure what mine showed, were actually correct.