/* sizes.c Jeff Ondich, 9 Jan 2022 Modified by Tanya Amert for Fall 2024 How many bytes are in some of our favorite types? Compile as usual: gcc -Wall -Werror -g -o sizes sizes.c Run with no command-line arguments: ./sizes */ #include // Prints the sizes (in bytes) of many common types, and some // not-so-common ones. int main(int argc, char *argv[]) { // Common types: whole numbers printf("sizeof(char) = %ld\n", sizeof(char)); printf("sizeof(short) = %ld\n", sizeof(short)); printf("sizeof(int) = %ld\n", sizeof(int)); printf("sizeof(long) = %ld\n", sizeof(long)); printf("\n"); // Common types: oooh, stars printf("sizeof(char *) = %ld\n", sizeof(char *)); printf("sizeof(int *) = %ld\n", sizeof(int *)); printf("\n"); // Common types: floating-point numbers printf("sizeof(float) = %ld\n", sizeof(float)); printf("sizeof(double) = %ld\n", sizeof(double)); printf("\n"); // The sizeof operator returns something of type size_t printf("sizeof(size_t) = %ld\n", sizeof(size_t)); printf("\n"); // TODO: Predict what the next two blocks of code will // do BEFORE you run them. Were you right? If not, why? // // We can use typedef to give fun/useful names to types // typedef unsigned short dog; // dog cheddar = 4; // printf("sizeof(cheddar) = %ld\n", sizeof(cheddar)); // printf("sizeof(dog) = %ld\n", sizeof(dog)); // printf("\n"); // // What about C-strings and arrays? // char *mystr = "hi"; // char myarr[3] = { 'h', 'i', '\0' }; // printf("sizeof(\"hi\") = %ld\n", sizeof("hi")); // printf("sizeof(mystr) = %ld\n", sizeof(mystr)); // printf("sizeof(myarr) = %ld\n", sizeof(myarr)); return 0; }