User Tools

Site Tools


Sidebar

projects

haas:spring2014:cprog:projects:datatypes

This is an old revision of the document!


Project: DATA TYPE EXPLORATION

A project for C/C++ Programming.

Objective

To familiarize yourself with the available data types in C- the variations, the attributes, the ranges therein.

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 change to course-specific source directory
  • 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 bit combinations are possible therein
  • ability to use version control system to track created source file(s)

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 (as accurately as possible) display the total quantity of values possible with each type.

The data types covered for this project will include signed and unsigned variations of:

  • char
  • short int
  • int
  • long int
  • long long int

The sizeof() and printf() functions, as well as arithmetic and logical operators, will be utilized in performing much of the work.

Code

Skeleton Program

Here is the skeleton of a program you'll want to use for this project:

/*
 * datatypes.c - A program to display information for signed and unsigned data types
 *
 *
 * Compile with: gcc -o datatypes datatypes.c -lm
 * Execute with: ./datatypes
 */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
    // Variables
    unsigned char uchr = 0;
 
    // Code for unsigned char
 
    // Code for signed char
 
    // Code for unsigned short int
 
    // Code for signed short int
 
    // Code for unsigned int
 
    // Code for signed int
 
    // Code for unsigned long int
 
    // Code for signed long int
 
    // Code for unsigned long long int
 
    // Code for signed long long int
 
    return(0);
}

unsigned char code block

Following is provided code giving you the requested information for an unsigned char. Study it, implement it, and adapt it as needed to the other data types:

    // Code for unsigned char
    fprintf(stdout, "TYPE: unsigned char, ");
    fprintf(stdout, "bytes: %hhu, ", sizeof(uchr)); 
    fprintf(stdout, "low: %hhu, ",  (uchr & 0x00)); // Why 2 zeros?
    fprintf(stdout, "high: %hhu, ", (uchr | 0xFF)); // Why 2 F's?
    uchr = uchr - 1;                                 // What does this line do?
    fprintf(stdout, "qty: %hu\n",   (uchr+1));      // What is going on here?

Execution

lab46:~/src/cprog$ ./datatypes
TYPE: unsigned char, bytes: 1, low: 0, high: 255, qty: 256
lab46:~/src/cprog$ 

Reflection

Be sure to provide any commentary on your opus regarding realizations had and discoveries made during your pursuit of this project.

Also, answer the following questions:

  • What two data types appeared “the same”?
    • Why may this may be?
    • Is this universal for all systems, or just a certain subset?
  • If sign was left unspecified, which state is assumed by default?
  • What are the & and | operators?
    • How did they help you achieve the solution?
  • If you experienced trouble displaying the total quantity for a certain type, why was this?
  • Based on your program's output, what are the total bits allocated for:
    • signed char
    • unsigned short int
    • unsigned int
    • signed int
    • signed long long int

Submission

To successfully complete this project, the following criteria must be met:

  • Executed program must display a total of 10 lines, one for each data type.
  • Code must be nicely and consistently indented (you may use the indent tool)
  • Code must be commented
  • Output Formatting (including spacing– overall field alignment is not needed) of program must conform to the provided output (see unsigned char example above).
  • Track the source code in a repository
  • Submit a copy of your source code to me using the submit tool.

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog datatypes datatypes.c
Submitting cprog project "datatypes":
    -> datatypes.c(OK)

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

Verify submission

To verify you submitted successfully, you may run the following (from anywhere on lab46):

lab46:~$ verify cprog datatypes
datatypes: submitted successfully

Note if automated assessment is available for the project, you may actually see results in the output as well.

haas/spring2014/cprog/projects/datatypes.1389574886.txt.gz · Last modified: 2014/01/13 01:01 by wedge