Completed: / exercises

Variables in C

The real thing (0.5p)

Describe how C variable int32_t luku = -345; would be stored in to an 8-bit memory.
Use the following format in your answer, and use as many memory locations as needed. Addresses start from zero and both address (4 bits) and value (8 bits) are given as binary numbers.
memory_address:value
You must write one line for each memory address.
Example. Variable uint8_t example = 9; would be stored in this memory followingly:
0000:00001001
You have only 5 attempts to answer correctly this question.

Warning: You have not logged in. You cannot answer.

Choosing Memory-Efficient Variable Types (0.75)

In C, the amount of memory occupied by some variable types depends on the compiler and target architecture.
Consider the following program:
#include <stdio.h>

int main(void) {
    printf("Size of char: %zu bytes\n", sizeof(char));
    printf("Size of signed char: %zu bytes\n", sizeof(signed char));
    printf("Size of unsigned char: %zu bytes\n", sizeof(unsigned char));
    printf("Size of short int: %zu bytes\n", sizeof(short int));
    printf("Size of unsigned short int: %zu bytes\n", sizeof(unsigned short int));
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of unsigned int: %zu bytes\n", sizeof(unsigned int));
    printf("Size of signed long int: %zu bytes\n", sizeof(signed long int));
    printf("Size of unsigned long int: %zu bytes\n", sizeof(unsigned long int));
    printf("Size of float: %zu bytes\n", sizeof(float));

    return 0;
}
When compiled and executed on the computer used for this exercise, the program produces:
Size of char: 1 bytes
Size of signed char: 1 bytes
Size of unsigned char: 1 bytes
Size of short int: 2 bytes
Size of unsigned short int: 2 bytes
Size of int: 4 bytes
Size of unsigned int: 4 bytes
Size of signed long int: 8 bytes
Size of unsigned long int: 8 bytes
Size of float: 4 bytes
Each variable below may contain any value between the given minimum and maximum.
Declare each variable using the variable type that occupies the least possible amount of memory while still being able to represent the complete range of values.
Variable Minimum value Maximum value
a 'A' 'Z'
b 0 255
c -129 127
e -32769 32767
f -2147483649 2147483647
g -3.14 3.14
For this exercise:
  • Do not use types from <stdint.h>, such as int8_t, uint8_t, int16_t, or uint16_t.
  • Remember to use adequate keywords when needed: short, long, signed...
  • Write only the variable declarations. Do not initialize the variables.
  • Write one declaration per line. Do not leave blank lines or whitespaces
For example:
int a;
long int b; 

Warning: You have not logged in. You cannot answer.

Mysteries of C variables (0.25p)

Which of the following statements are true? You have only 5 attempts to answer, so please, think carefully your answers.
Warning: You have not logged in. You cannot answer.
?