Table of Contents

Project: Data Types

A project for CSCS 1320 by Paul Sechrist during the Spring 2013.

Objectives

To show what the size of different data types are. Useful when running on a different system as well.

Code

Failed Program

1
#include<stdio.h>
int main()
{
 
//      signed char sclow, schigh;
//      sclow=((unsigned char)(0-1)./2)+1;
//      schigh=((unsigned char)(0-1)./2);
//      printf("low is %hhd, high is %hhd\n", sclow, schigh);
 
//%hhu unsigned half half
//%hu unsigned half int
//%u unsigned int
//%lu unsigned long int
//%llu unsigned long long int
 
//unsigned half half int
        unsigned char a=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "a address: 0x%X\n",&a);
        fprintf(stdout, "a contains: %hhu\n",a);
        a=a-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "a address: 0x%X\n",&a);
        fprintf(stdout, "a contain: %hhu\n",a);
 
        fprintf(stdout, "The size of an unsigned char is %hhu bytes\n",sizeof(a));
 
//unsigned half int
        unsigned short int b=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "b address: 0x%X\n",&b);
        fprintf(stdout, "b contains: %hu\n",b);
        b=b-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "b address: 0x%X\n",&b);
        fprintf(stdout, "b contain: %hu\n",b);
 
        fprintf(stdout, "The size of an unsigned short int is %hu bytes\n",sizeof(b));
 
//unsigned int
        unsigned int c=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "c address: 0x%X\n",&c);
        fprintf(stdout, "c contains: %u\n",c);
        c=c-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "c address: 0x%X\n",&c);
        fprintf(stdout, "c contain: %u\n",c);
 
        fprintf(stdout, "The size of an unsigned int is %u bytes\n",sizeof(c));
 
//unsigned long int
        unsigned long int d=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "d address: 0x%X\n",&d);
        fprintf(stdout, "d contains: %lu\n",d);
        d=d-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "d address: 0x%X\n",&d);
        fprintf(stdout, "d contain: %lu\n",d);
 
        fprintf(stdout, "The size of an unsigned long int is %lu bytes\n",sizeof(d));
 
//unsigned long long int
        unsigned long long int e=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "e address: 0x%X\n",&e);
        fprintf(stdout, "e contains: %llu\n",e);
 
        fprintf(stdout, "The size of an unsigned long long int is %llu bytes\n",sizeof(e));
 
//signed half half int
        signed char f=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "f address: 0x%X\n",&f);
        fprintf(stdout, "f contains: %hhd\n",f);
        f=f-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "f address: 0x%X\n",&f);
        fprintf(stdout, "f contain: %hhd\n",f);
 
        fprintf(stdout, "The size of an signed char is %hhd bytes\n",sizeof(f));
 
//signed half int
        signed short int g=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "g address: 0x%X\n",&g);
        fprintf(stdout, "g contains: %hd\n",g);
        g=g-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "g address: 0x%X\n",&g);
        fprintf(stdout, "g contain: %hd\n",g);
 
        fprintf(stdout, "The size of an signed short int is %hd bytes\n",sizeof(g));
 
//signed int
        signed int h=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "h address: 0x%X\n",&h);
        fprintf(stdout, "h contains: %d\n",h);
        h=h-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "h address: 0x%X\n",&h);
        fprintf(stdout, "h contain: %d\n",h);
 
        fprintf(stdout, "The size of an signed int is %d bytes\n",sizeof(h));
 
//signed long int
        signed long int i=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "i address: 0x%X\n",&i);
        fprintf(stdout, "i contains: %ld\n",i);
        i=i-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "i address: 0x%X\n",&i);
        fprintf(stdout, "i contain: %ld\n",i);
 
        fprintf(stdout, "The size of an signed long int is %ld bytes\n",sizeof(i));
 
//signed long long int
        signed long long int j=0;
        fprintf(stdout, "Before:\n-------\n");
        fprintf(stdout, "j address: 0x%X\n",&j);
        fprintf(stdout, "j contains: %lld\n",j);
        j=j-1;
        fprintf(stdout, "After:\n------\n");
        fprintf(stdout, "j address: 0x%X\n",&j);
        fprintf(stdout, "j contain: %lld\n",j);
 
        fprintf(stdout, "The size of an signed long long int is %lld bytes\n",sizeof(j));
 
        return(0);
}

Successful Program

1
#include<stdio.h>
int main()
{
 
//      signed char sclow, schigh;
//      sclow=((unsigned char)(0-1)./2)+1;
//      schigh=((unsigned char)(0-1)./2);
//      printf("low is %hhd, high is %hhd\n", sclow, schigh);
 
//%hhu unsigned half half
//%hu unsigned half int
//%u unsigned int
//%lu unsigned long int
//%llu unsigned long long int
//%hhd signed half half int
//%hd signed half int
//%d signed int
//%ld signed long int
//%lld signed long long int
 
    // Variable declarations
    //
    signed   char sc = 0;
    unsigned char uc = 0;
    signed short int ssi = 0;
    unsigned short int usi = 0;
    signed int si = 0;
    unsigned int ui = 0;
    signed long int sli = 0;
    unsigned long int uli = 0;
    signed long long int slli = 0;
    unsigned long long int ulli = 0;
 
    // Find the max value for a signed char (half of the max of an unsigned char)
    //
    sc = ((unsigned char)(uc-1)/2) + 1;
    ssi = ((unsigned short int)(usi-1)/2) + 1;
    si = ((unsigned int)(ui-1)/2) + 1;
    sli = ((unsigned long int)(uli-1)/2) + 1;
    slli = ((unsigned long long int)(ulli-1)/2) + 1;
 
    fprintf(stdout, "TYPE: unsigned char\n-------------------\n");
    fprintf(stdout, "\tsize: %hhu bytes\n", sizeof(unsigned char));
    fprintf(stdout, "\t low: %hhu\n", uc);
    fprintf(stdout, "\thigh: %hhu\n", (uc-1)); // max of unsigned is 1 less than 0-- roll-over
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE:   signed char\n-------------------\n");
    fprintf(stdout, "\tsize: %hhu bytes\n", sizeof(signed char));
    fprintf(stdout, "\t low: %hhd\n", sc);
    fprintf(stdout, "\thigh: %hhd\n",(sc-1));
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE: unsigned short int\n-------------------\n");
    fprintf(stdout, "\tsize: %hu bytes\n", sizeof(unsigned short int));
    fprintf(stdout, "\t low: %hu\n", usi);
    fprintf(stdout, "\thigh: %hu\n", (usi-1)); // max of unsigned is 1 less than 0-- roll-over
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE:   signed short int\n-------------------\n");
    fprintf(stdout, "\tsize: %hu bytes\n", sizeof(signed short int));
    fprintf(stdout, "\t low: %hd\n", ssi);
    fprintf(stdout, "\thigh: %hd\n", (ssi-1));
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE: unsigned int\n-------------------\n");
    fprintf(stdout, "\tsize: %u bytes\n", sizeof(unsigned int));
    fprintf(stdout, "\t low: %u\n", ui);
    fprintf(stdout, "\thigh: %u\n", (ui-1)); // max of unsigned is 1 less than 0-- roll-over
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE:   signed int\n-------------------\n");
    fprintf(stdout, "\tsize: %u bytes\n", sizeof(signed int));
    fprintf(stdout, "\t low: %d\n", si);
    fprintf(stdout, "\thigh: %d\n", (si-1));
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE: unsigned long int\n-------------------\n");
    fprintf(stdout, "\tsize: %lu bytes\n", sizeof(unsigned long int));
    fprintf(stdout, "\t low: %lu\n", uli);
    fprintf(stdout, "\thigh: %lu\n", (uli-1)); // max of unsigned is 1 less than 0-- roll-over
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE:   signed long int\n-------------------\n");
    fprintf(stdout, "\tsize: %lu bytes\n", sizeof(signed long int));
    fprintf(stdout, "\t low: %ld\n", sli);
    fprintf(stdout, "\thigh: %ld\n", (sli-1));
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE: unsigned long long int\n-------------------\n");
    fprintf(stdout, "\tsize: %llu bytes\n", sizeof(unsigned long long int));
    fprintf(stdout, "\t low: %llu\n", ulli);
    fprintf(stdout, "\thigh: %llu\n", (ulli-1)); // max of unsigned is 1 less than 0-- roll-over
    fprintf(stdout, "-------------------\n\n");
 
    fprintf(stdout, "TYPE:   signed long long int\n-------------------\n");
    fprintf(stdout, "\tsize: %llu bytes\n", sizeof(signed long long int));
    fprintf(stdout, "\t low: %lld\n", slli);
    fprintf(stdout, "\thigh: %lld\n", (slli-1));
    fprintf(stdout, "-------------------\n\n");
 
        return(0);
}

Execution

Incorrect Table

lab46:~/src$ ./datasize
Before:
-------
a address: 0xEE61379F
a contains: 0
After:
------
a address: 0xEE61379F
a contain: 255
The size of an unsigned char is 1 bytes
Before:
-------
b address: 0xEE61379C
b contains: 0
After:
------
b address: 0xEE61379C
b contain: 65535
The size of an unsigned short int is 2 bytes
Before:
-------
c address: 0xEE613798
c contains: 0
After:
------
c address: 0xEE613798
c contain: 4294967295
The size of an unsigned int is 4 bytes
Before:
-------
d address: 0xEE613790
d contains: 0
After:
------
d address: 0xEE613790
d contain: 18446744073709551615
The size of an unsigned long int is 8 bytes
Before:
-------
e address: 0xEE613788
e contains: 0
After:
------
e address: 0xEE613788
e contain: 18446744073709551615
The size of an unsigned long long int is 8 bytes
Before:
-------
f address: 0xEE613787
f contains: 0
After:
------
f address: 0xEE613787
f contain: -1
The size of an signed char is 1 bytes
Before:
-------
g address: 0xEE613784
g contains: 0
After:
------
g address: 0xEE613784
g contain: -1
The size of an signed short int is 2 bytes
Before:
-------
h address: 0xEE613780
h contains: 0
After:
------
h address: 0xEE613780
h contain: -1
The size of an signed int is 4 bytes
Before:
-------
i address: 0xEE613778
i contains: 0
After:
------
i address: 0xEE613778
i contain: -1
The size of an signed long int is 8 bytes
Before:
-------
j address: 0xEE613770
j contains: 0
After:
------
j address: 0xEE613770
j contain: -1
The size of an signed long long int is 8 bytes
lab46:~/src$ 

Correct Table

lab46:~/src$ ./datasize2
TYPE: unsigned char
-------------------
	size: 1 bytes
	 low: 0
	high: 255
-------------------

TYPE:   signed char
-------------------
	size: 1 bytes
	 low: -128
	high: 127
-------------------

TYPE: unsigned short int
-------------------
	size: 2 bytes
	 low: 0
	high: 65535
-------------------

TYPE:   signed short int
-------------------
	size: 2 bytes
	 low: -32768
	high: 32767
-------------------

TYPE: unsigned int
-------------------
	size: 4 bytes
	 low: 0
	high: 4294967295
-------------------

TYPE:   signed int
-------------------
	size: 4 bytes
	 low: -2147483648
	high: 2147483647
-------------------

TYPE: unsigned long int
-------------------
	size: 8 bytes
	 low: 0
	high: 18446744073709551615
-------------------

TYPE:   signed long int
-------------------
	size: 8 bytes
	 low: -9223372036854775808
	high: 9223372036854775807
-------------------

TYPE: unsigned long long int
-------------------
	size: 8 bytes
	 low: 0
	high: 18446744073709551615
-------------------

TYPE:   signed long long int
-------------------
	size: 8 bytes
	 low: -9223372036854775808
	high: 9223372036854775807
-------------------

lab46:~/src$ 

Reflection

Along with the problems with the original program, the fixed program had an initial issue of the highs and lows of the signed. I simply moved some code around to make it output correctly.

Reference