Corning Community College

HPC Fundamentals


Assignment #2: Fun with numbers in C




TODO

As we investigate operations on the computer with regards to the UNIX system, including various System Administration activities, it is important to have a working fundamental knowledge of some of the low level operations of the computer.

Specifically, how computers store and represent numbers. We've probably been told that computers use binary, and can logically conclude that all numbers on the computer are represented in binary.

What's more, is that computers do not have infinite resources, space must be precisely allocated to anything that is to be stored on them.

With our exploration of programming in C this week, we wrote a simple program that displayed the storage size of a particular variable (and therefore that type of data), as well as doing some simple math and displaying the before and after values to determine how data is stored and represented.

Here's the program, for a signed char:

#include<stdio.h>

int main()
{
	char a;	// declare a char
	a = 0;	// initialize it

	printf("a is %d bytes\n", sizeof(a));
	printf("before: %d\n", a);
	
	a = a - 1;

	printf("after: %d\n", a);

	return(0);
}

Although it is not the complete list, we have the following data types to work with:

To deal with a signed vs. unsigned quantity, simply add the word "signed" or "unsigned" before the variable's data type when it is declared, for example:

unsigned char a;

I'll tell you that, if you omit the signed/unsigned keyword, the compiler assumes a signed value by default (if you'd like to save on typing).

Please do the following:

  1. Modify the program to test the other data types. (Reference the printf(3) manual page for information on format specifiers).
  2. Tell me how many bytes are allocated for each data type.
  3. Tell me what the upper bound is for each data type (signed and unsigned)
  4. Tell me what the lower bound is for each data type (signed and unsigned)

NOTE: To access the section 3 manual page for printf, you'd want to run: man 3 printf on the command line.

ALSO: When displaying signed values, BE SURE to use signed format specifiers in printf. Likewise, when displaying unsigned values, BE SURE to use unsigned format specifiers in printf. Otherwise, you will not get the expected results and will wonder why you're getting a lot of duplicate results.

When you have your responses compiled, please enter them into the appropriate field in the electronic assignment submission form located here:

SUBMIT YOUR RESPONSES

Be sure to ask if you have any questions.

Last Updated: January 30, 2009