User Tools

Site Tools


user:brobbin4:portfolio:cprogproject0

Project: DATA TYPE EXPLORATION

A project for CSCS1320 C/C++ Programming by Brian Robbins during the spring 2012 semester.

This project was begun on February 7, 2012 and is anticipated to take 3 days to complete. Project was completed on February 10, 2012.

Objectives

The purpose of this project is to create a program that will calculate and output the byte size, range, and total ammount of unique values for each data type present within the C programming language. The point of this project is to learn about the datatype. Through the progression of this project I will learn about different datatypes including how datatypes are used, the capacities of different datatypes, and the upper and lower ranges of different datatypes. By undertaking this project I hope to achieve the knowledge of different datatypes, how they are supposed to be used within the C programming language and when each different datatype should be utilized.

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 idea of this project is to create a program that outputs information about each data type within the C programming lnaguage. Through the creation of this program we will learn about the different datatypes that are built into the C programming language. The datatypes that we will be learning about are the unsigned char, signed char, unsigned short int, signed short int, unsigned int, signed int, unsigned long int, signed long int, unsigned long long int, and signed long long int. Each of these datatypes has a different use and each can store a different amount of values. We will also learn about using math within a program to calculate the values for each datatype along with the proper arguments that are required to make the everything work.

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 <stdint.h>
#include <math.h>
 
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;
    unsigned long long int ulli = 0;
    signed long long int slli = 0;
 
    unsigned long long int quantity = 0;
 
    printf("\n");
 
    // 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; // This line take the value of an unsigned char and multiplies it by the value of an unsigned char minus one, and then add one to it.
    printf("An unsigned char can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for signed char data type
    printf("A signed char is %d bytes\n", sizeof(sc));
    printf("The range of a signed char is %hhd to %hhd\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
    quantity = (unsigned long long int)pow(2,(sizeof(sc)*8)); // This line calculates how many unique values a signed char can store by multiplying the size of a signed char (which is 1 byte) by 8 and then raising that result by a power of 2 
    printf("A signed char can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for unsigned short int
    printf("An unsigned short int is %d bytes\n", sizeof(unsigned short int));
    printf("The range of an unsigned short int is %hu to %hu\n", usi, (usi-1));
    quantity = pow(2,(sizeof(usi)*8));
    printf("An unsigned short int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for signed short int
    printf("A signed short int is %d bytes\n", sizeof(signed short int));
    printf("The range of an signed short int is %hd to %hd\n", (ssi-(quantity/2)), (ssi+(quantity/2)-1));
    quantity = pow(2,(sizeof(ssi)*8));
    printf("A signed short int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for unsigned int
    printf("An unsigned int is %d bytes\n", sizeof(unsigned int));
    printf("The range of an unsigned int is %u to %u\n", ui, (ui-1));
    quantity = pow(2,(sizeof(ui)*8));
    printf("An unsigned int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for signed int
    printf("A signed int is %d bytes\n", sizeof(signed int));
    printf("The range of an signed int is %d to %d\n", (si-(quantity/2)), (si+(quantity/2)-1));
    quantity = pow(2,(sizeof(si)*8));
    printf("A signed short int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display informaion for unsigned long int
    printf("An unsigned long int is %d bytes\n", sizeof(unsigned long int));
    printf("The range of an unsigned long int is %lu to %lu\n", uli, (uli-1));
    quantity = (unsigned long long int)pow(2,(sizeof(uli)*8));
    printf("An unsigned int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for signed long int
    printf("A signed long int is %d bytes\n", sizeof(signed long int));
    printf("The range of an signed long int is %ld to %ld\n", (sli-(quantity/2)), (sli+(quantity/2)-1));
    quantity = (unsigned long long int)pow(2,(sizeof(sli)*8));
    printf("A signed long int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for unsigned long long int
    printf("An unsigned long long int is %d bytes\n", sizeof(unsigned long long int));
    printf("The range of an unsigned long long int is %llu to %llu\n", ulli, (ulli-1));
    quantity = (unsigned long long int)pow(2,(sizeof(ulli)*8));
    printf("An unsigned long long int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    // Display information for signed long long int
    printf("A signed long long int is %d bytes\n", sizeof(signed long long int));
    printf("The range of an signed long long int is %lld to %lld\n", (slli-(quantity/2)), (slli+(quantity/2)-1));
    quantity = (unsigned long long int)pow(2,(sizeof(slli)*8));
    printf("A signed long int can store %llu unique values\n", quantity);
 
    printf("\n");
 
    return(0);
}

Execution

lab46:~/src/cprog$ ./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 -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 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
An unsigned int can store 4294967296 unique values

A signed int is 4 bytes
The range of an signed int is -2147483648 to 2147483647
A signed short 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 int can store 18446744073709551615 unique values

A signed long int is 8 bytes
The range of an 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 an signed long long int is -9223372036854775807 to 9223372036854775806
A signed long int can store 18446744073709551615 unique values

lab46:~/src/cprog$

Reflection

This project turned out to be more complicated then I thought when all was said and done. I observed that due to constraints in the C programming language that when the quantity was processed for both the signed and unsigned long and long long ints the ammounts were off by one value. I also observed when you start getting into larger datatypes there storage values appear to increase exponentially. After analysing this project I now see why there are several different types of datatypes and why one may be used over another in different applications. From completing this project I learned about the different types of datatypes that are used in the C language and when a certain datatype should be used.

References

In performing this project, the following resources were referenced:

  • C Pocket Reference from O'REILLY
  • The C Programming Language SECOND EDITION from Brian W. Kernighan and Dennis M. Ritchie
  • The all mighty wedge
  • The lab46 IRC Computer Sciences Channel
user/brobbin4/portfolio/cprogproject0.txt · Last modified: 2012/02/11 05:12 by brobbin4