-
Notifications
You must be signed in to change notification settings - Fork 3
/
ass6_20CS30042_test3.c
105 lines (92 loc) · 3.02 KB
/
ass6_20CS30042_test3.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
int printStr(char *s);
int readInt(int *eP);
int printInt(int n);
int numG1 = 20, numG2;
char charG1 = 'b', charG2;
int *ptrG1, *ptrG2;
char *strG1 = "Hello World, I am a global string.", *strG2;
int main() {
printStr("\n#### TEST 3 (Global variables, pointers and addresses) ####");
int numL1 = 5, numL2;
char charL1 = 'a', charL2;
int *ptrL1 = &numL1, *ptrL2;
char *strL1 = "Hello World, I am a local string.", *strL2;
printStr("\nLocal variables: ");
printStr("\nnumL1 = ");
printInt(numL1);
printStr(", charL1 (ascii value) = ");
printInt(charL1);
printStr(", ptrL1 (adress, truncated to first 32 bits only) = ");
printInt(ptrL1);
printStr(", strL1 = ");
printStr(strL1);
ptrG1 = &numG1;
printStr("\nGlobal variables: ");
printStr("\nnumG1 = ");
printInt(numG1);
printStr(", charG1 (ascii value) = ");
printInt(charG1);
printStr(", ptrG1 (adress, truncated to first 32 bits only) = ");
printInt(ptrG1);
printStr(", strG1 = ");
printStr(strG1);
printStr("\n");
printStr("\nAssigning locals to globals: ");
numG2 = numL1;
charG2 = charL1;
ptrG2 = ptrL1;
strG2 = strL1;
printStr("\nnumG2 = ");
printInt(numG2);
printStr(", charG2 (ascii value) = ");
printInt(charG2);
printStr(", ptrG2 (adress, truncated to first 32 bits only) = ");
printInt(ptrG2);
printStr(", strG2 = ");
printStr(strG2);
if(numG2 == numL1 && charG2 == charL1 && ptrG2 == ptrL1 && strG2 == strL1) {
printStr("\nSUCCESS: Local variables assigned to global variables.");
} else {
printStr("\nFAILURE: Local variables not assigned to global variables.");
}
printStr("\n");
printStr("\nAssigning globals to locals: ");
numL2 = numG1;
charL2 = charG1;
ptrL2 = ptrG1;
strL2 = strG1;
printStr("\nnumL2 = ");
printInt(numL2);
printStr(", charL2 (ascii value) = ");
printInt(charL2);
printStr(", ptrL2 (adress, truncated to first 32 bits only) = ");
printInt(ptrL2);
printStr(", strL2 = ");
printStr(strL2);
if(numL2 == numG1 && charL2 == charG1 && ptrL2 == ptrG1 && strL2 == strG1) {
printStr("\nSUCCESS: Global variables assigned to local variables.");
} else {
printStr("\nFAILURE: Global variables not assigned to local variables.");
}
printStr("\n");
printStr("\nAssigning globals to globals: ");
numG2 = numG1;
charG2 = charG1;
ptrG2 = ptrG1;
strG2 = strG1;
printStr("\nnumG2 = ");
printInt(numG2);
printStr(", charG2 (ascii value) = ");
printInt(charG2);
printStr(", ptrG2 (adress, truncated to first 32 bits only) = ");
printInt(ptrG2);
printStr(", strG2 = ");
printStr(strG2);
if(numG2 == numG1 && charG2 == charG1 && ptrG2 == ptrG1 && strG2 == strG1) {
printStr("\nSUCCESS: Global variables assigned to global variables.");
} else {
printStr("\nFAILURE: Global variables not assigned to global variables.");
}
printStr("\n\n");
return 0;
}