1) Type in the following C program which prints the size of a “char” type, but d
ID: 3721602 • Letter: 1
Question
1) Type in the following C program which prints the size of a “char” type, but duplicate the printf() line multiple times, changing it so it also prints the size of types: unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, float, double, char*, and long*. Compile, run and record the results.
#include int main() { printf(“sizeof char = %d ”, sizeof(char)); return 0; }
Note that the code for the following exercises may give compiler warnings. Normally you will want to fix your code so you don't get any warnings, as it usually indicates undesirable behavior (or at minimum it reveals tricky things that make your code harder to understand and verify).
2a) Write a short program which includes multiple lines similar to the following. The other lines should substitute the numbers 150, 150, 300, 1, and 65535. Run and record the results. Also give each result in 16bit binary. printf("%d %u ", (short)45, (unsigned short)45); 2b) Also, include multiple pairs of lines similar to the following, where c is declared as a char type. The other pairs of lines should substitute the values of 150, 150, 300, 1, and 65535. Run and record the results. Also give each result in 16bit binary. c = 45; printf("%d %d %u ", 45, (short)c, (unsigned short)c);
2c) Also, include multiple pairs of lines similar to the following, where uc is declared as an unsigned char type. The other pairs of lines should substitute the values of 150, 150, 300, 1, and 65535. Run and record the results. Also give each result in 16bit binary. uc = 45; printf("%d %d %u ", 45, (short)uc, (unsigned short)uc);
Explanation / Answer
Doing first one as per chegg policy
#include <stdio.h>
int main() {
printf("sizeof char = %d ", sizeof(char));
printf("sizeof unsigned char = %d ", sizeof(unsigned char));
printf("sizeof short = %d ", sizeof(short));
printf("sizeof unsigned short = %d ", sizeof(unsigned short));
printf("sizeof int = %d ", sizeof(int));
printf("sizeof unsigned int = %d ", sizeof(unsigned int));
printf("sizeof long = %d ", sizeof(long));
printf("sizeof unsigned long = %d ", sizeof(unsigned long));
printf("sizeof long long = %d ", sizeof(long long));
printf("sizeof unsigned long long = %d ", sizeof(unsigned long long));
printf("sizeof float = %d ", sizeof(float));
printf("sizeof double = %d ", sizeof(double));
printf("sizeof char* = %d ", sizeof(char*));
printf("sizeof long* = %d ", sizeof(long*));
return 0;
}
Sample run
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.