0
0
Embedded Cprogramming~10 mins

sizeof and memory budgeting in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the size of an int variable in bytes.

Embedded C
#include <stdio.h>

int main() {
    int x;
    printf("Size of int: %zu bytes\n", sizeof([1]));
    return 0;
}
Drag options to blanks, or click blank then click option'
Ax
Bsizeof
Cint
D&x
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name without declaration
Using the address operator & inside sizeof incorrectly
2fill in blank
medium

Complete the code to calculate total memory needed for an array of 10 floats.

Embedded C
#include <stdio.h>

int main() {
    size_t total_size = 10 * sizeof([1]);
    printf("Total memory: %zu bytes\n", total_size);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aint
Bdouble
Cchar
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong type inside sizeof
Forgetting to multiply by the number of elements
3fill in blank
hard

Fix the error in the code to correctly print the size of a pointer to char.

Embedded C
#include <stdio.h>

int main() {
    char *ptr;
    printf("Size of pointer: %zu bytes\n", sizeof([1]));
    return 0;
}
Drag options to blanks, or click blank then click option'
A*ptr
Bptr
Cchar
D&ptr
Attempts:
3 left
💡 Hint
Common Mistakes
Using *ptr inside sizeof instead of ptr
Using &ptr which is pointer to pointer
4fill in blank
hard

Fill both blanks to create a dictionary (struct) that stores word lengths only if length is greater than 3.

Embedded C
#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;
}
Drag options to blanks, or click blank then click option'
Astrlen
Bsizeof
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using sizeof instead of strlen for string length
Using less than operator instead of greater than
5fill in blank
hard

Fill all three blanks to create a memory budget dictionary for variables with conditions.

Embedded C
#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;
}
Drag options to blanks, or click blank then click option'
Aint
Bchar
Cfloat
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up types and their sizes
Using variable names instead of types inside sizeof