User Tools

Site Tools


user:dherman3:portfolio:datatypes

Project: DATA TYPE EXPLORATION

A project for Programming in C/C++ by Daniel Herman during the Fall 2011 Semester.

This project was begun on 9/24/11 and is anticipated to take 1 week to complete. Project was completed on 10/13/11.

Objectives

The purpose of this project is to explore different variables in the C/C++ Programming Language. By undergoing this project, I will learn more about variables and the role they take in programming.

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

In this project I will attempt to understand data types and the role they take in programming.

Some of the basic types of variables in C are:

  • char
  • int
  • float

The types used in this project are char and int.

These types can be used either signed or unsigned depending on the situation. An unsigned integer can only take positive values whereas a signed integer can take on positive or negative. They both hold the same amount of values so unsigned can be used for higher positive values.

The int type is able to be specified a size, such as short int, long int, or long long int. This can be used in a number of situations.

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.

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 ui = 0;
    signed short int si = 0;
    unsigned int uni = 0;
    signed int sni = 0;
    unsigned long int uli = 0;
    signed long int sli = 0;
    signed long long int slli = 0;
    unsigned long long int ulli = 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 info for unsigned short int data type
    printf("An unsigned short int is %d bytes\n", sizeof(ui));
    printf("The range of an unsigned char is 0 to 65535\n");
    printf("An unsigned short int can store 65536 unique values\n\n");
 
    // Display info for signed short int data type
    printf("A signed short int is %d bytes\n", sizeof(si));
    printf("The range of a signed short int is -32767 to 32767\n");
    printf("A signed short int can store 65536 unique values\n\n"); 
 
    // Display info for unsigned int data type
    printf("An unsigned int is %d bytes\n", sizeof(uni));
    printf("The range of an unsigned int is 0 to 65535\n");
    printf("An unsigned int can store 65536 unique values\n\n");
 
    // Display info for signed int data type
    printf("A signed int is %d bytes\n", sizeof(sni));
    printf("The range of a signed int is -32767 to 32767\n");
    printf("A signed int can store 65536 unique values\n\n");
 
    // Display info 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 0 to 4294967295\n");
    printf("An unsigned long int can store 4294967296 unique values\n\n");
 
    // Display info for signed long int data type
    printf("A signed long int is %d bytes\n", sizeof(sli));
    printf("The range of a signed long int is -2147483648 to 2147483647\n");
    printf("A signed long int can store 4294967296 unique values\n\n");
 
    // Display info 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 0 to 18446744073709551615\n");
    printf("An unsigned long long int can store 18446744073709551616 unique values\n\n");
 
    // Display info for signed long long int data type
    printf("A signed long long int is %d bytes\n", sizeof(slli));
    printf("The range of a signed long long int is -9223372036854775808 to 9223372036854775807\n");
    printf("A signed long long int can store 18446744073709551616 unique values\n\n");
 
    return(0);
}

Execution

Lab46:~/projects/datatypes$ ./range.out
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 char 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 -32767 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 65535
An unsigned int can store 65536 unique values

A signed int is 4 bytes
The range of a signed int is -32767 to 32767
A signed int can store 65536 unique values

An unsigned long int is 8 bytes
The range of an unsigned long int is 0 to 4294967295
An unsigned long int can store 4294967296 unique values

A signed long int is 8 bytes
The range of a signed long int is -2147483648 to 2147483647
A signed long int can store 4294967296 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 18446744073709551616 unique values

A signed long long int is 8 bytes
The range of a signed long long int is -9223372036854775808 to 9223372036854775807
A signed long long int can store 18446744073709551616 unique values

Reflection

Researching for this project lead me to learn about the limits header file. limits.h stores the values for maximum and minimum values of different data types. Also, I noticed how these different data types might be used in computing different amounts of values to save space or compute a larger amount of values.

References

In performing this project, the following resources were referenced:

These websites were useful in determining the ranges for each data type. I also learned about the limits header which stores the values of the minimum and maximum ranges from these documents.

user/dherman3/portfolio/datatypes.txt · Last modified: 2011/10/13 19:09 by dherman3