//========================================================================= #define PROGRAMMER "INSTRUCTOR, The" #define PROG_CODE "INST" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Summer" #define SECTION (0) #define ASSIGNMENT "HW#2" #define REVISION (0) #define TITLE "Working with Data Representations" #define SUBTITLE "Integer, Floating Point, Character" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "mod2ar0.c" //========================================================================= //========================================================================= // PROBLEM //========================================================================= // // This program allows the user to enter values and see how they are // represented in memory under the various data representations. // //========================================================================= // PSEUDOCODE //========================================================================= // // No pseudocode provided. // //========================================================================= // DEVIATIONS FROM SUBMITTED PSEUDOCODE //========================================================================= // // No pseudocode provided. // //========================================================================= // WAIVED COMPILER WARNINGS //========================================================================= // // Linker Warning: No module definition file specified: using defaults // Reason for Waiver: Can't suppress. Does not have adverse impact. // //========================================================================= //========================================================================= // CODE SECTION //========================================================================= //========================================================================= //== INCLUDE FILES ======================================================== #include // printf() #include // malloc() //== MACRO DEFINITIONS ==================================================== #define FALSE (0) #define TRUE (!FALSE) #define BLANKLINE printf("\n") #define BYTES (20) //== EXIT CODES =========================================================== #define NORMAL_EXIT (0) //== FUNCTION PROTOTYPES (for Primary Functions) ========================== void PrintHeader(void); //== MAIN FUNCTION ======================================================== int main(void) { void *bytes; // 'bytes' holds and address (of undeclared type) short *p_short; int *p_int; long *p_long; float *p_float; double *p_double; int i; PrintHeader(); BLANKLINE; // Print Size Table printf("Type Bytes\n"); printf("char ......... %2i\n", sizeof(char)); printf("short ........ %2i\n", sizeof(short)); printf("int .......... %2i\n", sizeof(int)); printf("long ......... %2i\n", sizeof(long)); printf("float ........ %2i\n", sizeof(float)); printf("double ....... %2i\n", sizeof(double)); printf("long double .. %2i\n", sizeof(long double)); printf("pointer ...... %2i\n", sizeof(void *)); BLANKLINE; // Allocate BYTES bytes of memory and place starting address in 'bytes' bytes = malloc(BYTES); // Set all data pointers to beginning of memory block p_short = (short *) bytes; p_int = (int *) bytes; p_long = (long *) bytes; p_float = (float *) bytes; p_double = (double *)bytes; // Print initial contents of block printf("Initial values stored in data block (stored at %p):\n", bytes); for(i = 0; i < BYTES; i++) { printf("%02X ", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a positive integer stored as a short printf("Enter a positive integer to be stored as a short: "); scanf("%hi", p_short); printf("Value entered: %hi, %hi\n", *p_short, *((short *) bytes)); printf("Value stored as 'short' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(short) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a negative integer stored as a short printf("Enter a negative integer to be stored as a short: "); scanf("%hi", p_short); printf("Value entered: %hi, %hi\n", *p_short, *((short *) bytes)); printf("Value stored as 'short' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(short) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a positive integer stored as an int printf("Enter a positive integer to be stored as an int: "); scanf("%i", p_int); printf("Value entered: %i, %i\n", *p_int, *((int *) bytes)); printf("Value stored as 'int' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(int) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a negative integer stored as an int printf("Enter a negative integer to be stored as an int: "); scanf("%i", p_int); printf("Value entered: %i, %i\n", *p_int, *((int *) bytes)); printf("Value stored as 'int' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(int) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a positive integer stored as a long int printf("Enter a positive integer to be stored as a long: "); scanf("%li", p_long); printf("Value entered: %li, %li\n", *p_long, *((long *) bytes)); printf("Value stored as 'long int' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(long) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a negative integer stored as a long int printf("Enter a negative integer to be stored as a long: "); scanf("%li", p_long); printf("Value entered: %li, %li\n", *p_long, *((long *) bytes)); printf("Value stored as 'long int' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(long) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a positive integer stored as a float printf("Enter a positive integer to be stored as a float: "); scanf("%f", p_float); printf("Value entered: %f, %f\n", *p_float, *((float *) bytes)); printf("Value stored as 'float' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(float) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a negative integer stored as a float printf("Enter a negative integer to be stored as a float: "); scanf("%f", p_float); printf("Value entered: %f, %f\n", *p_float, *((float *) bytes)); printf("Value stored as 'float' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(float) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a positive integer stored as a double printf("Enter a positive integer to be stored as a double: "); scanf("%lf", p_double); printf("Value entered: %f, %f\n", *p_double, *((double *) bytes)); printf("Value stored as 'double' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(double) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; // Representation of a negative integer stored as a double printf("Enter a negative integer to be stored as a double: "); scanf("%lf", p_double); printf("Value entered: %f, %f\n", *p_double, *((double *) bytes)); printf("Value stored as 'double' at beginning of data block:\n"); for(i = 0; i < BYTES; i++) { printf("%c", (sizeof(double) == i)? '|' : ' '); printf("%02X", *((unsigned char *)bytes + i)); } BLANKLINE; BLANKLINE; printf("Program Completed."); return(NORMAL_EXIT); } //========================================================================= // PRIMARY FUNCTIONS (functions called directly by main() ) //========================================================================= //== FUNCTION PROTOTYPES (for Support Functions) ========================== int printc(char c, int n); //== PRIMARY FUNCTIONS ==================================================== void PrintHeader(void) { printc('=', 79); printf("\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printc('=', 79); printf("\n"); return; } //========================================================================= // SUPPORT FUNCTIONS (functions not called directly by main() ) //========================================================================= int printc(char c, int n) { while(0 < n--) printf("%c", c); return(n); }