User Tools

Site Tools


user:sswimle1:portfolio:cprogproject0

Project: DATA TYPE EXPLORATION

A project for C/C++ Programming by Shane Swimley during the Spring 2012.

This project was begun on 2/14/2012 and is anticipated to take 1 day to complete. Project was completed on 02/14/2012.

Objectives

Display Data Types:

Size of each data type.

Range of each data type.

Unique Characters of each data type.

Write the code to display all of this.

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

See objectives

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;
    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;    // 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);
 
    // Display information for unsigned short int data type
    printf("An unsigned short int is %d bytes\n", sizeof(usi));
    printf("The range of an unsigned short int is %hhu to %hhu\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 short int 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 %hhd to %hhd\n", (ssi-(quantity/2)), (ssi+(quantity/2)-1));
    printf("A signed short int can store %llu unique values\n\n", quantity);
 
    // 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 %hhu to %hhu\n", ui, (ui-1));
    quantity = (unsigned 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 int 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 %hhd to %hhd\n", (si-(quantity/2)), (si+(quantity/2)-1));
    printf("A signed int can store %llu unique values\n\n", quantity);
 
    // Display information for unsigned long int data type
    printf("An unsigned long int is %d bytes\n", sizeof(uli));
    printf("The range of an unsigned long int is %hhu to %hhu\n", uli, (uli-1));
    quantity = (unsigned 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 long int 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 %hhd to %hhd\n", (sli-(quantity/2)), (sli+(quantity/2)-1));
    printf("A signed long int can store %llu unique values\n\n", quantity);
 
    // Display information for unsigned long long int data type
    printf("An unsigned long long int is %d bytes\n", sizeof(ulli));
    printf("The range of an unsigned long long int is %hhu to %hhu\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 long long int 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 %hhd to %hhd\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

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 255
An unsigned short int can store 65536 unique values

A signed short int is 2 bytes
The range of a signed short int is 0 to -1
A signed short int can store 65536 unique values

An unsigned int is 4 bytes
The range of an unsigned int is 0 to 255
An unsigned int can store 0 unique values

A signed int is 4 bytes
The range of a signed int is 0 to -1
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 255
An unsigned long int can store 0 unique values

A signed long int is 8 bytes
The range of a signed long int is 1 to -2
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 255
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 1 to -2
A signed long long int can store 18446744073709551615 unique values

Reflection

My reflection is in bytes, data types, and unique values.

References

Referenced the examples that you gave in the project code.

user/sswimle1/portfolio/cprogproject0.txt · Last modified: 2012/02/14 16:40 by sswimle1