User Tools

Site Tools


user:jcavalu3:portfolio:cprogproject0

Project: DATA TYPE EXPLORATION

A project for C/C++ Programming by Josh Cavaluzzi during the Spring 2012.

This project was begun on February 2, 2012 and is anticipated to take 5 days to complete. Project was completed on February 11, 2012, at 12:15 in the morning… I am tired!

Objectives

The objective of this project is to explore the different data types available for use in the C Programming Language.

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 use programming to show the different data types available in the C Programming Language. A list of them are:

  1. Unsigned char
  2. Signed char
  3. Unsigned short int
  4. Signed short int
  5. Unsigned int
  6. Signed int
  7. Unsigned long int
  8. Signed long int
  9. Unsigned long long int
  10. Signed long long int

Using this different data types allows programmers to use different amounts of space according to the size of the variable that they would like to represent. The smallest data type is the char (character) which has only one byte of memory available. The largest is the long long int (integer), which has 8 bytes of memory available.

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()
{
        // Signed Variables
        signed char sc = 0;
        signed short int ssint = 0;
        signed int sint = 0;
        signed long int slint = 0;
        signed long long int sllint = 0;
 
        // Unsigned Variables
        unsigned char uc = 0;
        unsigned short int usint = 0;
        unsigned int uint = 0;
        unsigned long int ulint = 0;
        unsigned long long int ullint = 0;
 
        // Junk Variable
        unsigned long long int quantity = 0;
 
        // Display information for unsigned char data type
        printf("\nAn unsigned char is %u bytes\n", sizeof(uc));
        printf("The range of an unsigned char is %hhu to %hhu\n", uc, (uc-1));
        quantity = (unsigned char)(uc-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 int data type
        printf("An unsigned int is %d bytes\n", sizeof(uint));
        printf("The range of an unsigned int is %u to %u\n", uint, (uint-1));
        quantity = (unsigned int)(uint-1);
        printf("An unsigned int 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(usint));
        printf("The range of an unsigned short int is %hu to %hu\n",  usint, (usint-1));
        quantity = (unsigned short int)(usint-1);
        printf("An unsigned short 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(sint));
        quantity = (unsigned int)pow(2, (sizeof(sint)*8));
        printf("The range of a signed int is %d to %d\n", (sint-(quantity/2)), (sint+(quantity/2)-1));
        printf("A signed 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(ssint));
        quantity = (unsigned short int)pow(2, (sizeof(ssint)*8));
        printf("The range of a signed short int is %hd to %hd\n", (ssint-(quantity/2)), (ssint+(quantity/2)-1));
        printf("A signed short 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(ulint));
        printf("The range of an unsigned long int is %lu to %lu\n", ulint, (ulint-1));
        quantity = (unsigned long int)(ulint-1);
        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(slint));
        quantity = (unsigned long int)pow(2, (sizeof(slint)*8));
        printf("The range of a signed long int is %ld to %ld\n", (slint-(quantity/2)), (slint+(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(ullint));
        printf("The range of an unsigned long long int is %llu to %llu\n", ullint, (ullint-1));
        quantity = (unsigned long long int)(ulint-1);
        printf("An unsigned long long int can store %llu unique values\n\n", quantity);
 
        // Display information for signed long long int
        printf("A signed long long int is %d bytes\n", sizeof(sllint));
        quantity = (unsigned long long int)pow(2, (sizeof(sllint)*8));
        printf("The range of a signed long long int is %lld to %lld\n", (sllint-(quantity/2)), (sllint+(quantity/2)-1));
        printf("A signed long long int can store %llu unique values\n\n", quantity);
 
        return(0);
}

Execution

An unsigned char is 1 bytes
The range of an unsigned char is 0 to 255
An unsigned char can store 255 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 int is 4 bytes
The range of an unsigned int is 0 to 4294967295
An unsigned int can store 4294967295 unique values

A signed int is 4 bytes
The range of a signed int is -2147483647 to 2147483646
A signed int can store 4294967295 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 65535 unique values

A signed short int is 2 bytes
The range of a signed short int is -32767 to 32766
A signed short int can store 65535 unique values

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

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

Reflection

The project really helped me begin learning how the different data types work and the sizes of each. Before the project, my thoughts of them were a little clouded by confusion, but now that I understand what is happening and how much memory they actually use, I find them very interesting. Also, I used a few debugging strategies when I ran into problems, which helped me fix them very quickly and effectively. My final thought is the '%u', '%d', etc… I had no idea what those meant the first few classes, but now I have a full understanding (as far as I know) of what they mean, and it is extremely helpful when programming in C.

References

In performing this project, the following resources were referenced:

I believe that is the only reference I used, and it really didn't help me with what I needed.

user/jcavalu3/portfolio/cprogproject0.txt · Last modified: 2012/02/11 05:39 by jcavalu3