A project for C++ by Rich Richardson during the Fall 2011 SEMESTER YEAR.
This project was begun on September 19 and is anticipated to take 1 week to complete. Project was completed on September 30, 2011.
This code is used to determine the values for “signed char” and “unsigned char”. This project is an exploration of the various Data Types available for use with variables in C. I hope to get an understanding of Variables and what different types of data can be stored, and how they are stored. Learn to input code properly, compile it properly and test it for functionality.
In order to complete this project, I have achieved the following:
State the idea or purpose of the project. What are you attempting to pursue?
Upon approval, you'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.
Providing any links to original source material, such as from a project page, is a good idea.
You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)
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:
The sizeof() and printf() functions, as well as arithmetic operators, will be utilized in performing much of the work.
/* * range.c - A program to display information for signed and unsigned data char types * * /*--- range.c begin --- * Compile with: gcc -o range range.c -lm * Execute with: ./range */ #include <stdio.h> #include <math.h> int main() { // Variables unsigned long long int quantity = 0; unsigned char uc = 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) + 1; // What does this line do? printf("An unsigned char 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)); // What is happening? 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); return(0); } //--- range.c end ---
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
Signed char value indicates whether the value is positive or negative.
Unsigned char value is treated as positive.
An unsigned is 0 - 255, signed is -128 to 127. Either can store a range of 256 unique values.
The difference between signed and unsigned variables is that signed variables can be either negative or positive but unsigned variables can only be positive. By using an unsigned variable you can increase the maximum positive range.
In performing this project, the following resources were referenced: