User Tools

Site Tools


user:swilli31:portfolio:cprogproject0

Project: DATA TYPE EXPLORATION

A project for C/C++ Programming by Stephanie Williams during the Spring Semester 2012.

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

Objectives

This project is to understand the different variables used within the C programming language. By the completion of this project, I will be able to further understand how much space is available for certain variable and be able to know what is needed for certain tasks.

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

This project is to observe the size and range of different data types in C. C is one of the many programming languages available for the computer. It allows for many things to be manipulated and programmed while still maintaing a higher level readability and functionality for the programmer.

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()
{
        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;
 
 
        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);
 
        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("An unsigned short 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);
        printf("An unsigned short int can store %llu unique values\n\n", quantity);
 
        printf("A signed short int is %d 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)), (sc+(quantity/2)-1));
 
 
        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);
        printf("An unsigned int can store %llu unique values\n\n", quantity);
 
        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);
        printf("An unsigned int can store %llu unique values\n\n", quantity);
 
        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 %hhd to %hhd\n", (si-(quantity/2)), (si+(quantity/2)-1));
 
 
        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);
        printf("An unsigned long int can store %llu unique values\n\n", quantity);
 
        printf("A signed long int is %d bytes\n", sizeof(sli));
        quantity=(unsigned long long int)pow(2, (sizeof(sli)*8));
        printf("The range of a signed long int is %hhd to %hhd\n", (sli-(quantity/2)), (sli+(quantity/2)-1));
 
        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);
        printf("An unsigned long long int can store %llu unique values\n\n", quantity);
 
        printf("A signed long long int is %d bytes\n", sizeof(slli));
        quantity=(unsigned long long int)pow(2, (sizeof(slli)*8));
        printf("The range of a signed long long int is %hhd to %hhd\n", (slli-(quantity/2)), (slli+(quantity/2)-1));
 
        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
An unsigned short int is 2 bytes
The range of an unsigned short int is 0 to 65535
An unsigned short int can store 65535 unique values

A signed short int is 2 bytes
The range of a signed short int is -32768 to 32767
An unsigned int is 4 bytes
The range of an unsigned int is 0 to 255
An unsigned int can store 4294967295 unique values

A signed int is 4 bytes
The range of a signed int is 0 to -1
An unsigned long int is 8 bytes
The range of an unsigned long int is 0 to 255
An unsigned long int can store 18446744073709551615 unique values

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

A signed long long int is 8 bytes
The range of a signed long long int is 1 to -2
lab46:~/src/cprog$ 

Reflection

This project is very informative. Not only did I delve into understanding the different types but also had the opportunity to see the difference a sign on a type makes. The long long int has room for many unique values because of its large size. I do believe I have a better grasp of what type of thing I need for certain programs.

References

I used class notes and hints from the instructor.

user/swilli31/portfolio/cprogproject0.txt · Last modified: 2012/02/10 20:05 by swilli31