User Tools

Site Tools


user:cforman:portfolio:cprogproject0

Project: DATA TYPE EXPLORATION

A project for C/C++ by Corey Forman during the Spring 2012.

This project was begun on 2/1/12 and took till 2/11/12 to complete.

Objectives

The purpose of this project is to test our ability to understand code and datatypes. Each datatype has different uses. Char is made to hold letters so its just large enough to function with the ASCII charts. While long long int is able to deal with … you guessed it …. large numbers. This Project is designed to allow us to explore these types of data and see their ranges so that we may better understand them.

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

The background behind choosing this project is as follows. Matthew Haas chose it for us so we can learn more about ranges of data types.

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 <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);
 
 
        //variables for short int
        unsigned short int usi = 0;
        signed short int ssi = 0;
 
        //Display information for unsigned short int data type
        printf("an unsigned short int is %hd bytes\n", sizeof(usi));
        printf("The range of an unsigned short int is %hu to %hu\n", uc, (usi-1));
        quantity = (unsigned short int)(usi -1) + 1; //multiplies unsigned short int with usi -1 and adds one to get maximum number of combinations with the system.
        printf("An unsigned short int can store %lu unique values\n\n", quantity);
 
        //Display information for signed short int data type
        printf("A signed short int is %hd bytes\n", sizeof(ssi));
        quantity = (unsigned long long int)pow(2, (sizeof(ssi)*8));
        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 %lu unique values\n\n", quantity);
 
 //variables for int
        unsigned int ui = 0;
        signed int si = 0;
 
        //Display information for unsigned int 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 int)(ui -1); //multiplies unsigned int with ui -1 and adds one to get maximum number of combinations with the system.
        printf("An unsigned int can store %lu unique values\n\n", quantity);
 
        //Display information for signed int data type
        printf("A signed int is %d bytes\n", sizeof(si));
        quantity = (unsigned long long int)pow(2, (sizeof(si)*8));
        printf("The range of a signed int is %d to %d\n", (si-(quantity/2)), (si+(quantity/2)-1));
        printf("A signed int can store %lu unique values\n\n", quantity);
 
 
        //variables for long int
        unsigned long int uli = 0;
        signed long int sli = 0;
 
        //Display information for unsigned long int data type
        printf("an unsigned long int is %ld bytes\n", sizeof(uli));
        printf("The range of an unsigned long int is %lu to %lu\n", uli, (uli-1));
        quantity = (unsigned long int)(uli -1); //multiplies unsigned int with uli -1 and adds one to get maximum number of combinations with the system.
        printf("An unsigned long int can store %lu unique values\n\n", quantity);
 
        //Display information for signed int data type
        printf("A signed long int is %ld bytes\n", sizeof(sli));
        quantity = (unsigned long long int)pow(2, (sizeof(sli)*8));
        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 %lu unique values\n\n", quantity);
 
 
        //variables for long long int
        unsigned long long int ulli = 0;
        signed long long int slli = 0;
 
        //Display information for unsigned long long int data type
        printf("an unsigned long long int is %lld 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); //multiplies unsigned int with uli -1 and adds one to get maximum number of combinations with the system.
        printf("An unsigned long long int can store %llu unique values\n\n", quantity);
 
        //Display information for signed long long int data type
        printf("A signed long long int is %lld bytes\n", sizeof(slli));
        quantity = (unsigned long long int)pow(2, (sizeof(slli)*8));
        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);
}

Execution

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 a 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 4294967295 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

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

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

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 a signed long long int is -9223372036854775807 to 9223372036854775806
A signed long long int can store 18446744073709551615 unique values

lab46:~/src/cprog$
I could not figure out for the life of me why long int and long long int were the same as i had changed all the values. 

Reflection

Knowing the datatypes is a very helpful thing. It allows the use of more efficient code. we are able to better manipulate data. This project was simple but left me with questions. Overall the project though was really fun especially when you had to search for the small typos one usually makes.

References

In performing this project, the following resources were referenced:

  • Class notes
user/cforman/portfolio/cprogproject0.txt · Last modified: 2012/02/11 14:07 by cforman