-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex8.c
89 lines (73 loc) · 2.82 KB
/
ex8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
int main(int argc, char *argv[])
{
// Removing the trailing '\0' results in the following
// Valgrind warning:
// "Conditional jump or move depends on uninitialised value(s)"
char full_name[] = {
'Z','e','d',
' ','A','.', ' ',
'S','h','a','w', '\0'
};
// Note: initializing with "Z\0ed" results in just "Z" being
// printed everywhere.
char name[] = "Zed";
int areas[] = {10, 12, 13, 14, 20};
// The below program prints the following:
//
// The size of an int: 4
// The size of areas (int[]): 20
// The number of ints in areas 5
// The first areas is 10, the 2nd 12.
// The size of a char: 1
// the size of name (char[]): 4
// the number of chars: 4
// The size of full_name (char[]): 12
// The number of chars: 12
// name="Zed" and full_name="Zed A. Shaw"
// WARNING: On some systems you may have to change the
// %ld in this code to %u since it will use unsigned ints
printf("The size of an int: %ld\n", sizeof(int));
printf("The size of areas (int[]): %ld\n",
sizeof(areas));
printf("The number of ints in areas %ld\n",
sizeof(areas) / sizeof(int));
// Setting areas[10] prints '0' and results in valgrind warning of
// "Conditional jump or move depends on uninitialised value(s)"
printf("The 1st element in areas is %d, the 2nd %d.\n",
areas[0], areas[1]);
printf("The size of a char: %ld\n", sizeof(char));
printf("the size of name (char[]): %ld\n",
sizeof(name));
printf("the number of chars: %ld\n",
sizeof(name) / sizeof(char));
printf("The size of full_name (char[]): %ld\n",
sizeof(full_name));
printf("The number of chars: %ld\n",
sizeof(full_name) / sizeof(char));
// removing the trailing \0 from the full_name array results
// in Valgrind outputing the following warning:
// "Conditional jump or move depends on uninitialised value(s)"
printf("name=\"%s\" and full_name=\"%s\"\n",
name, full_name);
////////////////////////////////////////////////////////////////
// EXTRA CREDIT
puts("EXTRA CREDIT...");
// 1) Assign elements in the areas array, for example:
puts("Assigning 100 to areas[0]...");
areas[0] = 100;
printf("areas[0] = %i\n", areas[0]);
// 2) Try assigning elements of name to full_name
puts("Assigning the first letter of name to the Z in full name...");
full_name[7] = name[0];
printf("full_name is now: %s\n", full_name); // Zed A. Zhaw
// 3) integer sizes on various CPUs
// See: http://www.oreillynet.com/network/2003/10/07/michael_barr.html
// - Motorola 68000 family: 16 bit = 65,536 unsigned values
// - Cobra (A10) processor: 48 bit = 281,474,976,710,656 unsigned values
// NOTE: The "^" operator is logical XOR. To calculate powers, you need
// the pow() function from <math.h>
////////////////////////////////////////////////////////////////
// End Extra Credit
return 0;
}