User Tools

Site Tools


user:smalik2:portfolio:cprogproject0

Project: DATA TYPE EXPLORATION

A project for C/C++ Programming by Saad Malik during Spring 2012.

This project was begun on 2/9/12 and is anticipated to take a day to complete. Project was completed on February 10, 2012.

Objectives

The point is to write a code that will display the bytes and range associated with signed and unsigned variable types.

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

Write a code that will display the amount of bytes and the range and amount of values that different variable types can hold.

Basically, take the example code provided and apply it to eight other variable types inside one program.

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

/*
 * Project0.c - A program to display information for signed and unsigned data char types
 *
 *
 * Compile with: gcc -o Project0 Project0.c -lm
 * Execute with: ./Project0
 */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
        // UNSIGNED VARIABLES
        unsigned long long int quantity = 0;
        unsigned long long int ullong = 0;
        unsigned long int ulong = 0;
        unsigned int uint = 0;
        unsigned short int ushort = 0;
        unsigned char uc = 0;
 
        // SIGNED VARIABLES
        signed long long int sllong = 0;
        signed long int slong = 0;
        signed int sint = 0;
        signed short int sshort = 0;
        signed char sc = 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);    //In quantity, stores the value of the variable - 1 = uppermost value of the variable b/c it loops to top (can't go negative).
        printf("An unsigned 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(ushort));
        printf("The range of an unsigned short int is %hu to %hu\n", ushort, (ushort-1));
        quantity = (unsigned short)(ushort-1);    //In quantity, stores the value of the variable - 1 = uppermost value of the variable b/c it loops to top (can't go negative).
        printf("An unsigned 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(uint));
        printf("The range of an unsigned int is %u to %u\n", uint, (uint-1));
        quantity = (unsigned int)(uint-1);    //In quantity, stores the value of the variable - 1 = uppermost value of the variable b/c it loops to top (can't go negative).
        printf("An unsigned 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(ulong));
        printf("The range of an unsigned long int is %lu to %lu\n", ulong, (ulong-1));
        quantity = (unsigned long int)(ulong-1);    //In quantity, stores the value of the variable - 1 = uppermost value of the variable b/c it loops to top (can't go negative).
        printf("An unsigned 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(ullong));
        printf("The range of an unsigned long long int is %llu to %llu\n", ullong, (ullong-1));
        quantity = (unsigned long long int)(ullong-1);    //In quantity, stores the value of the variable - 1 = uppermost value of the variable b/c it loops to top (can't go negative).
        printf("An unsigned long long int 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)); //In quantity, store size of variable type * 8--> # of bits. 2 raised to that power --> # of combinations. 
        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 signed short int data type
        printf("A signed short int is %d bytes\n", sizeof(sshort));
        quantity = (unsigned long long int)pow(2, (sizeof(sshort)*8)); //In quantity, store size of variable type * 8--> # of bits. 2 raised to that power --> # of combinations. 
        printf("The range of a signed short int is %hd to %hd\n", (sshort-(quantity/2)), (sshort-(quantity/2)-1));
        printf("A signed 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 long long int)pow(2, (sizeof(sint)*8)); //In quantity, store size of variable type * 8--> # of bits. 2 raised to that power --> # of combinations. 
        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 long int data type
        printf("A signed long int is %d bytes\n", sizeof(slong));
        quantity = (unsigned long long int)pow(2, (sizeof(slong)*8)); //In quantity, store size of variable type * 8--> # of bits. 2 raised to that power --> # of combinations. 
        printf("The range of a signed long int is %ld to %ld\n", (slong-(quantity/2)), (slong+(quantity/2)-1));
        printf("A signed char 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(sllong));
        quantity = (unsigned long long int)pow(2, (sizeof(sllong)*8)); //In quantity, store size of variable type * 8--> # of bits. 2 raised to that power --> # of combinations.
        printf("The range of a signed long long int is %lld to %lld\n", (sllong-(quantity/2)), (sllong+(quantity/2)-1));
        printf("A signed char can store %llu unique values\n\n", quantity);
 
 
        return(0);
}
 
 
^G Get Help                      ^O WriteOut                      ^R Read File                     ^Y Prev Page                     ^K Cut Text                      ^C Cur Pos
^X Exit                          ^J Justify                       ^W Where Is                      ^V Next Page                     ^U UnCut Text                    ^T To Spell

Execution

lab46:~/src/cprog/Projects$ ./Project0
An unsigned char is 1 bytes
The range of an unsigned char is 0 to 255
An unsigned char can store 255 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

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

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 char is 1 bytes
The range of a signed char is -128 to 127
A signed char can store 256 unique values

A 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

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

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

lab46:~/src/cprog/Projects$ 

Reflection

I had a lightbulb moment when I figured out how the code was producing the range of values for the given variable type, which I appreciated.

Also, now that I have written code to show it, I think I will more easily be able to remember the values that can be held by different types. I know off the top of my head now that a char can go upto 255, short int to 65 thousand, and int upto 4.2 billion. And long and long long int go upto something insane.

References

In performing this project, the following resources were referenced:

None.

user/smalik2/portfolio/cprogproject0.txt · Last modified: 2012/02/10 20:17 by smalik2