User Tools

Site Tools


user:drobie2:portfolio:datatypes

Project: DATA TYPE EXPLORATION

A project for CPROG C/C++ by Dalton Robie during the Fall 2011 Semester.

This project was begun on 9/21/11 and is anticipated to take 1 week to complete. Project was completed on September 27th, 2011.

Objectives

The purpose of this project is to be able to explore variables used in C.

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 purpose of this project is to explore data types and learn their values and ranges.

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 - The Range is 0 to 255 for a total ammount of values of 256, and the size of an unsigned char is 1 byte(s).
  • signed char - The Range is -128 to 127 for a total ammount of values of 256, and the size of a signed char is 1 byte(s).
  • 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.

Attributes

  • variables: A variable for each of the stated data types will be declared and manipulated.
  • I/O: To ascertain the properties of the data types, important values will be displayed to STDOUT.

Code

/*--- range.c begin ---
 * 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;
    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;

    // 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;
    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));
    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);

  // Display information for unsigned short int data type
    printf("An unsigned short int is %d bytes\n", sizeof(usi));
    printf("An unsigned short int can hold 65,535 unquie values");
// Display information for signed short int data type
    printf("An signed short int is %d bytes\n", sizeof(ssi));
    printf("A signed short int has a range of unique values from  -32,768 to +32,767");
// Display information for unsigned int data type
    printf("An unsigned int is %d bytes\n", sizeof(ui));
    printf("An unsigned int has a range of unique values from 0 to +4,294,967,295.");
// Display information for signed int data type
    printf("An signed int is %d bytes\n", sizeof(si));
    printf("A signed int has a range of unique values from -2,147,483,648 to +2,147,483,647.");
// Display information for unsigned long int data type
    printf("An unsigned long int is %d bytes\n", sizeof(uli));
    printf("An unsigned long int has a range of unique values from 0 to +4,294,967,295 in a 32-bit compiler and 0 to +18,446,744,073,709,551,615 in a 64 bit compiler."$
// Display information for signed long int data type
    printf("An signed long int is %d bytes\n", sizeof(sli));
    printf("A signed long int has a range of unique values from -2,147,483,648 to +2,147,483,647 in a 32 bit compiler, and -9,223,372,036,854,775,808 to +9,223,372,036$
// Display information for unsigned long long int data type
    printf("An unsigned long long int is %d bytes\n", sizeof(ulli));
    printf("An unsigned long long int has a range of unique values from 0 to +18,446,744,073,709,551,615.");
// Display information for signed long long int data type
    printf("An signed long long int is %d bytes\n", sizeof(slli));
    printf("A signed long long int has a range of unqiue values from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.");

    return(0);
}
//--- range.c end ---

Execution



Reflection

Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project? I learned that signed characters can hold negatives values, while unsigned characters cannot, however unsigned characters can hold more positive values.

References

In performing this project, the following resources were referenced:

Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)

user/drobie2/portfolio/datatypes.txt · Last modified: 2011/10/13 22:59 by drobie2