User Tools

Site Tools


haas:spring2011:cprog:tasks:task1


Corning Community College

C/C++ Programming


Task 1: Data Types

~~TOC~~

Objective

To become familiar with the various data types available to us in the C programming environment and their attributes.

Background

In class, we discussed the various data types available to us. They are:

Types

C/C++ keyword name description
char character one of our smallest data types, also used for displaying character data
short int short integer next bigger, used for display integer (whole number) data
int integer considered the “standard” or median size integer
long int integer a longer range integer (sometimes)
long long int integer an even longer range integer (if long isn't longer already)
float floating point number used for the display of floating point data
double floating point number a bigger precision floating point data type
long double floating point number an even bigger precision floating point data type

Sign

In addition, we can make use the following keywords, when prefixed to any of the above, to modify the range represented:

  • signed (assumed in absence)
  • unsigned

The presence of a sign indicates a value can display both positive and negative values. The absence indicates the display of ONLY positive values.

You'll see reference to data type 'quantities'. This refers to the total possible population of values able to be represented in a particular length of bits (each data type is of a fixed size in bytes). For example, an 8-bit number can hold a quantity of 256 unique values.

The computer stores everything as a binary number ultimately. So for the computer to represent a number, we have to look to base 2. In this example, we'll use a 3-bit number:

binary decimal
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

If we only cared about an unsigned quantity, our work would stop here. But, all too often we wish to display negative values. The method adopted to represent signed quantities involves allocating the most significant (left-most) bit to be used as a sign bit (0 means positive, 1 means negative).

At first, this would seem to effectively cut our range in half… but instead of cutting it in half, we shift the range, straddling it across 0.

To represent a negative value, we also use a process known as two's complement, which saves us the inconvenience of having something like a positive and negative zero (which wouldn't do anyone any good).

Two's complement is calculated by taking the original value (in binary), inverting all its bits, and adding one.

For example, to get the two's complement of 5:

Original binary for 3: 011
Inverted binary for 3: 100
Adding one to this --> + 1
Gives a result of ---> 101 <--- THIS is how the computer will represent -3

So to represent the above table as a signed quantity:

binary unsigned decimal signed decimal
000 0 0
001 1 1
010 2 2
011 3 3
100 4 -4
101 5 -3
110 6 -2
111 7 -1

We STILL have 8 discrete values… we just change the starting and ending values of our counting range.

This is an important realization to have with computers. They cannot just keep adding on new bits. Data Types are of a fixed allocated storage size. We must work with these pre-existing units in the representation of our values if we wish to use the other available facilities (like addition, subtraction, multiplication, division).

It's all Numbers

In some ways, at this level we really only have 2 types of data:

  • whole numbers
  • decimal/floating point numbers

It is just how we decide to display our numbers. The deeper you explore computers you will discover this is more and more the truth (numbers are either whole/integer or floating point).

So what about letters/characters? Sure, computers CAN display symbols… but those symbols are represented by (integer) numbers. I want everyone to realize this… there is nothing magical or special about letters. They are just numbers that we tell the computer to translate into a particular letter.

Exploration

What I'd like for you to do is to play with some code to get a better feel for some of these data types.

The code to use is as follows:

#include <stdio.h>
 
int main()
{
    signed char val1;
    unsigned char val2;
 
    val1 = 0;
    val2 = 0;
 
    printf("A char is %d bytes\n", sizeof(char));
    printf("val1 before is: %d\n", val1);
    printf("val2 before is: %u\n", val2);  // note we're using %u here for "unsigned"
 
    val1 = val1 - 1;
    val2 = val2 - 1;
 
    printf("val1 after: %d\n", val1);
    printf("val2 after: %u\n", val2);  // note we're using %u here for "unsigned"
 
    return(0);
}

Using the code provided above, along with appropriate modifications, I'd like for you to give me answers to the following:

  1. How big (in bytes) is a char?
  2. What happens when you subtract 1 from zero in a signed quantity?
  3. What happens when you subtract 1 from zero in an unsigned quantity?
  4. Which is bigger (in bytes), a signed or unsigned char?
  5. What is the range (starting to ending) of a signed char?
  6. What is the range (starting to ending) of an unsigned char?
  7. How big (in bytes) is a short int?
  8. How big (in bytes) is an int?
  9. How big (in bytes) is a long int?
  10. What is the range (starting to ending) of an unsigned short int?
  11. What is the range (starting to ending) of a signed int?
  12. How big (in bytes) is a float?
  13. How big (in bytes) is a double?
  14. How big (in bytes) is a long double?
  15. What data type would be the best fit (smallest available) for the value: -44237
  16. What data type would be the best fit (smallest available) for the value: 257

Review of Compiling/Executing

Just to review the compilation/execution process for working with your source code, if we had a file, hello.c, that we wished to compile to a binary called hello, we'd first want to compile the code, as follows:

lab46:~/src/cprog$ gcc -o hello hello.c
lab46:~/src/cprog$ 

Assuming there are no syntax errors or warnings, and everything compiled correctly, you should just get your prompt back. In the event of problems, the compiler will be sure to tell you about them.

Conceptually, the arrangement is as follows:

gcc -o BINARY_FILE SOURCE_FILE

The BINARY_FILE comes immediately after the -o, and the SOURCE_FILE, must never immediately follow a -o. It can precede, and such is perfectly valid (especially if you feel that way more intuitive).

To execute your binary, we need to specify a path to it, so we use ./, which basically says “in the current directory”:

lab46:~/src/cprog$ ./hello
Hello, World!
lab46:~/src/cprog$ 

Submission

All questions in this assignment require an action or response. Please organize your responses into an easily readable format and submit the final results to your instructor per the appropriate methods.

Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.

When complete, questions requiring a response can be electronically submit using the following form:

<html><center><a href=“http://lab46.corning-cc.edu/haas/content/cprog/submit.php?task1”>http://lab46.corning-cc.edu/haas/content/cprog/submit.php?task1</a></center></html>

Additionally, the successful results of the following actions will be considered for evaluation:

  • placement of the code to help you solve #10 in your ~/src/cprog directory
  • name that file: task1.c
  • addition/commit of task1.c into your repository

As always, the class mailing list and class IRC channel are available for assistance, but not answers.

haas/spring2011/cprog/tasks/task1.txt · Last modified: 2011/02/01 02:17 by 127.0.0.1