Complete the code to print the size of an int variable in bytes.
#include <stdio.h> int main() { int x; printf("Size of int: %zu bytes\n", sizeof([1])); return 0; }
The sizeof operator expects a type or variable. Using int gives the size of the int type.
Complete the code to calculate total memory needed for an array of 10 floats.
#include <stdio.h> int main() { size_t total_size = 10 * sizeof([1]); printf("Total memory: %zu bytes\n", total_size); return 0; }
We want the size of a float because the array holds floats.
Fix the error in the code to correctly print the size of a pointer to char.
#include <stdio.h> int main() { char *ptr; printf("Size of pointer: %zu bytes\n", sizeof([1])); return 0; }
To get the size of the pointer itself, use the pointer variable ptr. Using *ptr gives size of the char it points to.
Fill both blanks to create a dictionary (struct) that stores word lengths only if length is greater than 3.
#include <stdio.h> #include <string.h> struct WordLength { char word[20]; int length; }; int main() { struct WordLength wl[5]; char *words[] = {"cat", "house", "dog", "elephant", "bird"}; int count = 0; for (int i = 0; i < 5; i++) { int len = [1](words[i]); if (len [2] 3) { // store word and length count++; } } printf("Words longer than 3: %d\n", count); return 0; }
Use strlen to get the length of the word, and check if it is greater than 3.
Fill all three blanks to create a memory budget dictionary for variables with conditions.
#include <stdio.h> struct MemoryBudget { char name[10]; size_t size; }; int main() { struct MemoryBudget mb[3]; char *vars[] = {"a", "b", "c"}; size_t sizes[] = {sizeof([1]), sizeof([2]), sizeof([3])}; for (int i = 0; i < 3; i++) { // store variable name and size } return 0; }
The sizes array stores sizes of double, char, and float types respectively.