A project for CPROG C/C++ by Dalton Robie during the Fall 2011 Semester.
This project was begun on 9/21/11 and is anticipated to take 1 week to complete. Project was completed on September 27th, 2011.
The purpose of this project is to be able to explore variables used in C.
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
The purpose of this project is to explore data types and learn their values and ranges.
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 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; unsigned short int usi = 0; signed short int ssi = 0; unsigned int ui = 0; signed int si = 0; unsigned long int uli = 0; signed long int sli = 0; unsigned long long int ulli = 0; signed long long int slli = 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; 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)); 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 unsigned short int data type printf("An unsigned short int is %d bytes\n", sizeof(usi)); printf("An unsigned short int can hold 65,535 unquie values"); // Display information for signed short int data type printf("An signed short int is %d bytes\n", sizeof(ssi)); printf("A signed short int has a range of unique values from -32,768 to +32,767"); // Display information for unsigned int data type printf("An unsigned int is %d bytes\n", sizeof(ui)); printf("An unsigned int has a range of unique values from 0 to +4,294,967,295."); // Display information for signed int data type printf("An signed int is %d bytes\n", sizeof(si)); printf("A signed int has a range of unique values from -2,147,483,648 to +2,147,483,647."); // Display information for unsigned long int data type printf("An unsigned long int is %d bytes\n", sizeof(uli)); printf("An unsigned long int has a range of unique values from 0 to +4,294,967,295 in a 32-bit compiler and 0 to +18,446,744,073,709,551,615 in a 64 bit compiler."$ // Display information for signed long int data type printf("An signed long int is %d bytes\n", sizeof(sli)); printf("A signed long int has a range of unique values from -2,147,483,648 to +2,147,483,647 in a 32 bit compiler, and -9,223,372,036,854,775,808 to +9,223,372,036$ // Display information for unsigned long long int data type printf("An unsigned long long int is %d bytes\n", sizeof(ulli)); printf("An unsigned long long int has a range of unique values from 0 to +18,446,744,073,709,551,615."); // Display information for signed long long int data type printf("An signed long long int is %d bytes\n", sizeof(slli)); printf("A signed long long int has a range of unqiue values from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807."); return(0); } //--- range.c end ---
Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project? I learned that signed characters can hold negatives values, while unsigned characters cannot, however unsigned characters can hold more positive values.
In performing this project, the following resources were referenced:
Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)