User Tools

Site Tools


user:rmatsch:portfolio:cprogproject0

Project: DATA TYPE EXPLORATION

A project for C++ by Robert Matsch during the spring 2012.

This project was begun on 2/8/2012 and is anticipated to take 1 day to complete. Project was completed on febuary 9, 2012.

Objectives

The objective for this project is to explore different data types in the C++ programming laguage.

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 will be to display the data type sizes, range of data types and unique values of the data type using the sizeof() and printf() funtion.

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: ./project0
 */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
    // Variables
      unsigned char uc = 0;
        signed char sc = 0;
        signed short int ssi = 0;
        unsigned short int usi =0;
        signed long int sli = 0;
        unsigned long int uli =0;
        signed long long int slli = 0;
        unsigned long long int ulli = 0;
        signed int si  = 0;
        unsigned int ui = 0;
        unsigned long long int quantity = 0;
 
        //display info for unsigned char data type
        printf("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) + 1;/* unsigned char -1 = 255 +1 for all unique values*/
         printf("An unsigned char can store %llu unique values\n\n", quantity);
 
        //display info for signed char data type
        printf("Signed char is %d bytes\n", sizeof(sc));
        quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); /*2 raised to the power of 8 times data type in  bytes */
         printf("The range of a signed char is %hhd to %hhd\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
         printf("A unsigned char can store %llu unique values\n\n", quantity);
 
        printf("Unsigned short int is %u bytes\n", sizeof(usi));
        quantity = (unsigned long long int)pow(2, (sizeof(usi)*8));
         printf("The range of a unsigned short int is %u to %llu\n", usi, ((quantity)-1));
         printf("An unsigned short int can store %llu unique values\n\n", quantity);
 
        printf("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 %lld to %lld\n", (ssi-(quantity/2)), (ssi+(quantity/2)-1));
        printf("A signed short int can store %llu unique values\n\n", quantity);
 
        printf("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 %lld to %lld\n", (si-(quantity/2)), (si+(quantity/2)-1));
          printf("A signed int can store %llu unique values\n\n", quantity);
 
        printf("Unsigned int is %u bytes\n", sizeof(ui));
        quantity = (unsigned long long int)pow(2, (sizeof(ui)*8));
         printf("The range of a unsigned int is %llu to %llu\n", ui,((quantity)-1));
        printf("An unsigned int can store %llu unique values\n\n", quantity);
 
        printf("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 %lld to %lld\n", (sli-(quantity/2)), (sli+(quantity/2)-1));
        printf("A signed long int can store %llu unique values\n\n", quantity);
 
        printf("Unsigned long int is %u bytes\n", sizeof(uli));
        quantity = (unsigned long long int)pow(2, (sizeof(uli)*8));
         printf("The range of a unsigned long long int is %llu to %llu\n", uli,((quantity)-1));
        printf("An unsigned long int can store %llu unique values\n\n", quantity);
 
        printf("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 %lld to %lld\n", (slli-(quantity/2)), (slli+(quantity/2)-1));
        printf("A signed long long int can store %llu unique values\n\n", quantity);
          printf("Unsigned long long int is %u bytes\n", sizeof(ulli));
        quantity = (unsigned long long int)pow(2, (sizeof(ulli)*8));
         printf("The range of a unsigned long long int is %llu to %llu\n", ulli,((quantity)-1));
        printf("An unsigned long long int can store %llu unique values\n\n", quantity);
 
 
return(0)
}
 

Execution

lab46:~/src/cprog$ $ ./project0 
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


Unsigned short int is 2 bytes
The range of a unsigned short int is 0 to 65535
An unsigned short int can store 65536 unique values

Signed short int is 2 bytes
The range of a signed short int is -32768 to 32767
A signed short int can store 65536 unique values

Signed int is 4 bytes
The range of a signed int is -2147483648 to 2147483647
A signed int can store 4294967296 unique values

Unsigned int is 4 bytes
The range of a unsigned int is 0 to 4294967295
An unsigned int can store 4294967296 unique values

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

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

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

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

lab46:~/src/cprog$ 

Reflection

Upon completion of the project i learned the different data types and sizes in c programming language, and that depending on if you have a 32 bit system or 64 bit system the values of int, char, (etc) will change.

References

In performing this project, the following resources were referenced:

  • Matthew Hass
  • The C programing Language 2nd edition
user/rmatsch/portfolio/cprogproject0.txt · Last modified: 2012/02/13 00:26 by rmatsch