User Tools

Site Tools


user:dgirard3:portfolio:datatypes

Project: DATA TYPE EXPLORATION

A project for C Programming by Derek Girardi during the Fall semester 2011.

This project was begun on November 2, 2011 and is anticipated to take 2 days to complete. Project was completed on November 3, 2011.

Objectives

The purpose of this project is to learn about the datatype. We are to learn to about how each data type can be used, how much it can hold and what can be stored in it. With that knowledge we will be able to be more precise when it comes to our programming because we will know more about the types.

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 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 combinations are possible therein

Background

In this project we are learning about the datatypes on the C language. You have char, short int, int, long int and long long int. There are obviously others but these are the main datatypes and it helps to know the size of each. knowing the size will help one judge the appropriate datatype to use in a program. This project will also help with arguments, teaching which argument goes with what. Each type has its own argument to go with.

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

The data types covered for this project will include:

  • unsigned char
  • signed char
  • unsigned short int
  • signed short int
  • unsigned int
  • signed int
  • unsigned long int
  • signed long int
  • unsigned long long int
  • signed long long int

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

Attributes

  • variables: A variable for each of the stated data types will be declared and manipulated.
  • I/O: To ascertain the properties of the data types, important values will be displayed to STDOUT.

Code

This is for signed and unsigned char datatype:

/*--- range.c begin ---
  2  * Compile with: gcc -o range range.c -lm
  3  * Execute with: ./range
  4  */
  5 #include <stdio.h>
  6 #include <math.h>
  7
  8 int main()
  9 {
 10     // Variables
 11     unsigned long long int quantity = 0;
 12     unsigned char uc = 0;
 13     signed char sc = 0;
 14
 15 // Display information for unsigned char data type
 16     printf("An unsigned char is %d bytes\n", sizeof(uc));
 17     printf("The range of an unsigned char is %hhu to %hhu\n", uc, (uc-1));
 18     quantity = (unsigned char)(uc-1) + 1;    // What does this line do?
 19     printf("An unsigned char can store %llu unique values\n\n", quantity);
 20
 21 // Display information for signed char data type
 22     printf("A signed char is %d bytes\n", sizeof(sc));
 23     quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); 
 24     printf("The range of a signed char is %hhd to %hhd\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
 25     printf("A signed char can store %llu unique values\n\n", quantity);
 26
 27     return(0);
 28 }
 29 //--- range.c end ---

</code>

Code for signed and unsigned short int:

/*--- range.c begin ---
  2  * Compile with: gcc -o range range.c -lm
  3  * Execute with: ./range
  4  */
  5 #include <stdio.h>
  6 #include <math.h>
  7
  8 int main()
  9 {
 10     // Variables
 11     unsigned long long int quantity = 0;
 12     unsigned short int uc = 0;
 13     signed short int sc = 0;
 14
 15 // Display information for unsigned short int data type
 16     printf("An unsigned short int is %d bytes\n", sizeof(uc));
 17     printf("The range of an unsigned short int is %hu to %hu\n", uc, (uc-1));
 18     quantity = (unsigned short int)(uc-1) + 1;    // What does this line do?
 19     printf("An unsigned short int can store %llu unique values\n\n", quantity);
 20
 21 // Display information for signed short int data type
 22     printf("A signed short int is %d bytes\n", sizeof(sc));
 23     quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); 
 24     printf("The range of a signed short int is %hd to %hd\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
 25     printf("A signed short int can store %llu unique values\n\n", quantity);
 26
 27     return(0);
 28 }
 29 //--- range.c end ---

For signed and unsigned int:

  1 /*--- range.c begin ---
  2  * Compile with: gcc -o range range.c -lm
  3  * Execute with: ./range
  4  */
  5 #include <stdio.h>
  6 #include <math.h>
  7
  8 int main()
  9 {
 10     // Variables
 11     unsigned long long int quantity = 0;
 12     unsigned int uc = 0;
 13     signed int sc = 0;
 14
 15 // Display information for unsigned int data type
 16     printf("An unsigned int is %d bytes\n", sizeof(uc));
 17     printf("The range of an unsigned int is %u to %u\n", uc, (uc-1));
 18     quantity = (unsigned long long int)(uc-1) + 1;    // What does this line do?
 19     printf("An unsigned int can store %llu unique values\n\n", quantity);
 20
 21 // Display information for signed int data type
 22     printf("A signed short int is %d bytes\n", sizeof(sc));
 23     quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); // What is happening?
 24     printf("The range of a signed int is %i to %i\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
 25     printf("A signed int can store %llu unique values\n\n", quantity);
 26
 27     return(0);
 28 }
 29 //--- range.c end ---
 30

For signed and unsigned long int:

  1 /*--- range.c begin ---
  2  * Compile with: gcc -o range range.c -lm
  3  * Execute with: ./range
  4  */
  5 #include <stdio.h>
  6 #include <math.h>
  7
  8 int main()
  9 {
 10     // Variables
 11     unsigned long long int quantity = 0;
 12     unsigned long int uc = 0;
 13     signed long int sc = 0;
 14
 15 // Display information for unsigned long int data type
 16     printf("An unsigned long int is %d bytes\n", sizeof(uc));
 17     printf("The range of an unsigned long int is %lu to %lu\n", uc, (uc-1));
 18     quantity = (unsigned long int)(uc-1);    // What does this line do?
 19     printf("An unsigned long int can store %llu unique values\n\n", quantity);
 20
 21 // Display information for signed long int data type
 22     printf("A signed long int is %d bytes\n", sizeof(sc));
 23     quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); // What is happening?
 24     printf("The range of a signed long int is %ld to %ld\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
 25     printf("A signed long int can store %llu unique values\n\n", quantity);
 26
 27     return(0);
 28 }
 29 //--- range.c end ---
 30

For signed and unsigned long long int:

  1 /*--- range.c begin ---
  2  * Compile with: gcc -o range range.c -lm
  3  * Execute with: ./range
  4  */
  5 #include <stdio.h>
  6 #include <math.h>
  7
  8 int main()
  9 {
 10     // Variables
 11     unsigned long long int quantity = 0;
 12     unsigned long long int uc = 0;
 13     signed long long int sc = 0;
 14
 15 // Display information for unsigned long long int data type
 16     printf("An unsigned long long int is %d bytes\n", sizeof(uc));
 17     printf("The range of an unsigned long long int is %llu to %llu\n", uc, (uc-1));
 18     quantity = (unsigned long long int)(uc-1);    // What does this line do?
 19     printf("An unsigned long long int can store %llu unique values\n\n", quantity);
 20
 21 // Display information for signed long long int data type
 22     printf("A signed long long int is %d bytes\n", sizeof(sc));
 23     quantity = (unsigned long long int)pow(2, (sizeof(sc)*8)); // What is happening?
 24     printf("The range of a signed long long int is %lld to %lld\n", (sc-(quantity/2)), (sc+(quantity/2)-1));
 25     printf("A signed long long int can store %llu unique values\n\n", quantity);
 26
 27     return(0);
 28 }
 29 //--- range.c end ---
 30

Execution

For signed and unsigned char:

lab46:~/src/cprog$ ./data
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

lab46:~/src/cprog$

For unsigned and signed short int:

lab46:~/src/cprog$ ./data2
An unsigned short int is 2 bytes
The range of an unsigned short int is 0 to 65535
An unsigned short int can store 65536 unique values

A signed short int is 2 bytes
The range of a signed short int is -32768 to 32767
A signed short int can store 65536 unique values

lab46:~/src/cprog$

For signed and unsigned int:

lab46:~/src/cprog$ ./data3
An unsigned int is 4 bytes
The range of an unsigned int is 0 to 4294967295
An unsigned int can store 4294967296 unique values

A signed short int is 4 bytes
The range of a signed int is -2147483648 to 2147483647
A signed int can store 4294967296 unique values

lab46:~/src/cprog$

For signed and unsigned long int:

lab46:~/src/cprog$ ./data4
An unsigned long int is 8 bytes
The range of an unsigned long int is 0 to 18446744073709551615
An unsigned long int can store 18446744073709551615 unique values

A signed long int is 8 bytes
The range of a signed long int is -9223372036854775807 to 9223372036854775806
A signed long int can store 18446744073709551615 unique values

for signed and unsigned long long int:

lab46:~/src/cprog$ ./data5
An unsigned long long int is 8 bytes
The range of an unsigned long long int is 0 to 18446744073709551615
An unsigned long long int can store 18446744073709551615 unique values

A signed long long int is 8 bytes
The range of a signed long long int is -9223372036854775807 to 9223372036854775806
A signed long long int can store 18446744073709551615 unique values

Reflection

I have learned the size of datatypes and the argument that are associated with each type. However i feel long int and long long int should not be similar in size but that is what i got from this program unless something has gone wrong, regardless i learned a lot from this project and it was easier then expected, just a few bumps in the road.

References

user/dgirard3/portfolio/datatypes.txt · Last modified: 2011/11/03 16:49 by dgirard3