CC=gcc
CFLAGS=-std=c99 -Wall -Wextra
-all: exercise1 exercise2
+all: exercise1 exercise2 exercise3
+
+exercise3: exercise3.c
+ $(CC) $(CFLAGS) -O3 exercise3.c -o exercise3
exercise2: exercise2.c
$(CC) $(CFLAGS) -O3 exercise2.c -o exercise2
$(CC) $(CFLAGS) exercise1d.c -o exercise1d
clean:
- $(RM) exercise1b exercise1d exercise2
+ $(RM) exercise1b exercise1d exercise2 exercise3
int main(void)
{
FILE *f = fopen("/dev/urandom", "r");
- char c;
+ unsigned char c;
do {
c = fgetc(f);
printf("%i %u %x\n", c, c, c);
--82 4294967214 ffffffae
-91 91 5b
--63 4294967233 ffffffc1
--41 4294967255 ffffffd7
--22 4294967274 ffffffea
-103 103 67
--33 4294967263 ffffffdf
--115 4294967181 ffffff8d
--90 4294967206 ffffffa6
+150 150 96
+127 127 7f
+119 119 77
+46 46 2e
+190 190 be
+59 59 3b
+65 65 41
+27 27 1b
+125 125 7d
+26 26 1a
+171 171 ab
+8 8 8
+29 29 1d
+178 178 b2
+226 226 e2
+21 21 15
+229 229 e5
+31 31 1f
+106 106 6a
+100 100 64
+201 201 c9
+24 24 18
+225 225 e1
+82 82 52
+4 4 4
+9 9 9
+12 12 c
+44 44 2c
+167 167 a7
+178 178 b2
+31 31 1f
+202 202 ca
+211 211 d3
+113 113 71
+86 86 56
+65 65 41
+79 79 4f
+191 191 bf
+188 188 bc
+45 45 2d
+199 199 c7
+122 122 7a
+203 203 cb
+217 217 d9
+6 6 6
+66 66 42
+72 72 48
+167 167 a7
+116 116 74
+107 107 6b
+88 88 58
+22 22 16
+3 3 3
+65 65 41
+17 17 11
+155 155 9b
+178 178 b2
+144 144 90
+193 193 c1
+196 196 c4
+44 44 2c
+124 124 7c
+102 102 66
+18 18 12
+132 132 84
+86 86 56
+199 199 c7
+35 35 23
+175 175 af
+218 218 da
+125 125 7d
+181 181 b5
+201 201 c9
+123 123 7b
+240 240 f0
+224 224 e0
+96 96 60
+87 87 57
+17 17 11
+160 160 a0
+101 101 65
+57 57 39
+59 59 3b
+201 201 c9
+138 138 8a
+82 82 52
+26 26 1a
+5 5 5
+108 108 6c
+62 62 3e
+175 175 af
+236 236 ec
+252 252 fc
+6 6 6
+226 226 e2
+40 40 28
+125 125 7d
+138 138 8a
+242 242 f2
+149 149 95
+162 162 a2
+57 57 39
+228 228 e4
108 108 6c
60 60 3c
--42 4294967254 ffffffd6
--55 4294967241 ffffffc9
--26 4294967270 ffffffe6
-25 25 19
+254 254 fe
+57 57 39
+174 174 ae
+228 228 e4
+125 125 7d
+52 52 34
+200 200 c8
+183 183 b7
+255 255 ff
+170 170 aa
+76 76 4c
+63 63 3f
+242 242 f2
+158 158 9e
+57 57 39
+235 235 eb
+221 221 dd
+90 90 5a
+233 233 e9
+235 235 eb
+170 170 aa
+34 34 22
+52 52 34
+98 98 62
+21 21 15
+145 145 91
+222 222 de
+9 9 9
+61 61 3d
+52 52 34
+222 222 de
+240 240 f0
+87 87 57
+218 218 da
+187 187 bb
+10 10 a
+150 150 96
+3 3 3
+143 143 8f
+9 9 9
+241 241 f1
+154 154 9a
+190 190 be
+140 140 8c
+86 86 56
+163 163 a3
+151 151 97
+214 214 d6
+26 26 1a
+207 207 cf
+19 19 13
+10 10 a
+92 92 5c
+26 26 1a
+194 194 c2
+23 23 17
+99 99 63
+185 185 b9
+221 221 dd
+126 126 7e
+133 133 85
+236 236 ec
+152 152 98
+154 154 9a
+30 30 1e
+250 250 fa
+119 119 77
+215 215 d7
+201 201 c9
+134 134 86
+119 119 77
+119 119 77
+117 117 75
+104 104 68
+142 142 8e
+206 206 ce
+138 138 8a
+24 24 18
+65 65 41
+116 116 74
+219 219 db
+184 184 b8
+111 111 6f
+239 239 ef
+232 232 e8
+151 151 97
+143 143 8f
+252 252 fc
+187 187 bb
+253 253 fd
+154 154 9a
+90 90 5a
+216 216 d8
+224 224 e0
+182 182 b6
+3 3 3
+190 190 be
+140 140 8c
+181 181 b5
+126 126 7e
+40 40 28
+127 127 7f
+74 74 4a
+134 134 86
+91 91 5b
+163 163 a3
+69 69 45
+34 34 22
+35 35 23
+140 140 8c
+165 165 a5
+135 135 87
+69 69 45
+135 135 87
+29 29 1d
+112 112 70
+53 53 35
+160 160 a0
+160 160 a0
+187 187 bb
+13 13 d
+195 195 c3
+225 225 e1
+31 31 1f
+202 202 ca
+88 88 58
+196 196 c4
+8 8 8
+241 241 f1
+222 222 de
+223 223 df
+238 238 ee
+49 49 31
+34 34 22
+209 209 d1
+76 76 4c
+93 93 5d
+128 128 80
+186 186 ba
+96 96 60
+254 254 fe
+179 179 b3
+3 3 3
+101 101 65
+87 87 57
+129 129 81
+114 114 72
+61 61 3d
+113 113 71
+138 138 8a
+223 223 df
+140 140 8c
+142 142 8e
+1 1 1
+131 131 83
+124 124 7c
+6 6 6
+82 82 52
+167 167 a7
+19 19 13
+155 155 9b
+145 145 91
+182 182 b6
+205 205 cd
+221 221 dd
+41 41 29
+80 80 50
+52 52 34
+214 214 d6
+73 73 49
+69 69 45
+1 1 1
+55 55 37
+144 144 90
+144 144 90
+186 186 ba
+211 211 d3
+39 39 27
+172 172 ac
+130 130 82
+56 56 38
+134 134 86
+213 213 d5
+21 21 15
+73 73 49
+101 101 65
+43 43 2b
+225 225 e1
+210 210 d2
+231 231 e7
+125 125 7d
+11 11 b
+116 116 74
+192 192 c0
+33 33 21
+234 234 ea
42 42 2a
FILE *f = fopen("/dev/urandom", "r");
uint16_t c;
do {
- c = fgetc(f)+fgetc(f)*256;
+ c = fgetc(f) + fgetc(f) * 256;
printf("%04x\n", c);
} while(c != 42);
fclose(f);
void print(char *address, unsigned int size)
{
- printf("address: %p, size: %u, psize: %u\n", address, size, sizeof(address));
- for(unsigned char i=0; i<size; i++)
- {
- int value = *((char*)(address+i));
- printf("%p %x %u\n", address+i, value, value);
+ for(unsigned char i = 0; i < size; i++) {
+ unsigned char value = (unsigned char)*(address + i);
+ printf("%p 0x%02X %3u\n", address + i, value, value);
}
}
{
short i = 0x1234;
char x = -127;
- long sn1 = 4109503;/*
+ long sn1 = 4109503;
long sn2 = 4202015;
- int y[2] = {0x11223344, 0x443332211};*/
- printf("address content (hex) content(dec)\n");
-
+ int y[2] = {0x11223344, 0x44332211};
+ printf("address content (hex) content(dec)\n");
+ printf("------------------------------------------\n");
print((char *)&i, sizeof(i));
print((char *)&x, sizeof(x));
print((char *)&sn1, sizeof(sn1));
+ print((char *)&sn2, sizeof(sn2));
+ print((char *)&y, sizeof(y));
return 0;
}
--- /dev/null
+Markings in the last column denote the bytes representing the marking from the
+first byte after the previous marking up to and including the byte in the
+column of the marking. On this configuration supposedly stores the values in a
+little endian byte order, this is clearly visible in the values of the longs.
+
+Address Hex value Decimal value Explanation
+0x7fffbe1976be 0x34 52
+0x7fffbe1976bf 0x12 18 short i = 0x1234
+0x7fffbe1976bd 0x81 129 char x = -127
+0x7fffbe1976d0 0xBF 191
+0x7fffbe1976d1 0xB4 180
+0x7fffbe1976d2 0x3E 62
+0x7fffbe1976d3 0x00 0
+0x7fffbe1976d4 0x00 0
+0x7fffbe1976d5 0x00 0
+0x7fffbe1976d6 0x00 0
+0x7fffbe1976d7 0x00 0 long sn1 = 4109503
+0x7fffbe1976d8 0x1F 31
+0x7fffbe1976d9 0x1E 30
+0x7fffbe1976da 0x40 64
+0x7fffbe1976db 0x00 0
+0x7fffbe1976dc 0x00 0
+0x7fffbe1976dd 0x00 0
+0x7fffbe1976de 0x00 0
+0x7fffbe1976df 0x00 0 long sn1 = 4109503
+0x7fffbe1976c0 0x44 68
+0x7fffbe1976c1 0x33 51
+0x7fffbe1976c2 0x22 34
+0x7fffbe1976c3 0x11 17 int y[0] = 0x11223344
+0x7fffbe1976c4 0x11 17
+0x7fffbe1976c5 0x22 34
+0x7fffbe1976c6 0x33 51
+0x7fffbe1976c7 0x44 68 int y[0] = 0x44332211
--- /dev/null
+address content (hex) content(dec)
+------------------------------------------
+0x7fffbe1976be 0x34 52
+0x7fffbe1976bf 0x12 18
+0x7fffbe1976bd 0x81 129
+0x7fffbe1976d0 0xBF 191
+0x7fffbe1976d1 0xB4 180
+0x7fffbe1976d2 0x3E 62
+0x7fffbe1976d3 0x00 0
+0x7fffbe1976d4 0x00 0
+0x7fffbe1976d5 0x00 0
+0x7fffbe1976d6 0x00 0
+0x7fffbe1976d7 0x00 0
+0x7fffbe1976d8 0x1F 31
+0x7fffbe1976d9 0x1E 30
+0x7fffbe1976da 0x40 64
+0x7fffbe1976db 0x00 0
+0x7fffbe1976dc 0x00 0
+0x7fffbe1976dd 0x00 0
+0x7fffbe1976de 0x00 0
+0x7fffbe1976df 0x00 0
+0x7fffbe1976c0 0x44 68
+0x7fffbe1976c1 0x33 51
+0x7fffbe1976c2 0x22 34
+0x7fffbe1976c3 0x11 17
+0x7fffbe1976c4 0x11 17
+0x7fffbe1976c5 0x22 34
+0x7fffbe1976c6 0x33 51
+0x7fffbe1976c7 0x44 68
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+int main(void){
+ printf("A bool uses %lu bytes\n", sizeof(bool));
+ printf("Hex representation of true: %X\n", true);
+ printf("Hex representation of false: %X\n", false);
+/* Running the following code shows that all but the 0x00 byte evaluate true */
+/* for(unsigned char i = 0; i<255; i++)
+ printf("Hex %x evaluates to %s\n", i, i ? "true" : "false");*/
+ return 0;
+}