Write a program that uses the sizeof operator to determine the sizes in bytes of
ID: 3621715 • Letter: W
Question
Write a program that uses the sizeof operator to determine the sizes in bytes of the various data on your computer system. Write the result to the file "datasize.dat" so you may print the results later. The format for the results in the file should be as follows:
Data type Size
Char 1
unsigned char 1
short int 2
unsigned short int 2
int 4
unsigned in 4
long int 4
unsigned long int 4
float 4
double 8
long double 16
[Note: That type sizes on your computer might be different from those listed above.]
[Note: Prefer C programming language but can also use C++]
Explanation / Answer
Hope this helps. Let me know if you have any questions. Please rate. :) #include int main() { FILE * outFile; outFile = fopen("datasize.dat", "w"); // This shouldn't be an issue, but just in case we // can't create the output file, we should exit if (outFile == NULL){ printf("Couldn't open output file datasize.dat. Terminating program. "); return -1; } fprintf(outFile, "%-25s Size ", "Data type"); fprintf(outFile, "%-25s %d ", "char", sizeof(char)); fprintf(outFile, "%-25s %d ", "unsigned char", sizeof(unsigned char)); fprintf(outFile, "%-25s %d ", "short int", sizeof(short int)); fprintf(outFile, "%-25s %d ", "unsigned short int", sizeof(unsigned short int)); fprintf(outFile, "%-25s %d ", "int", sizeof(int)); fprintf(outFile, "%-25s %d ", "unsigned int", sizeof(unsigned int)); fprintf(outFile, "%-25s %d ", "long int", sizeof(long int)); fprintf(outFile, "%-25s %d ", "unsigned long int", sizeof(unsigned long int)); fprintf(outFile, "%-25s %d ", "float", sizeof(float)); fprintf(outFile, "%-25s %d ", "double", sizeof(double)); fprintf(outFile, "%-25s %d ", "long double", sizeof(long double)); fclose(outFile); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.