User Tools

Site Tools


user:asowers:data_type_exploration

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
user:asowers:data_type_exploration [2012/09/03 23:57] – [Code] asowersuser: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/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=====
 +
 +=====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=====
 +
 +<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 <stdio.h>
 +#include <math.h>
 + 
 +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("An unsigned char is %d bytes\n", sizeof(uc));
 +    printf("The range of an unsigned char is %hhu to %hhu\n", uc, (uc-1));
 +    quantity = (unsigned char)(uc-1) + 1;    // What does this line do?
 +    printf("An unsigned char can store %llu unique values\n\n", quantity);
 + 
 +    // Display information for signed char data type
 +    printf("A signed char is %d bytes\n", sizeof(sc));
 +    quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); // What is happening?
 +    printf("The range of a signed char is %hhd to %hhd\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
 +    printf("A signed char can store %llu unique values\n\n", quantity);
 + 
 +    return(0);
 +}
 +</code>
 +Output:
 +<cli>
 +lab46:~/bin$ gcc range.c -o range -lm
 +lab46:~/bin$ ./range
 +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:~/bin$ 
 +</cli>
 +
 +For short int:
 +<code>
 +#include <stdio.h>
 +#include <math.h>
 +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("An unsigned shot int is %d bytes\n", sizeof(usi));
 +    printf("The range of an unsigned short int is %hu to %hu\n", usi, (usi-1));
 +    quantity = (unsigned short int)(usi-1) + 1;    // What does this line do?
 +    printf("An unsigned short int can store %llu unique values\n\n", quantity);
 +    // Display information for signed data type
 +    printf("A signed short int is %d bytes\n", sizeof(ssi));
 +    quantity = (unsigned long long int)pow(2, (sizeof(ssi)*8)); // What is happening?
 +    printf("The range of a signed short int is %hd to %hd\n", (ssi-(quantity/2)), (ssi+(quantity/2)-1));
 +    printf("A signed short int can store %llu unique values\n\n", quantity);
 +    return(0);
 +}
 +</code>
 +Output:
 +<cli>
 +lab46:~/bin$ gcc range.c -o range1 -lm
 +lab46:~/bin$ ./range1
 +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:~/bin$ 
 +</cli>
 +
 +For int:
 +<code>
 +#include <stdio.h>
 +#include <math.h>
 +int main()
 +{
 +    // Variables
 +    unsigned long long int quantity = 0;
 +    unsigned int ui = 0;
 +    signed int si = 0;
 +    // Display information for unsigned data type
 +    printf("An unsigned int is %d bytes\n", sizeof(ui));
 +    printf("The range of an unsigned int is %u to %u\n", ui, (ui-1));
 +    quantity = (unsigned long long int)(ui-1) + 1;    // What does this line do?
 +    printf("An unsigned int can store %llu unique values\n\n", quantity);
 +    // Display information for signed data type
 +    printf("A signed int is %d bytes\n", sizeof(si));
 +    quantity = (unsigned long long int)pow(2, (sizeof(si)*8)); // What is happening?
 +    printf("The range of a signed int is %i to %i\n", (si-(quantity/2)), (si+(quantity/2)-1));
 +    printf("A signed int can store %llu unique values\n\n", quantity);
 +    return(0);
 +}
 +
 +</code>
 +Output:
 +<cli>
 +lab46:~/bin$ gcc range.c -o range2 -lm
 +lab46:~/bin$ ./range2
 +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:~/bin$ 
 +</cli>
 +
 +For long int:
 +<code>
 +#include <stdio.h>
 +#include <math.h>
 +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("An unsigned long int is %d bytes\n", sizeof(uli));
 +    printf("The range of an unsigned long int is %lu to %lu\n", uli, (uli-1));
 +    quantity = (unsigned long long int)(uli-1) + 1;    // What does this line do?
 +    printf("An unsigned long int can store %llu unique values\n\n", quantity);
 +    // Display information for signed data type
 +    printf("A signed long int is %d bytes\n", sizeof(sli));
 +    quantity = (unsigned long long int)pow(2, (sizeof(sli)*8)); // What is happening?
 +    printf("The range of a signed long int is %ld to %ld\n", (sli-(quantity/2)), (sli+(quantity/2)-1));
 +    printf("A signed long int can store %llu unique values\n\n", quantity);
 +    return(0);
 +}
 +</code>
 +Output:
 +<cli>
 +lab46:~/bin$ gcc range.c -o range3 -lm
 +lab46:~/bin$ ./range3
 +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:~/bin$ 
 +</cli>
 +for long long int:
 +<code>
 +#include <stdio.h>
 +#include <math.h>
 +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("An unsigned long long int is %d bytes\n", sizeof(ulli));
 +    printf("The range of an unsigned long long int is %llu to %llu\n", ulli, (ulli-1));
 +    quantity = (unsigned long long int)(ulli-1) + 1;    // What does this line do?
 +    printf("An unsigned long long int can store %llu unique values\n\n", quantity);
 +    // Display information for signed data type
 +    printf("A signed long long int is %d bytes\n", sizeof(slli));
 +    quantity = (unsigned long long int)pow(2, (sizeof(slli)*8)); // What is happening?
 +    printf("The range of a signed long long int is %lld to %lld\n", (slli-(quantity/2)), (slli+(quantity/2)-1));
 +    printf("A signed long long int can store %llu unique values\n\n", quantity);
 +    return(0);
 +}
 +</code>
 +Output:
 +<cli>
 +lab46:~/bin$ gcc range.c -o range4 -lm
 +lab46:~/bin$ ./range4
 +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:~/bin$ 
 +</cli>
 +=====Reflection=====
 +This line:
 +<code>
 + quantity = (unsigned char)(uc-1) + 1;    // What does this line do?
 +</code>
 +this line is mathematical operation that establishes the value of the unsigned char and sets it equal to "quantity" to be manipulated later in the program.
 +an unsigned char has 8 bits allocated and its range is 0 to 255.
 +This line:
 +<code>
 +quantity = (unsigned long long int)pow(2, (sizeof(slli)*8)); // What is happening?
 +</code>
 +Pow is a mathematical "Power-Function" tool that can manipulate exponents.
 +
 +2,(variable)*8 set's up for the next line:
 +<code>
 +printf("The range of a signed long long int is %lld to %lld\n", (slli-(quantity/2)), (slli+(quantity/2)-1));
 +</code>
 +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://msdn.microsoft.com/en-us/library/s3f49ktz%28v=vs.80%29.aspx
 +http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.2.html
 +http://www.codecogs.com/reference/computing/c/math.h/pow.php
 +http://www.skillsheaven.com/clang.php